From f1381b7f67f87ae5e118f8cf2deaba4884fb3975 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Thu, 19 Oct 2023 23:00:22 +0200 Subject: [PATCH] Add some more sensors #6 --- custom_components/speedport/__init__.py | 3 ++ custom_components/speedport/binary_sensor.py | 51 ++++++++++++++++++-- custom_components/speedport/manifest.json | 4 +- custom_components/speedport/sensor.py | 36 ++++++++++++-- custom_components/speedport/switch.py | 2 - requirements.txt | 2 +- 6 files changed, 85 insertions(+), 13 deletions(-) diff --git a/custom_components/speedport/__init__.py b/custom_components/speedport/__init__.py index 838c54a..96bdec5 100644 --- a/custom_components/speedport/__init__.py +++ b/custom_components/speedport/__init__.py @@ -1,4 +1,6 @@ """The Speedport integration.""" +import asyncio + from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform from homeassistant.core import HomeAssistant @@ -29,6 +31,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ).create() hass.data[DOMAIN][entry.entry_id] = speedport hass.data[DOMAIN]["coordinators"] = {} + await asyncio.gather(*[speedport.update_status(), speedport.update_ip_data()]) entry.async_on_unload(entry.add_update_listener(update_listener)) diff --git a/custom_components/speedport/binary_sensor.py b/custom_components/speedport/binary_sensor.py index c885ef4..4034756 100644 --- a/custom_components/speedport/binary_sensor.py +++ b/custom_components/speedport/binary_sensor.py @@ -1,4 +1,5 @@ import logging +from dataclasses import dataclass from homeassistant.components.binary_sensor import ( BinarySensorDeviceClass, @@ -15,16 +16,51 @@ _LOGGER = logging.getLogger(__name__) -BINARY_SENSORS: tuple[BinarySensorEntityDescription, ...] = ( - BinarySensorEntityDescription( + +@dataclass +class SpeedportBinarySensorEntityDescription(BinarySensorEntityDescription): + condition_key: str = "" + value: str = "" + + +BINARY_SENSORS: tuple[SpeedportBinarySensorEntityDescription, ...] = ( + SpeedportBinarySensorEntityDescription( key="onlinestatus", name="Connection", device_class=BinarySensorDeviceClass.CONNECTIVITY, + value="online", ), - BinarySensorEntityDescription( + SpeedportBinarySensorEntityDescription( key="dsl_link_status", name="DSL-Connection", device_class=BinarySensorDeviceClass.PLUG, + value="online", + ), + SpeedportBinarySensorEntityDescription( + key="dualstack", + name="Dual Stack", + value="1", + ), + SpeedportBinarySensorEntityDescription( + key="dsl_tunnel", + name="DSL Tunnel", + device_class=BinarySensorDeviceClass.CONNECTIVITY, + condition_key="use_lte", + value="1", + ), + SpeedportBinarySensorEntityDescription( + key="lte_tunnel", + name="LTE Tunnel", + device_class=BinarySensorDeviceClass.CONNECTIVITY, + condition_key="use_lte", + value="1", + ), + SpeedportBinarySensorEntityDescription( + key="hybrid_tunnel", + name="Hybrid Tunnel", + device_class=BinarySensorDeviceClass.CONNECTIVITY, + condition_key="use_lte", + value="1", ), ) @@ -38,18 +74,23 @@ async def async_setup_entry( entities = [ SpeedportBinarySensor(hass, speedport, description) for description in BINARY_SENSORS + if not description.condition_key + or speedport.get(description.condition_key) == "1" ] async_add_entities(entities) class SpeedportBinarySensor(SpeedportEntity, BinarySensorEntity): - entity_description: BinarySensorEntityDescription + entity_description: SpeedportBinarySensorEntityDescription @property def is_on(self) -> bool | None: """Return true if the binary sensor is on.""" - return self._speedport.get(self.entity_description.key) == "online" + return ( + self._speedport.get(self.entity_description.key) + == self.entity_description.value + ) def available(self) -> bool: if self._speedport.get(self.entity_description.key) is None: diff --git a/custom_components/speedport/manifest.json b/custom_components/speedport/manifest.json index 85976eb..5a90467 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.5.5" + "speedport-api==0.5.6" ], - "version": "0.3.2" + "version": "0.3.3" } diff --git a/custom_components/speedport/sensor.py b/custom_components/speedport/sensor.py index 6befaa0..e8f90a4 100644 --- a/custom_components/speedport/sensor.py +++ b/custom_components/speedport/sensor.py @@ -9,8 +9,8 @@ ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( - EntityCategory, UnitOfDataRate, + SIGNAL_STRENGTH_DECIBELS_MILLIWATT, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -66,6 +66,34 @@ device_class=SensorDeviceClass.DATA_RATE, icon="mdi:download", ), + SensorEntityDescription( + key="router_state", + name="Router State", + icon="mdi:router-wireless", + ), + SensorEntityDescription( + key="dsl_pop", + name="DSL-PoP", + icon="mdi:map-marker-radius", + ), + SensorEntityDescription( + key="ex5g_signal_5g", + name="5G Signal", + native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, + icon="mdi:signal", + ), + SensorEntityDescription( + key="ex5g_freq_5g", name="5G Frequency", icon="mdi:signal-5g" + ), + SensorEntityDescription( + key="ex5g_signal_lte", + name="LTE Signal", + native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, + icon="mdi:signal", + ), + SensorEntityDescription( + key="ex5g_freq_lte", name="LTE Frequency", icon="mdi:signal-4g" + ), ) @@ -76,7 +104,9 @@ async def async_setup_entry( speedport: Speedport = hass.data[DOMAIN][entry.entry_id] entities = [ - SpeedportBinarySensor(hass, speedport, description) for description in SENSORS + SpeedportBinarySensor(hass, speedport, description) + for description in SENSORS + if speedport.get(description.key) is not None ] async_add_entities(entities) @@ -91,7 +121,7 @@ def native_value(self) -> StateType: if (data := self._speedport.get(self.entity_description.key)) is None: return None if self.entity_description.device_class == SensorDeviceClass.TIMESTAMP: - date = datetime.strptime(data, "%Y-%m-%d %H:%M:%S") + date = datetime.strptime(data, "%Y-%m-%d %H:%M:%S").replace(second=0) return pytz.timezone("Europe/Berlin").localize(date) return data diff --git a/custom_components/speedport/switch.py b/custom_components/speedport/switch.py index 4f39b3e..ab5c800 100644 --- a/custom_components/speedport/switch.py +++ b/custom_components/speedport/switch.py @@ -1,6 +1,5 @@ from __future__ import annotations -import asyncio import logging from typing import Any @@ -23,7 +22,6 @@ async def async_setup_entry( """Set up entry.""" speedport: Speedport = hass.data[DOMAIN][entry.entry_id] - await asyncio.gather(*[speedport.update_status(), speedport.update_ip_data()]) async_add_entities( [ SpeedportWifiSwitch(hass, speedport), diff --git a/requirements.txt b/requirements.txt index 589dfca..d741924 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -speedport-api==0.5.5 +speedport-api==0.5.6 homeassistant~=2023.10 pytz~=2023.3