Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
Add support for update entites (#136)
Browse files Browse the repository at this point in the history
* Add update sensors
  • Loading branch information
elupus authored May 10, 2022
1 parent daddda3 commit 7a232d7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
10 changes: 9 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -127,6 +127,7 @@ def ensure_system_dict(value: dict[int, dict] | list[dict] | None) -> dict[int,
"binary_sensor",
"water_heater",
"fan",
"update",
)


Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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():
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
6 changes: 6 additions & 0 deletions sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)


Expand Down
49 changes: 49 additions & 0 deletions update.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 7a232d7

Please sign in to comment.