Skip to content
This repository has been archived by the owner on Mar 3, 2024. It is now read-only.

Commit

Permalink
Drop toggle support
Browse files Browse the repository at this point in the history
  • Loading branch information
zachfi committed May 5, 2023
1 parent 095b0f9 commit 0d5ad48
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 35 deletions.
1 change: 0 additions & 1 deletion modules/lights/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ type Handler interface {
SetBrightness(context.Context, string, int32) error
SetColor(context.Context, string, string) error
SetColorTemp(context.Context, string, int32) error
Toggle(context.Context, string) error
}
2 changes: 1 addition & 1 deletion modules/lights/lights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func TestActionHandler(t *testing.T) {
Zone: "zone",
},
mock: &MockLight{
ToggleCalls: map[string]int{"zone": 1},
OffCalls: map[string]int{"zone": 1},
},
},
"double": {
Expand Down
9 changes: 0 additions & 9 deletions modules/lights/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ type MockLight struct {
SetBrightnessCalls map[string]int
SetColorCalls map[string]int
SetColorTempCalls map[string]int
ToggleCalls map[string]int
}

func (m *MockLight) Alert(ctx context.Context, groupName string) error {
Expand Down Expand Up @@ -61,14 +60,6 @@ func (m *MockLight) SetColor(ctx context.Context, groupName string, hex string)
return nil
}

func (m *MockLight) Toggle(ctx context.Context, groupName string) error {
if len(m.ToggleCalls) == 0 {
m.ToggleCalls = make(map[string]int)
}
m.ToggleCalls[groupName]++
return nil
}

func (m *MockLight) SetColorTemp(ctx context.Context, groupName string, temp int32) error {
if len(m.SetColorTempCalls) == 0 {
m.SetColorTempCalls = make(map[string]int)
Expand Down
5 changes: 0 additions & 5 deletions modules/lights/zigbee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ func TestZigbeeLight_New(t *testing.T) {
require.NoError(t, l.RandomColor(ctx, "group1", []string{"#006c7f"}))
require.Equal(t, "zigbee2mqtt/testdevice1/set", mqttClient.LastPublishTopic)
require.Equal(t, `{"color":{"hex":"#006c7f"},"transition":0.5}`, mqttClient.LastPublishPayload)

require.NoError(t, l.Toggle(ctx, "group1"))
require.Equal(t, "zigbee2mqtt/testdevice1/set", mqttClient.LastPublishTopic)
require.Equal(t, `{"state":"TOGGLE","transition":0.5}`, mqttClient.LastPublishPayload)

}

func TestIsLightDevice(t *testing.T) {
Expand Down
36 changes: 25 additions & 11 deletions modules/lights/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
)

func NewZone(name string) *Zone {
z := &Zone{}
z.lock = new(sync.Mutex)
z.SetName(name)
// default
z.colorTemp = dayTemp
z := &Zone{
lock: new(sync.Mutex),
colorTemp: dayTemp,
brightnessMap: defaultBrightnessMap,
colorTempMap: defaultColorTemperatureMap,
}

z.brightnessMap = defaultBrightnessMap
z.colorTempMap = defaultColorTemperatureMap
z.SetName(name)

return z
}
Expand Down Expand Up @@ -112,11 +112,25 @@ func (z *Zone) On(ctx context.Context) error {
}

func (z *Zone) Toggle(ctx context.Context) error {
for _, h := range z.handlers {
err := h.Toggle(ctx, z.Name())
if err != nil {
return fmt.Errorf("%s toggle: %w", z.name, ErrHandlerFailed)

switch z.state {
case ZoneState_ON:
for _, h := range z.handlers {
err := h.Off(ctx, z.Name())
if err != nil {
return fmt.Errorf("%s Off: %w", z.name, ErrHandlerFailed)
}
}

case ZoneState_OFF:
for _, h := range z.handlers {
err := h.On(ctx, z.Name())
if err != nil {
return fmt.Errorf("%s On: %w", z.name, ErrHandlerFailed)
}
}
default:
return fmt.Errorf("unhandled toggle from current state: %s", z.state)
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions modules/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,27 +625,27 @@ func (l *Telemetry) updateZigbeeMessageMetrics(m iot.ZigbeeMessage, request *inv
component := request.DeviceDiscovery.Component

if m.Battery != nil {
telemetryIOTBatteryPercent.WithLabelValues(deviceName, component, zone).Set(float64(*m.Battery))
telemetryIOTBatteryPercent.WithLabelValues(deviceName, component, zone).Set(*m.Battery)
}

if m.LinkQuality != nil {
telemetryIOTLinkQuality.WithLabelValues(deviceName, component, zone).Set(float64(*m.LinkQuality))
}

if m.Temperature != nil {
telemetryIOTTemperature.WithLabelValues(deviceName, component, zone).Set(float64(*m.Temperature))
telemetryIOTTemperature.WithLabelValues(deviceName, component, zone).Set(*m.Temperature)
}

if m.Humidity != nil {
telemetryIOTHumidity.WithLabelValues(deviceName, component, zone).Set(float64(*m.Humidity))
telemetryIOTHumidity.WithLabelValues(deviceName, component, zone).Set(*m.Humidity)
}

if m.Co2 != nil {
telemetryIOTCo2.WithLabelValues(deviceName, component, zone).Set(float64(*m.Co2))
telemetryIOTCo2.WithLabelValues(deviceName, component, zone).Set(*m.Co2)
}

if m.Formaldehyde != nil {
telemetryIOTFormaldehyde.WithLabelValues(deviceName, component, zone).Set(float64(*m.Formaldehyde))
telemetryIOTFormaldehyde.WithLabelValues(deviceName, component, zone).Set(*m.Formaldehyde)
}

if m.VOC != nil {
Expand Down
2 changes: 1 addition & 1 deletion modules/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestReportIOTDevice_lights_handling(t *testing.T) {
},
Zone: "dungeon",
Handler: &lights.MockLight{
ToggleCalls: map[string]int{"dungeon": 1},
OffCalls: map[string]int{"dungeon": 1},
},
},
"hold": {
Expand Down
4 changes: 2 additions & 2 deletions pkg/iot/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (

type ZigbeeMessage struct {
Action *string `json:"action,omitempty"`
Battery *float32 `json:"battery,omitempty"`
Battery *float64 `json:"battery,omitempty"`
Illuminance *int `json:"illuminance,omitempty"`
LinkQuality *int `json:"linkquality,omitempty"`
Occupancy *bool `json:"occupancy,omitempty"`
Tamper *bool `json:"tamper,omitempty"`
Temperature *float32 `json:"temperature,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
Humidity *float64 `json:"humidity,omitempty"`
Voltage *int `json:"voltage,omitempty"`
WaterLeak *bool `json:"water_leak,omitempty"`
Expand Down

0 comments on commit 0d5ad48

Please sign in to comment.