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

Refresh device_info for Shelly devices #62899

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions homeassistant/components/shelly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
UPDATE_PERIOD_MULTIPLIER,
)
from .utils import (
device_update_info,
get_block_device_name,
get_block_device_sleep_period,
get_coap_context,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -407,6 +408,7 @@ async def _async_update_data(self) -> None:
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

Expand Down Expand Up @@ -485,7 +487,9 @@ def _handle_ha_stop(self, _event: Event) -> None:
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"]
Expand All @@ -504,13 +508,22 @@ def __init__(self, hass: HomeAssistant, device: BlockDevice) -> None:
update_interval=timedelta(seconds=update_interval),
)
self.device = device
self.entry = entry

async def _async_update_data(self) -> None:
"""Fetch data."""
try:
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

Expand Down Expand Up @@ -679,6 +692,7 @@ async def _async_update_data(self) -> None:
_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

Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/shelly/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
27 changes: 26 additions & 1 deletion homeassistant/components/shelly/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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

Expand Down Expand Up @@ -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
)
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down