From c38539b368cc2a0c9571a44a940425382ed338cc Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Sat, 18 May 2024 13:10:06 +0200 Subject: [PATCH] Use generator expression in poolsense (#117582) --- .../components/poolsense/binary_sensor.py | 9 +++------ .../components/poolsense/coordinator.py | 10 ++++++++++ homeassistant/components/poolsense/entity.py | 7 +++---- homeassistant/components/poolsense/sensor.py | 16 ++++------------ 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/poolsense/binary_sensor.py b/homeassistant/components/poolsense/binary_sensor.py index ebbb379cc24..7668845f318 100644 --- a/homeassistant/components/poolsense/binary_sensor.py +++ b/homeassistant/components/poolsense/binary_sensor.py @@ -7,7 +7,6 @@ from homeassistant.components.binary_sensor import ( BinarySensorEntity, BinarySensorEntityDescription, ) -from homeassistant.const import CONF_EMAIL from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -36,12 +35,10 @@ async def async_setup_entry( """Defer sensor setup to the shared sensor module.""" coordinator = config_entry.runtime_data - entities = [ - PoolSenseBinarySensor(coordinator, config_entry.data[CONF_EMAIL], description) + async_add_entities( + PoolSenseBinarySensor(coordinator, description) for description in BINARY_SENSOR_TYPES - ] - - async_add_entities(entities, False) + ) class PoolSenseBinarySensor(PoolSenseEntity, BinarySensorEntity): diff --git a/homeassistant/components/poolsense/coordinator.py b/homeassistant/components/poolsense/coordinator.py index c8842acad98..d9e7e8468ff 100644 --- a/homeassistant/components/poolsense/coordinator.py +++ b/homeassistant/components/poolsense/coordinator.py @@ -1,28 +1,38 @@ """DataUpdateCoordinator for poolsense integration.""" +from __future__ import annotations + import asyncio from datetime import timedelta import logging +from typing import TYPE_CHECKING from poolsense import PoolSense from poolsense.exceptions import PoolSenseError +from homeassistant.const import CONF_EMAIL from homeassistant.core import HomeAssistant from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .const import DOMAIN +if TYPE_CHECKING: + from . import PoolSenseConfigEntry + _LOGGER = logging.getLogger(__name__) class PoolSenseDataUpdateCoordinator(DataUpdateCoordinator[dict[str, StateType]]): """Define an object to hold PoolSense data.""" + config_entry: PoolSenseConfigEntry + def __init__(self, hass: HomeAssistant, poolsense: PoolSense) -> None: """Initialize.""" super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=timedelta(hours=1)) self.poolsense = poolsense + self.email = self.config_entry.data[CONF_EMAIL] async def _async_update_data(self) -> dict[str, StateType]: """Update data via library.""" diff --git a/homeassistant/components/poolsense/entity.py b/homeassistant/components/poolsense/entity.py index 88abe67670a..447c91ceb37 100644 --- a/homeassistant/components/poolsense/entity.py +++ b/homeassistant/components/poolsense/entity.py @@ -17,14 +17,13 @@ class PoolSenseEntity(CoordinatorEntity[PoolSenseDataUpdateCoordinator]): def __init__( self, coordinator: PoolSenseDataUpdateCoordinator, - email: str, description: EntityDescription, ) -> None: - """Initialize poolsense sensor.""" + """Initialize poolsense entity.""" super().__init__(coordinator) self.entity_description = description - self._attr_unique_id = f"{email}-{description.key}" + self._attr_unique_id = f"{coordinator.email}-{description.key}" self._attr_device_info = DeviceInfo( - identifiers={(DOMAIN, email)}, + identifiers={(DOMAIN, coordinator.email)}, model="PoolSense", ) diff --git a/homeassistant/components/poolsense/sensor.py b/homeassistant/components/poolsense/sensor.py index 3b10d9173af..8cfb982d33b 100644 --- a/homeassistant/components/poolsense/sensor.py +++ b/homeassistant/components/poolsense/sensor.py @@ -7,12 +7,7 @@ from homeassistant.components.sensor import ( SensorEntity, SensorEntityDescription, ) -from homeassistant.const import ( - CONF_EMAIL, - PERCENTAGE, - UnitOfElectricPotential, - UnitOfTemperature, -) +from homeassistant.const import PERCENTAGE, UnitOfElectricPotential, UnitOfTemperature from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType @@ -75,12 +70,9 @@ async def async_setup_entry( """Defer sensor setup to the shared sensor module.""" coordinator = config_entry.runtime_data - entities = [ - PoolSenseSensor(coordinator, config_entry.data[CONF_EMAIL], description) - for description in SENSOR_TYPES - ] - - async_add_entities(entities, False) + async_add_entities( + PoolSenseSensor(coordinator, description) for description in SENSOR_TYPES + ) class PoolSenseSensor(PoolSenseEntity, SensorEntity):