Add support for static typing for the PECO library (#68707)
This commit is contained in:
parent
8714beb5e7
commit
fb14ae211e
5 changed files with 56 additions and 33 deletions
|
@ -8,6 +8,6 @@
|
||||||
],
|
],
|
||||||
"iot_class": "cloud_polling",
|
"iot_class": "cloud_polling",
|
||||||
"requirements": [
|
"requirements": [
|
||||||
"peco==0.0.21"
|
"peco==0.0.25"
|
||||||
]
|
]
|
||||||
}
|
}
|
|
@ -1,9 +1,13 @@
|
||||||
"""Sensor component for PECO outage counter."""
|
"""Sensor component for PECO outage counter."""
|
||||||
import asyncio
|
from __future__ import annotations
|
||||||
from datetime import timedelta
|
|
||||||
from typing import Final, cast
|
|
||||||
|
|
||||||
from peco import BadJSONError, HttpError
|
import asyncio
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from datetime import timedelta
|
||||||
|
from typing import Final
|
||||||
|
|
||||||
|
from peco import BadJSONError, HttpError, OutageResults
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
|
@ -23,16 +27,44 @@ from homeassistant.helpers.update_coordinator import (
|
||||||
|
|
||||||
from .const import CONF_COUNTY, DOMAIN, LOGGER, SCAN_INTERVAL
|
from .const import CONF_COUNTY, DOMAIN, LOGGER, SCAN_INTERVAL
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class PECOSensorEntityDescriptionMixin:
|
||||||
|
"""Mixin for required keys."""
|
||||||
|
|
||||||
|
value_fn: Callable[[OutageResults], int]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class PECOSensorEntityDescription(
|
||||||
|
SensorEntityDescription, PECOSensorEntityDescriptionMixin
|
||||||
|
):
|
||||||
|
"""Description for PECO sensor."""
|
||||||
|
|
||||||
|
|
||||||
PARALLEL_UPDATES: Final = 0
|
PARALLEL_UPDATES: Final = 0
|
||||||
SENSOR_LIST = (
|
SENSOR_LIST: tuple[PECOSensorEntityDescription, ...] = (
|
||||||
SensorEntityDescription(key="customers_out", name="Customers Out"),
|
PECOSensorEntityDescription(
|
||||||
SensorEntityDescription(
|
key="customers_out",
|
||||||
|
name="Customers Out",
|
||||||
|
value_fn=lambda data: int(data.customers_out),
|
||||||
|
),
|
||||||
|
PECOSensorEntityDescription(
|
||||||
key="percent_customers_out",
|
key="percent_customers_out",
|
||||||
name="Percent Customers Out",
|
name="Percent Customers Out",
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
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),
|
||||||
),
|
),
|
||||||
SensorEntityDescription(key="outage_count", name="Outage Count"),
|
|
||||||
SensorEntityDescription(key="customers_served", name="Customers Served"),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -46,16 +78,13 @@ async def async_setup_entry(
|
||||||
websession = async_get_clientsession(hass)
|
websession = async_get_clientsession(hass)
|
||||||
county: str = config_entry.data[CONF_COUNTY]
|
county: str = config_entry.data[CONF_COUNTY]
|
||||||
|
|
||||||
async def async_update_data() -> dict[str, float]:
|
async def async_update_data() -> OutageResults:
|
||||||
"""Fetch data from API."""
|
"""Fetch data from API."""
|
||||||
try:
|
try:
|
||||||
data = (
|
data: OutageResults = (
|
||||||
cast(dict[str, float], await api.get_outage_totals(websession))
|
await api.get_outage_totals(websession)
|
||||||
if county == "TOTAL"
|
if county == "TOTAL"
|
||||||
else cast(
|
else await api.get_outage_count(county, websession)
|
||||||
dict[str, float],
|
|
||||||
await api.get_outage_count(county, websession),
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
except HttpError as err:
|
except HttpError as err:
|
||||||
raise UpdateFailed(f"Error fetching data: {err}") from err
|
raise UpdateFailed(f"Error fetching data: {err}") from err
|
||||||
|
@ -63,11 +92,6 @@ async def async_setup_entry(
|
||||||
raise UpdateFailed(f"Error parsing data: {err}") from err
|
raise UpdateFailed(f"Error parsing data: {err}") from err
|
||||||
except asyncio.TimeoutError as err:
|
except asyncio.TimeoutError as err:
|
||||||
raise UpdateFailed(f"Timeout fetching data: {err}") from err
|
raise UpdateFailed(f"Timeout fetching data: {err}") from err
|
||||||
if data["percent_customers_out"] < 5:
|
|
||||||
percent_out = round(
|
|
||||||
data["customers_out"] / data["customers_served"] * 100, 3
|
|
||||||
)
|
|
||||||
data["percent_customers_out"] = percent_out
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
coordinator = DataUpdateCoordinator(
|
coordinator = DataUpdateCoordinator(
|
||||||
|
@ -87,19 +111,18 @@ async def async_setup_entry(
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
class PecoSensor(
|
class PecoSensor(CoordinatorEntity[DataUpdateCoordinator[OutageResults]], SensorEntity):
|
||||||
CoordinatorEntity[DataUpdateCoordinator[dict[str, float]]], SensorEntity
|
|
||||||
):
|
|
||||||
"""PECO outage counter sensor."""
|
"""PECO outage counter sensor."""
|
||||||
|
|
||||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||||
_attr_icon: str = "mdi:power-plug-off"
|
_attr_icon: str = "mdi:power-plug-off"
|
||||||
|
entity_description: PECOSensorEntityDescription
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
description: SensorEntityDescription,
|
description: PECOSensorEntityDescription,
|
||||||
county: str,
|
county: str,
|
||||||
coordinator: DataUpdateCoordinator[dict[str, float]],
|
coordinator: DataUpdateCoordinator[OutageResults],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
@ -108,6 +131,6 @@ class PecoSensor(
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> float:
|
def native_value(self) -> int:
|
||||||
"""Return the value of the sensor."""
|
"""Return the value of the sensor."""
|
||||||
return self.coordinator.data[self.entity_description.key]
|
return self.entity_description.value_fn(self.coordinator.data)
|
||||||
|
|
|
@ -1180,7 +1180,7 @@ panasonic_viera==0.3.6
|
||||||
pdunehd==1.3.2
|
pdunehd==1.3.2
|
||||||
|
|
||||||
# homeassistant.components.peco
|
# homeassistant.components.peco
|
||||||
peco==0.0.21
|
peco==0.0.25
|
||||||
|
|
||||||
# homeassistant.components.pencom
|
# homeassistant.components.pencom
|
||||||
pencompy==0.0.3
|
pencompy==0.0.3
|
||||||
|
|
|
@ -777,7 +777,7 @@ panasonic_viera==0.3.6
|
||||||
pdunehd==1.3.2
|
pdunehd==1.3.2
|
||||||
|
|
||||||
# homeassistant.components.peco
|
# homeassistant.components.peco
|
||||||
peco==0.0.21
|
peco==0.0.25
|
||||||
|
|
||||||
# homeassistant.components.aruba
|
# homeassistant.components.aruba
|
||||||
# homeassistant.components.cisco_ios
|
# homeassistant.components.cisco_ios
|
||||||
|
|
|
@ -66,7 +66,7 @@ async def test_sensor_available(
|
||||||
if sensor == "total_customers_out":
|
if sensor == "total_customers_out":
|
||||||
assert sensor_entity.state == "123"
|
assert sensor_entity.state == "123"
|
||||||
elif sensor == "total_percent_customers_out":
|
elif sensor == "total_percent_customers_out":
|
||||||
assert sensor_entity.state == "15.589"
|
assert sensor_entity.state == "1"
|
||||||
elif sensor == "total_outage_count":
|
elif sensor == "total_outage_count":
|
||||||
assert sensor_entity.state == "456"
|
assert sensor_entity.state == "456"
|
||||||
elif sensor == "total_customers_served":
|
elif sensor == "total_customers_served":
|
||||||
|
@ -125,7 +125,7 @@ async def test_sensor_available(
|
||||||
if sensor == "bucks_customers_out":
|
if sensor == "bucks_customers_out":
|
||||||
assert sensor_entity.state == "123"
|
assert sensor_entity.state == "123"
|
||||||
elif sensor == "bucks_percent_customers_out":
|
elif sensor == "bucks_percent_customers_out":
|
||||||
assert sensor_entity.state == "15.589"
|
assert sensor_entity.state == "1"
|
||||||
elif sensor == "bucks_outage_count":
|
elif sensor == "bucks_outage_count":
|
||||||
assert sensor_entity.state == "456"
|
assert sensor_entity.state == "456"
|
||||||
elif sensor == "bucks_customers_served":
|
elif sensor == "bucks_customers_served":
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue