Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fra-orolo
Copy link

This is a first part to keep deep-sleep sensors alive.
Seems to work for me, need to figure out timers...

@@ -182,6 +182,7 @@
# Power units
POWER_WATT: Final = "W"
REACTIVE_POWER = "VAr"
POWER_FACTOR: Final = "Cos φ"
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

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

Comment on lines +140 to +154
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
Copy link
Owner

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)
Copy link
Owner

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:

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()

@emontnemery
Copy link
Owner

emontnemery commented Nov 11, 2023

@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
Note that the PR description is not correct, setting LWT message to DeepSleep was reverted.

If you want, you can contact me, @emontnemery, on discord to discuss the details.

@stefanbode
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants