hass-core/homeassistant/components/peco/sensor.py

114 lines
3.6 KiB
Python
Raw Normal View History

Add PECO power outage counter integration (#65194) * Create a new NWS Alerts integration * Create a new NWS Alerts integration * Create new PECO integration * Remove empty keys * Revert "Create a new NWS Alerts integration" This reverts commit 38309c5a878d78f26df56a62e56cb602d9dc9a9e. * Revert "Create a new NWS Alerts integration" This reverts commit aeabdd37b86c370bdb8009e885806bdac6e464d8. * Fix test with new mock data * Add init and sensor to .coveragerc and more tests for config flow * Small fixes and replacing patch with pytest.raises in testing invalid county * Add type defs and fix test_config_flow to use MultipleValid instead * Fix issues with 'typing.Dict' * Move API communication to seperate PyPI library * Switch PyPI library from httpx to aiohttp to allow for passing in websessions * Commit file changes requested by farmio as listed here: https://github.com/home-assistant/core/pull/65194/files/d267e4300a4d359d88ef33e43b66d0e961ac154d * Add suggestions requested by farmio as listed here: https://github.com/home-assistant/core/pull/65194/files/586d8ffa42d7860d91e25fb82b2d6eace6645a82 * Move native_unit_of_measurement from prop to attr * Update PLATFORMS constant type annotation Co-authored-by: Matthias Alphart <farmio@alphart.net> * Add peco to .strict-typing I am from school so I can't run mypy atm * Forgot to import Final * Do as requested [here](https://github.com/home-assistant/core/runs/5070634928?check_suite_focus=true) * Updated mypy.ini, checks should pass now * Fix to conform to mypy restrictions https://github.com/home-assistant/core/runs/5072861837\?check_suite_focus\=true * Fix type annotations * Fix tests * Use cast in async_update_data * Add data type to CoordinatorEntity and DataUpdateCoordinator * More cleanup from suggestions here: https://github.com/home-assistant/core/pull/65194\#pullrequestreview-908183493 * Fix tests for new code * Cleaning up a speck of dust * Remove unused variable from the peco sensor * Add rounding to percentage, and extra clean-up * Final suggestions from @farmio * Update SCAN_INTERVAL to be a little bit faster * Change the SCAN_INTERVAL to be somewhat near the update interval of the outage map, as noted by farmio * New UpdateCoordinator typing
2022-03-21 18:56:53 -04:00
"""Sensor component for PECO outage counter."""
import asyncio
from datetime import timedelta
from typing import Final, cast
from peco import BadJSONError, HttpError
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.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed,
)
from .const import CONF_COUNTY, DOMAIN, LOGGER, SCAN_INTERVAL
PARALLEL_UPDATES: Final = 0
SENSOR_LIST = (
SensorEntityDescription(key="customers_out", name="Customers Out"),
SensorEntityDescription(
key="percent_customers_out",
name="Percent Customers Out",
native_unit_of_measurement=PERCENTAGE,
),
SensorEntityDescription(key="outage_count", name="Outage Count"),
SensorEntityDescription(key="customers_served", name="Customers Served"),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the sensor platform."""
api = hass.data[DOMAIN][config_entry.entry_id]
websession = async_get_clientsession(hass)
county: str = config_entry.data[CONF_COUNTY]
async def async_update_data() -> dict[str, float]:
"""Fetch data from API."""
try:
data = (
cast(dict[str, float], await api.get_outage_totals(websession))
if county == "TOTAL"
else cast(
dict[str, float],
await api.get_outage_count(county, websession),
)
)
except HttpError as err:
raise UpdateFailed(f"Error fetching data: {err}") from err
except BadJSONError as err:
raise UpdateFailed(f"Error parsing data: {err}") from err
except asyncio.TimeoutError as 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
coordinator = DataUpdateCoordinator(
hass,
LOGGER,
name="PECO Outage Count",
update_method=async_update_data,
update_interval=timedelta(minutes=SCAN_INTERVAL),
)
await coordinator.async_config_entry_first_refresh()
async_add_entities(
[PecoSensor(sensor, county, coordinator) for sensor in SENSOR_LIST],
True,
)
return
class PecoSensor(
CoordinatorEntity[DataUpdateCoordinator[dict[str, float]]], SensorEntity
):
"""PECO outage counter sensor."""
_attr_state_class = SensorStateClass.MEASUREMENT
_attr_icon: str = "mdi:power-plug-off"
def __init__(
self,
description: SensorEntityDescription,
county: str,
coordinator: DataUpdateCoordinator[dict[str, float]],
) -> 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
def native_value(self) -> float:
"""Return the value of the sensor."""
return self.coordinator.data[self.entity_description.key]