From b0b7cb7f026581c38dbf7273c0ed0bca3ee6a915 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Sat, 7 Oct 2023 01:03:15 +0200 Subject: [PATCH] Make device tracker working --- custom_components/speedport/device_tracker.py | 50 +++++++++++++++---- custom_components/speedport/manifest.json | 4 +- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/custom_components/speedport/device_tracker.py b/custom_components/speedport/device_tracker.py index f1f7690..19d722b 100644 --- a/custom_components/speedport/device_tracker.py +++ b/custom_components/speedport/device_tracker.py @@ -1,32 +1,56 @@ from __future__ import annotations +import logging +from datetime import timedelta + from homeassistant.components.device_tracker import ScannerEntity, SourceType from homeassistant.config_entries import ConfigEntry -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.update_coordinator import ( + CoordinatorEntity, + DataUpdateCoordinator, +) +from speedport import Speedport from speedport.device import WlanDevice from .const import DOMAIN +_LOGGER = logging.getLogger(__name__) + async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: - coordinator = hass.data[DOMAIN][entry.entry_id] - devices = [SpeedportTracker(device) for device in await coordinator.devices] + speedport = hass.data[DOMAIN][entry.entry_id] + coordinator = SpeedportCoordinator(hass, speedport) + await coordinator.async_config_entry_first_refresh() + devices = [SpeedportTracker(coordinator, mac) for mac in await speedport.devices] async_add_entities(devices) -class SpeedportTracker(ScannerEntity): - _attr_should_poll = False +class SpeedportCoordinator(DataUpdateCoordinator): + def __init__(self, hass: HomeAssistant, api: Speedport): + """Initialize my coordinator.""" + super().__init__( + hass, + _LOGGER, + name="Speedport", + update_interval=timedelta(seconds=10), + ) + self._api: Speedport = api + + async def _async_update_data(self): + return await self._api.devices - def __init__(self, device: WlanDevice) -> None: - super().__init__() - self._device = device - self._attr_unique_id = device.mac - self._attr_name = device.name + +class SpeedportTracker(CoordinatorEntity, ScannerEntity): + def __init__(self, coordinator: SpeedportCoordinator, mac: str) -> None: + super().__init__(coordinator) + self._mac = mac + self._device: WlanDevice = self.coordinator.data.get(mac) @property def source_type(self) -> SourceType: @@ -83,3 +107,9 @@ def extra_state_attributes(self) -> dict[str, str]: def name(self) -> str: """Return device name.""" return self._device.name + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self._device = self.coordinator.data.get(self._mac) + self.async_write_ha_state() diff --git a/custom_components/speedport/manifest.json b/custom_components/speedport/manifest.json index 3460c03..dd8bc6c 100644 --- a/custom_components/speedport/manifest.json +++ b/custom_components/speedport/manifest.json @@ -12,7 +12,7 @@ "iot_class": "local_polling", "issue_tracker": "https://github.com/Andre0512/speedport/issues", "requirements": [ - "speedport-api~=0.4" + "speedport-api==0.4.7" ], - "version": "0.1.0" + "version": "0.1.1" }