-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#263 observe deep sleep and keep sensors always available #264
base: master
Are you sure you want to change the base?
Conversation
@@ -182,6 +182,7 @@ | |||
# Power units | |||
POWER_WATT: Final = "W" | |||
REACTIVE_POWER = "VAr" | |||
POWER_FACTOR: Final = "Cos φ" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems unrelated to this PR?
@@ -98,27 +104,65 @@ def __init__(self, **kwds: Any): | |||
self._on_availability_callback: Callable[ | |||
[bool], Coroutine[Any, Any, None] | |||
] | None = None | |||
self.uses_deep_sleep = False | |||
self.deep_sleep_interval = None | |||
self.last_up=0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please give this a better name, maybe last_up_timestamp
, timestamp_last_up
or something like that
super().__init__(**kwds) | ||
|
||
def get_availability_topics(self) -> dict: | ||
"""Return MQTT topics to subscribe to for availability state.""" | ||
|
||
async def availability_message_received(msg: ReceiveMessage) -> None: | ||
"""Handle a new received MQTT availability message.""" | ||
last_up_retain=self.last_up |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This variable is never used
async def sleep_state_message_received(msg: ReceiveMessage) -> None: | ||
"""Handle state messages to indicate deep sleep.""" | ||
#try: | ||
# payload = json.loads(msg.payload) | ||
#except json.decoder.JSONDecodeError: | ||
# return | ||
_LOGGER.debug("sleep state %s -> %s", msg.topic, msg.payload) | ||
state = get_value_by_path(msg.payload, ["StatusPRM","RestartReason"]) | ||
if state is not None: | ||
state=str(state).lower() | ||
if state.startswith('deep sleep'): | ||
if not(self.uses_deep_sleep): | ||
_LOGGER.debug("switching to deep sleep mode %s", msg.topic) | ||
self.deep_sleep_interval=None | ||
self.uses_deep_sleep=True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of detecting a device's support for deep sleep when it wakes up from deep sleep, we should detect it from the device's discovery message: https://github.com/arendst/Tasmota/pull/19134/files#diff-fe970b21f48758677cb799d64f0ac866cbbe0aa48eb3e0f6aaa52c22ef73c735R189
Also
if not(self.uses_deep_sleep): | ||
available=False | ||
else: | ||
_LOGGER.debug("inhibit deep sleep %s", msg.topic) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we start a timer here, which if it expires sets availability to False
and calls the availability callback?
You can use this timer class:
Lines 17 to 33 in 8a19e8d
class Timer: | |
"""Simple timer.""" | |
def __init__( | |
self, timeout: float, callback: Callable[[], Coroutine[Any, Any, None]] | |
): | |
self._timeout = timeout | |
self._callback = callback | |
self._task = asyncio.ensure_future(self._job()) | |
async def _job(self) -> None: | |
await asyncio.sleep(self._timeout) | |
await self._callback() | |
def cancel(self) -> None: | |
"""Cancel the timer.""" | |
self._task.cancel() |
@fra-orolo This is for sure wanted functionality, thanks for the PR! @stefanbode also started working on this functionality, he created this PR on tasmota: arendst/Tasmota#19134 If you want, you can contact me, @emontnemery, on discord to discuss the details. |
Yes, but we can see battery operated in telemetry and deepsleep. The on/off detection in hatasmoto has to be changed. This was more complicated than expected. |
This is a first part to keep deep-sleep sensors alive.
Seems to work for me, need to figure out timers...