mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Fix Shelly Plus H&T sleep period on external power state change (#81121)
This commit is contained in:
parent
bcae6d604e
commit
5cf4483df5
@ -41,7 +41,12 @@ from .const import (
|
|||||||
SLEEP_PERIOD_MULTIPLIER,
|
SLEEP_PERIOD_MULTIPLIER,
|
||||||
UPDATE_PERIOD_MULTIPLIER,
|
UPDATE_PERIOD_MULTIPLIER,
|
||||||
)
|
)
|
||||||
from .utils import device_update_info, get_block_device_name, get_rpc_device_name
|
from .utils import (
|
||||||
|
device_update_info,
|
||||||
|
get_block_device_name,
|
||||||
|
get_rpc_device_name,
|
||||||
|
get_rpc_device_wakeup_period,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -355,6 +360,24 @@ class ShellyRpcCoordinator(DataUpdateCoordinator):
|
|||||||
LOGGER.debug("Reloading entry %s", self.name)
|
LOGGER.debug("Reloading entry %s", self.name)
|
||||||
await self.hass.config_entries.async_reload(self.entry.entry_id)
|
await self.hass.config_entries.async_reload(self.entry.entry_id)
|
||||||
|
|
||||||
|
def update_sleep_period(self) -> bool:
|
||||||
|
"""Check device sleep period & update if changed."""
|
||||||
|
if (
|
||||||
|
not self.device.initialized
|
||||||
|
or not (wakeup_period := get_rpc_device_wakeup_period(self.device.status))
|
||||||
|
or wakeup_period == self.entry.data.get(CONF_SLEEP_PERIOD)
|
||||||
|
):
|
||||||
|
return False
|
||||||
|
|
||||||
|
data = {**self.entry.data}
|
||||||
|
data[CONF_SLEEP_PERIOD] = wakeup_period
|
||||||
|
self.hass.config_entries.async_update_entry(self.entry, data=data)
|
||||||
|
|
||||||
|
update_interval = SLEEP_PERIOD_MULTIPLIER * wakeup_period
|
||||||
|
self.update_interval = timedelta(seconds=update_interval)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_device_updates_handler(self) -> None:
|
def _async_device_updates_handler(self) -> None:
|
||||||
"""Handle device updates."""
|
"""Handle device updates."""
|
||||||
@ -365,6 +388,8 @@ class ShellyRpcCoordinator(DataUpdateCoordinator):
|
|||||||
):
|
):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
self.update_sleep_period()
|
||||||
|
|
||||||
self._last_event = self.device.event
|
self._last_event = self.device.event
|
||||||
|
|
||||||
for event in self.device.event["events"]:
|
for event in self.device.event["events"]:
|
||||||
@ -393,6 +418,9 @@ class ShellyRpcCoordinator(DataUpdateCoordinator):
|
|||||||
|
|
||||||
async def _async_update_data(self) -> None:
|
async def _async_update_data(self) -> None:
|
||||||
"""Fetch data."""
|
"""Fetch data."""
|
||||||
|
if self.update_sleep_period():
|
||||||
|
return
|
||||||
|
|
||||||
if sleep_period := self.entry.data.get(CONF_SLEEP_PERIOD):
|
if sleep_period := self.entry.data.get(CONF_SLEEP_PERIOD):
|
||||||
# Sleeping device, no point polling it, just mark it unavailable
|
# Sleeping device, no point polling it, just mark it unavailable
|
||||||
raise UpdateFailed(
|
raise UpdateFailed(
|
||||||
|
@ -272,6 +272,11 @@ def get_rpc_device_sleep_period(config: dict[str, Any]) -> int:
|
|||||||
return cast(int, config["sys"].get("sleep", {}).get("wakeup_period", 0))
|
return cast(int, config["sys"].get("sleep", {}).get("wakeup_period", 0))
|
||||||
|
|
||||||
|
|
||||||
|
def get_rpc_device_wakeup_period(status: dict[str, Any]) -> int:
|
||||||
|
"""Return the device wakeup period in seconds or 0 for non sleeping devices."""
|
||||||
|
return cast(int, status["sys"].get("wakeup_period", 0))
|
||||||
|
|
||||||
|
|
||||||
def get_info_auth(info: dict[str, Any]) -> bool:
|
def get_info_auth(info: dict[str, Any]) -> bool:
|
||||||
"""Return true if device has authorization enabled."""
|
"""Return true if device has authorization enabled."""
|
||||||
return cast(bool, info.get("auth") or info.get("auth_en"))
|
return cast(bool, info.get("auth") or info.get("auth_en"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user