diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index 8d78c148b51..4737dd6317e 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -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 diff --git a/homeassistant/components/shelly/manifest.json b/homeassistant/components/shelly/manifest.json index 7d4da653d51..1baecc484e5 100644 --- a/homeassistant/components/shelly/manifest.json +++ b/homeassistant/components/shelly/manifest.json @@ -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.", diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py index a77f338a51e..a824488327b 100644 --- a/homeassistant/components/shelly/utils.py +++ b/homeassistant/components/shelly/utils.py @@ -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 + ) diff --git a/requirements_all.txt b/requirements_all.txt index a0d893895ea..7dbb2a9d411 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index d9c8b9f7165..f7ebff0b4ab 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -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