From 7a232d730f9574afa90a49e5498f290b380fed74 Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Tue, 10 May 2022 12:44:47 +0200 Subject: [PATCH] Add support for update entites (#136) * Add update sensors --- __init__.py | 10 +++++++++- manifest.json | 4 ++-- sensor.py | 6 ++++++ update.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 update.py diff --git a/__init__.py b/__init__.py index 291a028..d3f7192 100644 --- a/__init__.py +++ b/__init__.py @@ -16,7 +16,7 @@ from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from nibeuplink import Uplink, UplinkSession -from nibeuplink.typing import ParameterId, ParameterType, System +from nibeuplink.typing import ParameterId, ParameterType, System, SystemSoftwareInfo from .const import ( CONF_ACCESS_DATA, @@ -127,6 +127,7 @@ def ensure_system_dict(value: dict[int, dict] | list[dict] | None) -> dict[int, "binary_sensor", "water_heater", "fan", + "update", ) @@ -269,6 +270,7 @@ def __init__( self.parent = parent self.notice: list[dict] = [] self.statuses: set[str] = set() + self.software: SystemSoftwareInfo | None = None self._unsub: list[Callable] = [] self.config = config self._parameters: ParameterSet = {} @@ -312,6 +314,7 @@ async def _async_update_data(self) -> None: """Update data via library.""" await self.update_notifications() await self.update_statuses() + await self.update_version() parameters = set() for subscriber_parameters in self._parameter_subscribers.values(): @@ -321,6 +324,11 @@ async def _async_update_data(self) -> None: await self.update_parameters(parameters) + async def update_version(self): + """Update software version.""" + self.software = await self.uplink.get_system_software(self.system_id) + _LOGGER.debug("Version: %s", self.software) + async def update_statuses(self): """Update status list.""" status_icons = await self.uplink.get_status(self.system_id) diff --git a/manifest.json b/manifest.json index e46b9ec..6c1dc24 100644 --- a/manifest.json +++ b/manifest.json @@ -4,10 +4,10 @@ "config_flow": true, "documentation": "https://github.com/elupus/hass_nibe", "requirements": [ - "nibeuplink==1.2.1" + "nibeuplink==1.3.0" ], "codeowners": [ "@elupus" ], - "version": "1.4.0" + "version": "1.5.0" } diff --git a/sensor.py b/sensor.py index 03e6af2..13cd9fb 100644 --- a/sensor.py +++ b/sensor.py @@ -294,6 +294,12 @@ class NibeSystemSensorEntityDescription(SensorEntityDescription): entity_category=EntityCategory.DIAGNOSTIC, state_fn=lambda x: str(x.system["hasAlarmed"]), ), + NibeSystemSensorEntityDescription( + key="software", + name="software version", + entity_category=EntityCategory.DIAGNOSTIC, + state_fn=lambda x: str(x.software["current"]["name"]) if x.software else None, + ), ) diff --git a/update.py b/update.py new file mode 100644 index 0000000..4e6d766 --- /dev/null +++ b/update.py @@ -0,0 +1,49 @@ +"""Update sensors for nibe uplink.""" +from __future__ import annotations + +from homeassistant.components.update import ENTITY_ID_FORMAT, UpdateEntity +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from . import NibeData, NibeSystem +from .const import DATA_NIBE_ENTRIES, DOMAIN + + +async def async_setup_entry( + hass: HomeAssistant, entry: ConfigEntry, async_add_entities +): + """Set up the device based on a config entry.""" + data: NibeData = hass.data[DATA_NIBE_ENTRIES][entry.entry_id] + + entities = [NibeUpdateSensor(system) for system in data.systems.values()] + async_add_entities(entities, False) + + +class NibeUpdateSensor(CoordinatorEntity[NibeSystem], UpdateEntity): + """Update sensor.""" + + def __init__(self, system: NibeSystem): + """Init.""" + super().__init__(system) + self._attr_name = "software update" + self._attr_device_info = {"identifiers": {(DOMAIN, system.system_id)}} + self._attr_unique_id = f"{system.system_id}_system_update" + self.entity_id = ENTITY_ID_FORMAT.format(f"{DOMAIN}_{system.system_id}_update") + + @callback + def _handle_coordinator_update(self) -> None: + """Update when the coordinator updates.""" + if not self.coordinator.software: + return + self._attr_installed_version = self.coordinator.software["current"]["name"] + if self.coordinator.software["upgrade"]: + self._attr_latest_version = self.coordinator.software["upgrade"]["name"] + self._attr_release_summary = self.coordinator.software["upgrade"][ + "releaseDate" + ] + else: + self._attr_latest_version = self._attr_installed_version + self._attr_release_summary = None + self._attr_release_url = f"https://nibeuplink.com/System/{self.coordinator.system_id}/Support/Software" + super()._handle_coordinator_update()