2022-03-21 22:56:53 +00:00
|
|
|
"""Sensor component for PECO outage counter."""
|
2022-03-29 06:47:20 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Callable
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Final
|
2022-03-21 22:56:53 +00:00
|
|
|
|
2022-03-30 07:50:25 +00:00
|
|
|
from peco import OutageResults
|
2022-03-21 22:56:53 +00:00
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
SensorStateClass,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import PERCENTAGE
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
|
|
|
|
2022-03-30 07:50:25 +00:00
|
|
|
from .const import CONF_COUNTY, DOMAIN
|
2022-03-21 22:56:53 +00:00
|
|
|
|
2022-03-29 06:47:20 +00:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class PECOSensorEntityDescriptionMixin:
|
|
|
|
"""Mixin for required keys."""
|
|
|
|
|
|
|
|
value_fn: Callable[[OutageResults], int]
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class PECOSensorEntityDescription(
|
|
|
|
SensorEntityDescription, PECOSensorEntityDescriptionMixin
|
|
|
|
):
|
|
|
|
"""Description for PECO sensor."""
|
|
|
|
|
|
|
|
|
2022-03-21 22:56:53 +00:00
|
|
|
PARALLEL_UPDATES: Final = 0
|
2022-03-29 06:47:20 +00:00
|
|
|
SENSOR_LIST: tuple[PECOSensorEntityDescription, ...] = (
|
|
|
|
PECOSensorEntityDescription(
|
|
|
|
key="customers_out",
|
|
|
|
name="Customers Out",
|
|
|
|
value_fn=lambda data: int(data.customers_out),
|
|
|
|
),
|
|
|
|
PECOSensorEntityDescription(
|
2022-03-21 22:56:53 +00:00
|
|
|
key="percent_customers_out",
|
|
|
|
name="Percent Customers Out",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
2022-03-29 06:47:20 +00:00
|
|
|
value_fn=lambda data: int(data.percent_customers_out),
|
|
|
|
),
|
|
|
|
PECOSensorEntityDescription(
|
|
|
|
key="outage_count",
|
|
|
|
name="Outage Count",
|
|
|
|
value_fn=lambda data: int(data.outage_count),
|
|
|
|
),
|
|
|
|
PECOSensorEntityDescription(
|
|
|
|
key="customers_served",
|
|
|
|
name="Customers Served",
|
|
|
|
value_fn=lambda data: int(data.customers_served),
|
2022-03-21 22:56:53 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the sensor platform."""
|
|
|
|
county: str = config_entry.data[CONF_COUNTY]
|
2022-03-30 07:50:25 +00:00
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
2022-03-21 22:56:53 +00:00
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
[PecoSensor(sensor, county, coordinator) for sensor in SENSOR_LIST],
|
|
|
|
True,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2022-03-29 06:47:20 +00:00
|
|
|
class PecoSensor(CoordinatorEntity[DataUpdateCoordinator[OutageResults]], SensorEntity):
|
2022-03-21 22:56:53 +00:00
|
|
|
"""PECO outage counter sensor."""
|
|
|
|
|
|
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
|
|
|
_attr_icon: str = "mdi:power-plug-off"
|
2022-03-29 06:47:20 +00:00
|
|
|
entity_description: PECOSensorEntityDescription
|
2022-03-21 22:56:53 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2022-03-29 06:47:20 +00:00
|
|
|
description: PECOSensorEntityDescription,
|
2022-03-21 22:56:53 +00:00
|
|
|
county: str,
|
2022-03-29 06:47:20 +00:00
|
|
|
coordinator: DataUpdateCoordinator[OutageResults],
|
2022-03-21 22:56:53 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(coordinator)
|
|
|
|
self._attr_name = f"{county.capitalize()} {description.name}"
|
|
|
|
self._attr_unique_id = f"{county}-{description.key}"
|
|
|
|
self.entity_description = description
|
|
|
|
|
|
|
|
@property
|
2022-03-29 06:47:20 +00:00
|
|
|
def native_value(self) -> int:
|
2022-03-21 22:56:53 +00:00
|
|
|
"""Return the value of the sensor."""
|
2022-03-29 06:47:20 +00:00
|
|
|
return self.entity_description.value_fn(self.coordinator.data)
|