|
7 | 7 | "github.com/hemtjanst/sladdlos/tradfri"
|
8 | 8 | "github.com/lucasb-eyer/go-colorful"
|
9 | 9 | "log"
|
| 10 | + "math" |
10 | 11 | "strconv"
|
11 | 12 | "strings"
|
12 | 13 | "sync"
|
@@ -311,63 +312,111 @@ func (h *HemtjanstDevice) lightSetting() *tradfri.LightSetting {
|
311 | 312 | return &l.LightSetting
|
312 | 313 | }
|
313 | 314 |
|
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 | + } |
315 | 361 | switch feature {
|
316 | 362 | case "on":
|
317 | 363 | dim := h.dimmable()
|
318 | 364 | if dim == nil {
|
319 |
| - return fmt.Errorf("Device doesn't support %s", feature) |
| 365 | + return "", fmt.Errorf("Device doesn't support %s", feature) |
320 | 366 | }
|
321 |
| - val := "0" |
322 | 367 | if dim.IsOn() {
|
323 |
| - val = "1" |
| 368 | + return "1", nil |
324 | 369 | }
|
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 |
329 | 371 | case "brightness":
|
330 | 372 | dim := h.dimmable()
|
331 | 373 | if dim == nil {
|
332 |
| - return fmt.Errorf("Device doesn't support %s", feature) |
| 374 | + return "", fmt.Errorf("Device doesn't support %s", feature) |
333 | 375 | }
|
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 |
339 | 377 | case "colorTemperature":
|
340 | 378 | ls := h.lightSetting()
|
341 | 379 | 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) |
343 | 381 | }
|
344 |
| - newVal := "" |
345 | 382 | switch ls.GetColorName() {
|
346 | 383 | case "cold":
|
347 |
| - newVal = "111" |
348 |
| - case "normal": |
349 |
| - newVal = "222" |
| 384 | + return "111", nil |
350 | 385 | case "warm":
|
351 |
| - newVal = "400" |
| 386 | + return "400", nil |
| 387 | + default: |
| 388 | + return "222", nil |
352 | 389 | }
|
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) |
357 | 390 | case "reachable":
|
358 | 391 | 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) |
360 | 393 | }
|
361 |
| - val := "0" |
362 | 394 | 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 |
367 | 396 | }
|
| 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 { |
368 | 411 | return fmt.Errorf("Feature %s not found", feature)
|
369 | 412 | }
|
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 |
371 | 420 | }
|
372 | 421 |
|
373 | 422 | func (h *HemtjanstDevice) onTradfriChange(change []*tradfri.ObservedChange) {
|
|
0 commit comments