Refresh device_info for Shelly devices (#62899)

* Rework device_info

* Update device_info via registry

* Optimized checks: only when needed

* Revert device_info rework as not needed

* Fix reboot detection for all scenarios

* Final optimization

* Remove leftover

* Bump aioshelly

* Bump fritzconnection and remove workarounds

* Cleanup fritzbox_callmonitor

* Rework device_info

* Update device_info via registry

* Optimized checks: only when needed

* Revert device_info rework as not needed

* Fix reboot detection for all scenarios

* Final optimization

* Remove leftover

* Update homeassistant/components/shelly/utils.py

Co-authored-by: Shay Levy <levyshay1@gmail.com>

* Update homeassistant/components/shelly/__init__.py

Co-authored-by: Shay Levy <levyshay1@gmail.com>

* Revert slipped in commit

* Rename param

* Apply review comment

* Apply comment review #2

Co-authored-by: Shay Levy <levyshay1@gmail.com>
This commit is contained in:
Simone Chemelli 2021-12-30 15:37:28 +01:00 committed by GitHub
parent 8599ddf51e
commit c27e4feff6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 6 deletions

View file

@ -59,6 +59,7 @@ from .const import (
UPDATE_PERIOD_MULTIPLIER,
)
from .utils import (
device_update_info,
get_block_device_name,
get_block_device_sleep_period,
get_coap_context,
@ -222,7 +223,7 @@ async def async_block_device_setup(
if not entry.data.get(CONF_SLEEP_PERIOD):
hass.data[DOMAIN][DATA_CONFIG_ENTRY][entry.entry_id][
REST
] = ShellyDeviceRestWrapper(hass, device)
] = ShellyDeviceRestWrapper(hass, device, entry)
platforms = BLOCK_PLATFORMS
hass.config_entries.async_setup_platforms(entry, platforms)
@ -407,6 +408,7 @@ class BlockDeviceWrapper(update_coordinator.DataUpdateCoordinator):
try:
async with async_timeout.timeout(POLLING_TIMEOUT_SEC):
await self.device.update()
device_update_info(self.hass, self.device, self.entry)
except OSError as err:
raise update_coordinator.UpdateFailed("Error fetching data") from err
@ -485,7 +487,9 @@ class BlockDeviceWrapper(update_coordinator.DataUpdateCoordinator):
class ShellyDeviceRestWrapper(update_coordinator.DataUpdateCoordinator):
"""Rest Wrapper for a Shelly device with Home Assistant specific functions."""
def __init__(self, hass: HomeAssistant, device: BlockDevice) -> None:
def __init__(
self, hass: HomeAssistant, device: BlockDevice, entry: ConfigEntry
) -> None:
"""Initialize the Shelly device wrapper."""
if (
device.settings["device"]["type"]
@ -504,6 +508,7 @@ class ShellyDeviceRestWrapper(update_coordinator.DataUpdateCoordinator):
update_interval=timedelta(seconds=update_interval),
)
self.device = device
self.entry = entry
async def _async_update_data(self) -> None:
"""Fetch data."""
@ -511,6 +516,14 @@ class ShellyDeviceRestWrapper(update_coordinator.DataUpdateCoordinator):
async with async_timeout.timeout(AIOSHELLY_DEVICE_TIMEOUT_SEC):
_LOGGER.debug("REST update for %s", self.name)
await self.device.update_status()
if self.device.status["uptime"] > 2 * REST_SENSORS_UPDATE_INTERVAL:
return
old_firmware = self.device.firmware_version
await self.device.update_shelly()
if self.device.firmware_version == old_firmware:
return
device_update_info(self.hass, self.device, self.entry)
except OSError as err:
raise update_coordinator.UpdateFailed("Error fetching data") from err
@ -679,6 +692,7 @@ class RpcDeviceWrapper(update_coordinator.DataUpdateCoordinator):
_LOGGER.debug("Reconnecting to Shelly RPC Device - %s", self.name)
async with async_timeout.timeout(AIOSHELLY_DEVICE_TIMEOUT_SEC):
await self.device.initialize()
device_update_info(self.hass, self.device, self.entry)
except OSError as err:
raise update_coordinator.UpdateFailed("Device disconnected") from err

View file

@ -3,7 +3,7 @@
"name": "Shelly",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/shelly",
"requirements": ["aioshelly==1.0.5"],
"requirements": ["aioshelly==1.0.6"],
"zeroconf": [
{
"type": "_http._tcp.local.",

View file

@ -12,7 +12,7 @@ from aioshelly.rpc_device import RpcDevice
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EVENT_HOMEASSISTANT_STOP, TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import singleton
from homeassistant.helpers import device_registry, singleton
from homeassistant.helpers.typing import EventType
from homeassistant.util.dt import utcnow
@ -346,3 +346,28 @@ def get_rpc_input_triggers(device: RpcDevice) -> list[tuple[str, str]]:
triggers.append((trigger_type, subtype))
return triggers
@callback
def device_update_info(
hass: HomeAssistant, shellydevice: BlockDevice | RpcDevice, entry: ConfigEntry
) -> None:
"""Update device registry info."""
_LOGGER.debug("Updating device registry info for %s", entry.title)
assert entry.unique_id
dev_registry = device_registry.async_get(hass)
if device := dev_registry.async_get_device(
identifiers={(DOMAIN, entry.entry_id)},
connections={
(
device_registry.CONNECTION_NETWORK_MAC,
device_registry.format_mac(entry.unique_id),
)
},
):
dev_registry.async_update_device(
device.id, sw_version=shellydevice.firmware_version
)

View file

@ -251,7 +251,7 @@ aiorecollect==1.0.8
aioridwell==2021.12.2
# homeassistant.components.shelly
aioshelly==1.0.5
aioshelly==1.0.6
# homeassistant.components.switcher_kis
aioswitcher==2.0.6

View file

@ -180,7 +180,7 @@ aiorecollect==1.0.8
aioridwell==2021.12.2
# homeassistant.components.shelly
aioshelly==1.0.5
aioshelly==1.0.6
# homeassistant.components.switcher_kis
aioswitcher==2.0.6