Skip to content

Commit

Permalink
Make device tracker working
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre0512 committed Oct 6, 2023
1 parent f5c8680 commit b0b7cb7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
50 changes: 40 additions & 10 deletions custom_components/speedport/device_tracker.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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()
4 changes: 2 additions & 2 deletions custom_components/speedport/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit b0b7cb7

Please sign in to comment.