hellqvio86 / home_assistant_casambi

Home assistant Integration for Casambi Cloud lights
MIT License
25 stars 9 forks source link

Wake up offline units #10

Closed robsonke closed 3 years ago

robsonke commented 3 years ago

First of all, I'm really happy with the latest releases, all works pretty stable!

My question might not be related to your code but I'll give it a shot. I have one outdoor light which is a bit further away, usually in range of bluetooth but it looses connection now and then. The api reports the device as offline and that makes it unavailable in Home Assistant obviously.

Once I press the light in the Casambi app, it immediately turns on and the status is pushed through the websocket to Home Assistant as well and the unit is back online. It feels like once the unit went offline in Casambi, it can be awaken somehow by Casambi but that needs to be done before Home Assistant realises it too. Is this something we can periodically force from the library maybe.

hellqvio86 commented 3 years ago

The underlying casambi library now supports getting network state, they didn't support it when I started the project but I have added support for it.

But that might trigger the unit to come back online, I don't know and I don't have the same issue in my Casambi network, so I don't know if I can produce it, but it's not that hard to test it out, a loop with the call and a sleep. If that works can we add pull to the plugin.

Otherwise the other way of doing it is to disable the "disable entity" functionality in the plugin.

Can you test if get_unit_state, get_network_state, or another function triggers a state to get sent by the websocket?

robsonke commented 3 years ago

Makes sense, to be sure, in case a unit if offline, I do an api call to https://door.casambi.com/v1/networks/123/units/123/state Right?

hellqvio86 commented 3 years ago

Yepp and get_network_state (network/{nerwork_id}/state), I guess that's the same thing, but more efficient in the pulling of the state since its just one call, if this triggers the device to wake up that is.

robsonke commented 3 years ago

Tried it but it didn't work. Both state calls we're saying "offline" for this unit. The logs in HA remained empty.

I tried to turn on the light in the Casambi app and immediately it came back online in HA and the logs said: 2021-01-29 12:29:50 DEBUG (MainThread) [custom_components.casambi.light] signalling_callback signal: data data: {'blabla-20': <Unit Buitenlamp zijdeur: unit_id=20 address=3151edb83f80 value=1.0 state=on online=True network_id=bla wire_id=9 >} 2021-01-29 12:29:50 DEBUG (MainThread) [custom_components.casambi.light] process_update self: <Casambi light Buitenlamp zijdeur: unit=<Unit Buitenlamp zijdeur: unit_id=20 address=3151edb83f80 value=1.0 state=on online=True network_id=bla wire_id=9 > data: <Unit Buitenlamp zijdeur: unit_id=20 address=3151edb83f80 value=1.0 state=on online=True network_id=bla wire_id=9 > 2021-01-29 12:29:50 DEBUG (MainThread) [custom_components.casambi.light] async_update <Casambi light Buitenlamp zijdeur: unit=<Unit Buitenlamp zijdeur: unit_id=20 address=3151edb83f80 value=1.0 state=on online=True network_id=bla wire_id=9 >

hellqvio86 commented 3 years ago

I have implemented pulling of get_network_state in a separate branch: https://github.com/hellqvio86/home_assistant_casambi/tree/pull

I am testing out stability currently, but this will probably not help, its the same thing you did in your test but integrated into the plugin.

They are probably probing the light via the Bluetooth protocol.

robsonke commented 3 years ago

I thought the same thing, they better add a call in the API to do ask the gateway to do the same probing....

hellqvio86 commented 3 years ago

Yep best way forward it to open a feature request, they are listening, getting the unit state is a fairly new feature in the REST portion of the api

robsonke commented 3 years ago

Agree, done that!

hellqvio86 commented 3 years ago

I update the lights state now with a periodic pull every minute, its in the release v0.20, I had to change the callback mechanism a bit and that change is needed for this as well since the new state is probably not sent over websocket, it will probably be over REST pull.

robsonke commented 3 years ago

Thanks, I'll give this a shot too.

robsonke commented 3 years ago

Casambi support didn't come up with a very satisfying answer. They don't have a poll / probe option, they say that the units aren't reachable enough.

