Add outage map alerts to PECO (#69825)
This commit is contained in:
parent
c87992715f
commit
6c75eaa1bc
8 changed files with 86 additions and 33 deletions
|
@ -5,8 +5,6 @@ from collections.abc import Callable
|
|||
from dataclasses import dataclass
|
||||
from typing import Final
|
||||
|
||||
from peco import OutageResults
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
|
@ -21,14 +19,16 @@ from homeassistant.helpers.update_coordinator import (
|
|||
DataUpdateCoordinator,
|
||||
)
|
||||
|
||||
from .const import CONF_COUNTY, DOMAIN
|
||||
from . import PECOCoordinatorData
|
||||
from .const import ATTR_CONTENT, CONF_COUNTY, DOMAIN
|
||||
|
||||
|
||||
@dataclass
|
||||
class PECOSensorEntityDescriptionMixin:
|
||||
"""Mixin for required keys."""
|
||||
|
||||
value_fn: Callable[[OutageResults], int]
|
||||
value_fn: Callable[[PECOCoordinatorData], int | str]
|
||||
attribute_fn: Callable[[PECOCoordinatorData], dict[str, str]]
|
||||
|
||||
|
||||
@dataclass
|
||||
|
@ -43,23 +43,33 @@ SENSOR_LIST: tuple[PECOSensorEntityDescription, ...] = (
|
|||
PECOSensorEntityDescription(
|
||||
key="customers_out",
|
||||
name="Customers Out",
|
||||
value_fn=lambda data: int(data.customers_out),
|
||||
value_fn=lambda data: int(data.outages.customers_out),
|
||||
attribute_fn=lambda data: {},
|
||||
),
|
||||
PECOSensorEntityDescription(
|
||||
key="percent_customers_out",
|
||||
name="Percent Customers Out",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
value_fn=lambda data: int(data.percent_customers_out),
|
||||
value_fn=lambda data: int(data.outages.percent_customers_out),
|
||||
attribute_fn=lambda data: {},
|
||||
),
|
||||
PECOSensorEntityDescription(
|
||||
key="outage_count",
|
||||
name="Outage Count",
|
||||
value_fn=lambda data: int(data.outage_count),
|
||||
value_fn=lambda data: int(data.outages.outage_count),
|
||||
attribute_fn=lambda data: {},
|
||||
),
|
||||
PECOSensorEntityDescription(
|
||||
key="customers_served",
|
||||
name="Customers Served",
|
||||
value_fn=lambda data: int(data.customers_served),
|
||||
value_fn=lambda data: int(data.outages.customers_served),
|
||||
attribute_fn=lambda data: {},
|
||||
),
|
||||
PECOSensorEntityDescription(
|
||||
key="map_alert",
|
||||
name="Map Alert",
|
||||
value_fn=lambda data: str(data.alerts.alert_title),
|
||||
attribute_fn=lambda data: {ATTR_CONTENT: data.alerts.alert_content},
|
||||
),
|
||||
)
|
||||
|
||||
|
@ -80,26 +90,36 @@ async def async_setup_entry(
|
|||
return
|
||||
|
||||
|
||||
class PecoSensor(CoordinatorEntity[DataUpdateCoordinator[OutageResults]], SensorEntity):
|
||||
class PecoSensor(
|
||||
CoordinatorEntity[DataUpdateCoordinator[PECOCoordinatorData]], SensorEntity
|
||||
):
|
||||
"""PECO outage counter sensor."""
|
||||
|
||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||
_attr_icon: str = "mdi:power-plug-off"
|
||||
entity_description: PECOSensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
description: PECOSensorEntityDescription,
|
||||
county: str,
|
||||
coordinator: DataUpdateCoordinator[OutageResults],
|
||||
coordinator: DataUpdateCoordinator[PECOCoordinatorData],
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator)
|
||||
self._attr_name = f"{county.capitalize()} {description.name}"
|
||||
self._attr_unique_id = f"{county}-{description.key}"
|
||||
self._attr_icon = (
|
||||
"mdi:alert" if description.key == "map_alert" else "mdi:power-plug-off"
|
||||
)
|
||||
if description.key != "map_alert":
|
||||
self._attr_state_class = SensorStateClass.MEASUREMENT
|
||||
self.entity_description = description
|
||||
|
||||
@property
|
||||
def native_value(self) -> int:
|
||||
def native_value(self) -> int | str:
|
||||
"""Return the value of the sensor."""
|
||||
return self.entity_description.value_fn(self.coordinator.data)
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> dict[str, str]:
|
||||
"""Return state attributes for the sensor."""
|
||||
return self.entity_description.attribute_fn(self.coordinator.data)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue