-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
187 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import logging | ||
|
||
from homeassistant.components.binary_sensor import ( | ||
BinarySensorDeviceClass, | ||
BinarySensorEntity, | ||
BinarySensorEntityDescription, | ||
) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
from speedport import Speedport | ||
|
||
from .const import DOMAIN | ||
from .device import SpeedportEntity | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
BINARY_SENSORS: tuple[BinarySensorEntityDescription, ...] = ( | ||
BinarySensorEntityDescription( | ||
key="onlinestatus", | ||
name="Connection", | ||
device_class=BinarySensorDeviceClass.CONNECTIVITY, | ||
), | ||
BinarySensorEntityDescription( | ||
key="dsl_link_status", | ||
name="DSL-Connection", | ||
device_class=BinarySensorDeviceClass.PLUG, | ||
), | ||
) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback | ||
) -> None: | ||
"""Set up entry.""" | ||
speedport: Speedport = hass.data[DOMAIN][entry.entry_id] | ||
|
||
entities = [ | ||
SpeedportBinarySensor(hass, speedport, description) | ||
for description in BINARY_SENSORS | ||
] | ||
|
||
async_add_entities(entities) | ||
|
||
|
||
class SpeedportBinarySensor(SpeedportEntity, BinarySensorEntity): | ||
entity_description: BinarySensorEntityDescription | ||
|
||
@property | ||
def is_on(self) -> bool | None: | ||
"""Return true if the binary sensor is on.""" | ||
return self._speedport.get(self.entity_description.key) == "online" | ||
|
||
def available(self) -> bool: | ||
if self._speedport.get(self.entity_description.key) is None: | ||
return False | ||
return super().available |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from datetime import datetime | ||
|
||
import pytz | ||
from homeassistant.components.sensor import ( | ||
SensorEntityDescription, | ||
SensorDeviceClass, | ||
SensorStateClass, | ||
SensorEntity, | ||
) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import ( | ||
EntityCategory, | ||
UnitOfDataRate, | ||
) | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
from homeassistant.helpers.typing import StateType | ||
from speedport import Speedport | ||
|
||
from custom_components.speedport import DOMAIN | ||
from custom_components.speedport.device import SpeedportEntity | ||
|
||
SENSORS: tuple[SensorEntityDescription, ...] = ( | ||
SensorEntityDescription( | ||
key="public_ip_v4", | ||
name="IPv4", | ||
icon="mdi:earth", | ||
), | ||
SensorEntityDescription( | ||
key="public_ip_v6", | ||
name="IPv6", | ||
icon="mdi:earth", | ||
), | ||
SensorEntityDescription( | ||
key="inet_uptime", | ||
name="Internet Uptime", | ||
device_class=SensorDeviceClass.TIMESTAMP, | ||
), | ||
SensorEntityDescription( | ||
key="inet_upload", | ||
name="Upload", | ||
state_class=SensorStateClass.MEASUREMENT, | ||
native_unit_of_measurement=UnitOfDataRate.KILOBITS_PER_SECOND, | ||
device_class=SensorDeviceClass.DATA_RATE, | ||
icon="mdi:upload", | ||
), | ||
SensorEntityDescription( | ||
key="inet_download", | ||
name="Download", | ||
state_class=SensorStateClass.MEASUREMENT, | ||
native_unit_of_measurement=UnitOfDataRate.KILOBITS_PER_SECOND, | ||
device_class=SensorDeviceClass.DATA_RATE, | ||
icon="mdi:download", | ||
), | ||
SensorEntityDescription( | ||
key="dsl_upstream", | ||
name="DSL-Link Upstream", | ||
native_unit_of_measurement=UnitOfDataRate.KILOBITS_PER_SECOND, | ||
device_class=SensorDeviceClass.DATA_RATE, | ||
icon="mdi:upload", | ||
), | ||
SensorEntityDescription( | ||
key="dsl_downstream", | ||
name="DSL-Link Downstream", | ||
native_unit_of_measurement=UnitOfDataRate.KILOBITS_PER_SECOND, | ||
device_class=SensorDeviceClass.DATA_RATE, | ||
icon="mdi:download", | ||
), | ||
) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback | ||
) -> None: | ||
"""Set up entry.""" | ||
speedport: Speedport = hass.data[DOMAIN][entry.entry_id] | ||
|
||
entities = [ | ||
SpeedportBinarySensor(hass, speedport, description) for description in SENSORS | ||
] | ||
|
||
async_add_entities(entities) | ||
|
||
|
||
class SpeedportBinarySensor(SpeedportEntity, SensorEntity): | ||
entity_description: SensorEntityDescription | ||
|
||
@property | ||
def native_value(self) -> StateType: | ||
"""Return the value reported by the sensor.""" | ||
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") | ||
return pytz.timezone("Europe/Berlin").localize(date) | ||
return data | ||
|
||
def available(self) -> bool: | ||
if self._speedport.get(self.entity_description.key) is None: | ||
return False | ||
return super().available |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
speedport-api==0.5.2 | ||
homeassistant~=2023.10 | ||
pytz~=2023.3 |