I was thinking about alternatives. Can't we detect unavailable lights every x seconds and then turn the light on/off very shortly with a brightness of 0? Not very beautiful but might be effective. And it shouldn't be happening by default imo, enabling with a config param or so.

hellqvio86 commented 3 years ago

Thinking about something similar, but the easiest might be to set it to the last know state brightness and just continue to do that until the unit is online again

robsonke commented 3 years ago

If that's a possibility, then yes. Since I already wondered if a unit can become unavailable while turned on, I haven't seen that yet but I assume there's a fair chance.

hellqvio86 commented 3 years ago

I have released a new version (v0.27), this version will send the same state to every light that are marked as offline

robsonke commented 3 years ago

Great! Testing testing :)

robsonke commented 3 years ago

Bug maybe:

File "/usr/local/lib/python3.8/site-packages/aiocasambi/controller.py", line 242, in wake_up_units
    await self.units.wake_up_offline_units
TypeError: object method can't be used in 'await' expression
hellqvio86 commented 3 years ago

Its a typo in the library, should now be fixed (version 0.28 is the latest).

robsonke commented 3 years ago

Thanks, error is gone but the problem isn't fixed yet. At what times is it trying to trigger it and can I see that somewhere if it does?

hellqvio86 commented 3 years ago

It's a part of the polling of the casambi api, you can control the sac interval through _scaninterval in the config, the default is 60 seconds, don't set to low that might lead to complaint from casambi support 😉

hellqvio86 commented 3 years ago

You can search for the string "Trying to wake up unit:", aiocasambi units will generate that string if it identifies an unit that isn't online

robsonke commented 3 years ago

I'll leave the scan interval on 60, no worries haha. But I'm not able to find those logs. While I do see other error and debug logs, for example:

021-02-17 10:33:11 ERROR (MainThread) [custom_components.casambi.light] unit is not online: <Casambi light Buitenlamp zijdeur: unit=<Unit Buitenlamp zijdeur: unit_id=20 address=3151edb83f80 value=0.0 state=off online=False network_id=J77iLoNeXMDOd541lyHqqv28rQlTA2bT wire_id=9 >
2021-02-17 10:33:11 DEBUG (MainThread) [custom_components.casambi.light] async_update <Casambi light Zolder: unit=<Unit Zolder: unit_id=29 address=b7a383cd1d96 value=0.0 state=off online=True network_id=J77iLoNeXMDOd541lyHqqv28rQlTA2bT wire_id=9 >
hellqvio86 commented 3 years ago

I think you might have missed to enable logging for aiocasambi, this is my config:

logger:
  default: info
  logs:
    homeassistant.components.casambi: debug
    custom_components.casambi: debug
    aiocasambi: debug
    aiocasambi.controller: debug
    aiocasambi.websocket: debug
    aiocasambi.scenes: debug
    aiocasambi.units: debug
    ...

But this is probably enough:

logger:
  default: info
  logs:
    homeassistant.components.casambi: debug
    custom_components.casambi: debug
    aiocasambi: debug
    ...
robsonke commented 3 years ago

Ahh yes, missed the third one. It doesn't seem to kick the unit back online though

hellqvio86 commented 3 years ago

Too bad, I don't think we can wake up the device then by the Cloud API.

robsonke commented 3 years ago

My assumption is that the api doesn't push something for a unit to the gateway that is marked as offline

hellqvio86 commented 3 years ago

Most likely, they are releasing ethernet products: https://www.aimotion-smartliving.de/en/produkt/casambi-ethernet-gateway-en/

https://casambi.com/ecosystem/x-moment/

Hopefully that means better integration, local api and more feature rich cloud api.

I will probably not invest more in casambi, esphome, tasmota and zigbee is more appealing for me (currently) 🙂

hellqvio86 commented 3 years ago

@robsonke Casambi is looking for feedback for the API, I voiced my opinion regarding this, hope you do the same so they implement support for waking up units :)

robsonke commented 3 years ago

Yes, I got the same email and am going to highlight the difference in power between api and app/gateway :)

hellqvio86 commented 3 years ago

Closing this issue so it doesn't linger, hopefully Casambi changes their minds so wake up can be implemented in this plugin.