Browse Source

Pass golint

master
parent
commit
70b1769ece
5 changed files with 47 additions and 5 deletions
  1. +7
    -0
      client.go
  2. +3
    -2
      cmd/client.go
  3. +1
    -1
      cmd/esphome-grab/main.go
  4. +4
    -0
      discover.go
  5. +32
    -2
      entity.go

+ 7
- 0
client.go View File

@ -12,12 +12,14 @@ import (
"maze.io/x/esphome/api"
)
// Client defaults.
const (
DefaultTimeout = 10 * time.Second
DefaultPort = 6053
defaultClientInfo = "maze.io go/esphome"
)
// Client for an ESPHome device.
type Client struct {
// Info identifies this device with the ESPHome node.
Info string
@ -69,6 +71,7 @@ func Dial(addr string) (*Client, error) {
return DialTimeout(addr, 0)
}
// DialTimeout is like Dial with a custom timeout.
func DialTimeout(addr string, timeout time.Duration) (*Client, error) {
conn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
@ -326,6 +329,7 @@ func (c *Client) Login(password string) error {
return nil
}
// Close the device connection.
func (c *Client) Close() error {
_, err := c.sendAndWaitResponseTimeout(&api.DisconnectRequest{}, api.DisconnectResponseType, 5*time.Second)
select {
@ -378,6 +382,7 @@ func (c *Client) DeviceInfo() (DeviceInfo, error) {
}, nil
}
// Entities returns all configured entities on the connected device.
func (c *Client) Entities() Entities {
var entities = Entities{
BinarySensor: make(map[string]BinarySensor),
@ -420,8 +425,10 @@ func (c *Client) Entities() Entities {
return entities
}
// LogLevel represents the logger level.
type LogLevel int32
// Log levels.
const (
LogNone LogLevel = iota
LogError


+ 3
- 2
cmd/client.go View File

@ -4,6 +4,7 @@ import (
"flag"
"net"
"os"
"strconv"
"maze.io/x/esphome"
)
@ -15,13 +16,13 @@ const (
var (
NodeFlag = flag.String("node", getenv(envHost, "esphome.local"), "node API hostname or IP ("+envHost+")")
PortFlag = flag.String("port", esphome.DefaultPort, "node API port")
PortFlag = flag.Int("port", esphome.DefaultPort, "node API port")
PasswordFlag = flag.String("password", "", "node API password ("+envPassword+")")
TimeoutFlag = flag.Duration("timeout", esphome.DefaultTimeout, "network timeout")
)
func Dial() (*esphome.Client, error) {
addr := net.JoinHostPort(*NodeFlag, *PortFlag)
addr := net.JoinHostPort(*NodeFlag, strconv.Itoa(*PortFlag))
client, err := esphome.DialTimeout(addr, *TimeoutFlag)
if err != nil {
return nil, err


+ 1
- 1
cmd/esphome-grab/main.go View File

@ -16,7 +16,7 @@ func main() {
var output = flag.String("output", "camera.jpg", "output file")
flag.Parse()
log.Printf("connecting to node %s:%s", *cmd.NodeFlag, *cmd.PortFlag)
log.Printf("connecting to node %s:%d", *cmd.NodeFlag, *cmd.PortFlag)
client, err := cmd.Dial()
if err != nil {
log.Fatalln(err)


+ 4
- 0
discover.go View File

@ -14,6 +14,7 @@ import (
"github.com/miekg/dns"
)
// Discovery defaults.
const (
DefaultMDNSService = "_esphomelib._tcp"
DefaultMDNSDomain = "local"
@ -30,6 +31,7 @@ var (
mDNSAddr6 = &net.UDPAddr{IP: net.ParseIP(mDNSIP6), Port: mDNSPort}
)
// Device is an ESPHome device (returned by Discover).
type Device struct {
Name string
Host string
@ -55,10 +57,12 @@ func (d *Device) complete() bool {
return d.Host != "" && (d.IP != nil || d.IP6 != nil)
}
// Discover ESPHome deices on the network.
func Discover(devices chan<- *Device) error {
return DiscoverService(devices, DefaultMDNSService, DefaultMDNSDomain, DefaultMDNSTimeout)
}
// DiscoverService is used by Discover, can be used to override the default service and domain and to customize timeouts.
func DiscoverService(devices chan<- *Device, service, domain string, timeout time.Duration) error {
c, err := newmDNSClient()
if err != nil {


+ 32
- 2
entity.go View File

@ -7,6 +7,7 @@ import (
"maze.io/x/esphome/api"
)
// Entity is the base struct for all supported entities.
type Entity struct {
Name string
ObjectID string
@ -28,6 +29,7 @@ type Entities struct {
TextSensor map[string]TextSensor
}
// BinarySensor can be pressed, released and/or clicked.
type BinarySensor struct {
Entity
DeviceClass string
@ -47,13 +49,17 @@ func newBinarySensor(client *Client, entity *api.ListEntitiesBinarySensorRespons
}
}
// Climate devices can represent different types of hardware, but the defining factor is that climate devices have a
// settable target temperature and can be put in different modes like HEAT, COOL, AUTO or OFF.
type Climate struct {
Entity
// Capabilities of the entity.
Capabilities ClimateCapabilities
}
type (
// ClimateCapabilities represents the capabilities of a climate device.
ClimateCapabilities struct {
CurrentTemperature bool
TwoPointTargetTemperature bool
@ -66,11 +72,18 @@ type (
FanModes []ClimateFanMode
SwingModes []ClimateSwingMode
}
ClimateMode int32
ClimateFanMode int32
// ClimateMode represents the mode for a climate device.
ClimateMode int32
// ClimateFanMode represents a climate fan speed.
ClimateFanMode int32
// ClimateSwingMode represents a climate (fan) swing mode.
ClimateSwingMode int32
)
// Climate modes.
const (
ClimateModeOff ClimateMode = iota
ClimateModeAuto
@ -80,6 +93,7 @@ const (
ClimateModeDry
)
// Climate fan modes.
const (
ClimateFanModeOn ClimateFanMode = iota
ClimateFanModeOff
@ -92,6 +106,7 @@ const (
ClimateFanModeDiffuse
)
// Climate swing modes.
const (
ClimateSwingModeOff ClimateSwingMode = iota
ClimateSwingModeBoth
@ -137,6 +152,7 @@ func newClimate(client *Client, entity *api.ListEntitiesClimateResponse) *Climat
}
}
// Cover device.
type Cover struct {
Entity
@ -155,6 +171,7 @@ func newCover(client *Client, entity *api.ListEntitiesCoverResponse) *Cover {
}
}
// Fan device.
type Fan struct {
Entity
@ -173,6 +190,7 @@ func newFan(client *Client, entity *api.ListEntitiesFanResponse) *Fan {
}
}
// Light device.
type Light struct {
Entity
@ -189,6 +207,7 @@ type Light struct {
HandleEffect func(string)
}
// LightCapabilities represents the capabilities of a Light.
type LightCapabilities struct {
Brightness bool
RGB bool
@ -198,6 +217,7 @@ type LightCapabilities struct {
MaxMired float32
}
// LightState represents the state of a Light.
type LightState struct {
On bool
Brightness float32
@ -290,12 +310,14 @@ func (entity *Light) update(state *api.LightStateResponse) {
entity.StateIsValid = true
}
// SetBrightness sets the light's intensity (brightness).
func (entity Light) SetBrightness(value float32) error {
request := entity.commandRequest()
request.Brightness = value
return entity.client.sendTimeout(request, entity.client.Timeout)
}
// SetColor sets the light's red, green and blue values.
func (entity Light) SetColor(value color.Color) error {
r, g, b, _ := value.RGBA()
request := entity.commandRequest()
@ -305,24 +327,28 @@ func (entity Light) SetColor(value color.Color) error {
return entity.client.sendTimeout(request, entity.client.Timeout)
}
// SetWhite sets the light's white value.
func (entity Light) SetWhite(value float32) error {
request := entity.commandRequest()
request.White = value
return entity.client.sendTimeout(request, entity.client.Timeout)
}
// SetEffect selects a preconfigured effect.
func (entity Light) SetEffect(effect string) error {
request := entity.commandRequest()
request.Effect = effect
return entity.client.sendTimeout(request, entity.client.Timeout)
}
// SetState turns the light on or off.
func (entity Light) SetState(on bool) error {
request := entity.commandRequest()
request.State = on
return entity.client.sendTimeout(request, entity.client.Timeout)
}
// Sensor probes.
type Sensor struct {
Entity
Icon string
@ -357,6 +383,7 @@ func (entity *Sensor) update(state *api.SensorStateResponse) {
entity.StateIsValid = !state.MissingState
}
// Switch includes all platforms that should show up like a switch and can only be turned ON or OFF.
type Switch struct {
Entity
Icon string
@ -396,12 +423,15 @@ func (entity Switch) commandRequest() *api.SwitchCommandRequest {
}
}
// SetState updates the switch state.
func (entity Switch) SetState(on bool) error {
request := entity.commandRequest()
request.State = on
return entity.client.sendTimeout(request, entity.client.Timeout)
}
// TextSensor is a lot like Sensor, but where the “normal” sensors only represent sensors that output numbers, this
// component can represent any text.
type TextSensor struct {
Entity
State string


Loading…
Cancel
Save