Skip to content

Commit

Permalink
Add some more sensors #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre0512 committed Oct 19, 2023
1 parent c962a00 commit df5757e
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 13 deletions.
3 changes: 3 additions & 0 deletions custom_components/speedport/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))

Expand Down
50 changes: 45 additions & 5 deletions custom_components/speedport/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from dataclasses import dataclass

from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
Expand All @@ -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",
),
)

Expand All @@ -38,18 +74,22 @@ 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:
Expand Down
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.5.5"
"speedport-api==0.5.6"
],
"version": "0.3.2"
"version": "0.3.3"
}
36 changes: 33 additions & 3 deletions custom_components/speedport/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
),
)


Expand All @@ -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)
Expand All @@ -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

Expand Down
2 changes: 0 additions & 2 deletions custom_components/speedport/switch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import asyncio
import logging
from typing import Any

Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
speedport-api==0.5.5
speedport-api==0.5.6
homeassistant~=2023.10
pytz~=2023.3

0 comments on commit df5757e

Please sign in to comment.