Add upnp sensor for IP, Status, and Uptime (#54780)
Co-authored-by: Joakim Sørensen <hi@ludeeus.dev>
This commit is contained in:
parent
ae9e3c237a
commit
4310a7d814
6 changed files with 191 additions and 138 deletions
|
@ -9,8 +9,15 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import UpnpDataUpdateCoordinator, UpnpEntity
|
||||
from .const import DOMAIN, LOGGER, WANSTATUS
|
||||
from . import UpnpBinarySensorEntityDescription, UpnpDataUpdateCoordinator, UpnpEntity
|
||||
from .const import DOMAIN, LOGGER, WAN_STATUS
|
||||
|
||||
BINARYSENSOR_ENTITY_DESCRIPTIONS: tuple[UpnpBinarySensorEntityDescription, ...] = (
|
||||
UpnpBinarySensorEntityDescription(
|
||||
key=WAN_STATUS,
|
||||
name="wan status",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
|
@ -23,10 +30,14 @@ async def async_setup_entry(
|
|||
|
||||
LOGGER.debug("Adding binary sensor")
|
||||
|
||||
sensors = [
|
||||
UpnpStatusBinarySensor(coordinator),
|
||||
]
|
||||
async_add_entities(sensors)
|
||||
async_add_entities(
|
||||
UpnpStatusBinarySensor(
|
||||
coordinator=coordinator,
|
||||
entity_description=entity_description,
|
||||
)
|
||||
for entity_description in BINARYSENSOR_ENTITY_DESCRIPTIONS
|
||||
if coordinator.data.get(entity_description.key) is not None
|
||||
)
|
||||
|
||||
|
||||
class UpnpStatusBinarySensor(UpnpEntity, BinarySensorEntity):
|
||||
|
@ -37,18 +48,12 @@ class UpnpStatusBinarySensor(UpnpEntity, BinarySensorEntity):
|
|||
def __init__(
|
||||
self,
|
||||
coordinator: UpnpDataUpdateCoordinator,
|
||||
entity_description: UpnpBinarySensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the base sensor."""
|
||||
super().__init__(coordinator)
|
||||
self._attr_name = f"{coordinator.device.name} wan status"
|
||||
self._attr_unique_id = f"{coordinator.device.udn}_wanstatus"
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available."""
|
||||
return super().available and self.coordinator.data.get(WANSTATUS)
|
||||
super().__init__(coordinator=coordinator, entity_description=entity_description)
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return true if the binary sensor is on."""
|
||||
return self.coordinator.data[WANSTATUS] == "Connected"
|
||||
return self.coordinator.data[self.entity_description.key] == "Connected"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue