Skip to content

Commit 5003ada

Browse files
committed
📝 Figure out state of group every time a single device in a group is updated
1 parent e2f549d commit 5003ada

File tree

1 file changed

+80
-31
lines changed

1 file changed

+80
-31
lines changed

device.go

+80-31
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/hemtjanst/sladdlos/tradfri"
88
"github.com/lucasb-eyer/go-colorful"
99
"log"
10+
"math"
1011
"strconv"
1112
"strings"
1213
"sync"
@@ -311,63 +312,111 @@ func (h *HemtjanstDevice) lightSetting() *tradfri.LightSetting {
311312
return &l.LightSetting
312313
}
313314

314-
func (h *HemtjanstDevice) publish(feature string) error {
315+
func (h *HemtjanstDevice) featureVal(feature string) (string, error) {
316+
if h.isGroup {
317+
min := math.MaxInt64
318+
max := math.MinInt64
319+
var last string
320+
321+
for _, m := range h.members {
322+
val, err := m.featureVal(feature)
323+
if err != nil {
324+
continue
325+
}
326+
if val != "" {
327+
last = val
328+
}
329+
ival, err := strconv.Atoi(val)
330+
if err != nil {
331+
continue
332+
}
333+
if ival < min {
334+
min = ival
335+
}
336+
if ival > max {
337+
max = ival
338+
}
339+
}
340+
341+
switch feature {
342+
case "on":
343+
if max == 1 {
344+
return "1", nil
345+
}
346+
return "0", nil
347+
case "brightness":
348+
if max != math.MinInt64 {
349+
return strconv.Itoa(max), nil
350+
}
351+
return "0", nil
352+
case "colorTemperature":
353+
return last, nil
354+
case "reachable":
355+
if min == 0 {
356+
return "0", nil
357+
}
358+
return "1", nil
359+
}
360+
}
315361
switch feature {
316362
case "on":
317363
dim := h.dimmable()
318364
if dim == nil {
319-
return fmt.Errorf("Device doesn't support %s", feature)
365+
return "", fmt.Errorf("Device doesn't support %s", feature)
320366
}
321-
val := "0"
322367
if dim.IsOn() {
323-
val = "1"
368+
return "1", nil
324369
}
325-
if ft, err := h.device.GetFeature("on"); err == nil && ft != nil {
326-
return ft.Update(val)
327-
}
328-
return fmt.Errorf("Feature %s not found", feature)
370+
return "0", nil
329371
case "brightness":
330372
dim := h.dimmable()
331373
if dim == nil {
332-
return fmt.Errorf("Device doesn't support %s", feature)
374+
return "", fmt.Errorf("Device doesn't support %s", feature)
333375
}
334-
newDim := dim.DimInt()
335-
if ft, err := h.device.GetFeature("brightness"); err == nil && ft != nil {
336-
return ft.Update(strconv.Itoa(newDim))
337-
}
338-
return fmt.Errorf("Feature %s not found", feature)
376+
return strconv.Itoa(dim.DimInt()), nil
339377
case "colorTemperature":
340378
ls := h.lightSetting()
341379
if ls == nil || !ls.HasColorTemperature() {
342-
return fmt.Errorf("Device doesn't support %s", feature)
380+
return "", fmt.Errorf("Device doesn't support %s", feature)
343381
}
344-
newVal := ""
345382
switch ls.GetColorName() {
346383
case "cold":
347-
newVal = "111"
348-
case "normal":
349-
newVal = "222"
384+
return "111", nil
350385
case "warm":
351-
newVal = "400"
386+
return "400", nil
387+
default:
388+
return "222", nil
352389
}
353-
if ft, err := h.device.GetFeature("colorTemperature"); err == nil && ft != nil {
354-
return ft.Update(newVal)
355-
}
356-
return fmt.Errorf("Feature %s not found", feature)
357390
case "reachable":
358391
if h.isGroup || h.accessory == nil {
359-
return fmt.Errorf("Device doesn't support %s", feature)
392+
return "", fmt.Errorf("Device doesn't support %s", feature)
360393
}
361-
val := "0"
362394
if h.accessory.IsAlive() {
363-
val = "1"
364-
}
365-
if ft, err := h.device.GetFeature("reachable"); err == nil && ft != nil {
366-
return ft.Update(val)
395+
return "1", nil
367396
}
397+
return "0", nil
398+
}
399+
return "", fmt.Errorf("Device doesn't support %s", feature)
400+
}
401+
402+
func (h *HemtjanstDevice) publish(feature string) error {
403+
var ft *device.Feature
404+
var err error
405+
406+
if !h.isGroup && len(h.members) == 1 {
407+
h.members[0].publish(feature)
408+
}
409+
410+
if ft, err = h.device.GetFeature(feature); err != nil || ft == nil {
368411
return fmt.Errorf("Feature %s not found", feature)
369412
}
370-
return fmt.Errorf("Device doesn't support %s", feature)
413+
414+
newVal, err := h.featureVal(feature)
415+
if err != nil {
416+
return err
417+
}
418+
ft.Update(newVal)
419+
return nil
371420
}
372421

373422
func (h *HemtjanstDevice) onTradfriChange(change []*tradfri.ObservedChange) {

0 commit comments

Comments
 (0)