From a2ef0caa07c210413e32705e5d4edbdd20a0a2a9 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sat, 7 Jan 2023 14:13:16 +0100 Subject: [PATCH] Improve `sleepiq` generic typing (#85330) --- .../components/sleepiq/binary_sensor.py | 9 +++++---- homeassistant/components/sleepiq/entity.py | 20 +++++++++++-------- homeassistant/components/sleepiq/light.py | 10 ++++++---- homeassistant/components/sleepiq/number.py | 7 +++---- homeassistant/components/sleepiq/select.py | 10 ++++++---- homeassistant/components/sleepiq/sensor.py | 9 +++++---- homeassistant/components/sleepiq/switch.py | 4 +++- 7 files changed, 40 insertions(+), 29 deletions(-) diff --git a/homeassistant/components/sleepiq/binary_sensor.py b/homeassistant/components/sleepiq/binary_sensor.py index b176320e671..e137edb29ce 100644 --- a/homeassistant/components/sleepiq/binary_sensor.py +++ b/homeassistant/components/sleepiq/binary_sensor.py @@ -8,10 +8,9 @@ from homeassistant.components.binary_sensor import ( from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from .const import DOMAIN, ICON_EMPTY, ICON_OCCUPIED, IS_IN_BED -from .coordinator import SleepIQData +from .coordinator import SleepIQData, SleepIQDataUpdateCoordinator from .entity import SleepIQSleeperEntity @@ -29,14 +28,16 @@ async def async_setup_entry( ) -class IsInBedBinarySensor(SleepIQSleeperEntity, BinarySensorEntity): +class IsInBedBinarySensor( + SleepIQSleeperEntity[SleepIQDataUpdateCoordinator], BinarySensorEntity +): """Implementation of a SleepIQ presence sensor.""" _attr_device_class = BinarySensorDeviceClass.OCCUPANCY def __init__( self, - coordinator: DataUpdateCoordinator, + coordinator: SleepIQDataUpdateCoordinator, bed: SleepIQBed, sleeper: SleepIQSleeper, ) -> None: diff --git a/homeassistant/components/sleepiq/entity.py b/homeassistant/components/sleepiq/entity.py index c73988ce638..3dcfc1784d3 100644 --- a/homeassistant/components/sleepiq/entity.py +++ b/homeassistant/components/sleepiq/entity.py @@ -1,17 +1,21 @@ """Entity for the SleepIQ integration.""" from abc import abstractmethod +from typing import TypeVar, Union from asyncsleepiq import SleepIQBed, SleepIQSleeper from homeassistant.core import callback from homeassistant.helpers import device_registry from homeassistant.helpers.entity import DeviceInfo, Entity -from homeassistant.helpers.update_coordinator import ( - CoordinatorEntity, - DataUpdateCoordinator, -) +from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import ENTITY_TYPES, ICON_OCCUPIED +from .coordinator import SleepIQDataUpdateCoordinator, SleepIQPauseUpdateCoordinator + +_SleepIQCoordinatorT = TypeVar( + "_SleepIQCoordinatorT", + bound=Union[SleepIQDataUpdateCoordinator, SleepIQPauseUpdateCoordinator], +) def device_from_bed(bed: SleepIQBed) -> DeviceInfo: @@ -33,14 +37,14 @@ class SleepIQEntity(Entity): self._attr_device_info = device_from_bed(bed) -class SleepIQBedEntity(CoordinatorEntity): +class SleepIQBedEntity(CoordinatorEntity[_SleepIQCoordinatorT]): """Implementation of a SleepIQ sensor.""" _attr_icon = ICON_OCCUPIED def __init__( self, - coordinator: DataUpdateCoordinator, + coordinator: _SleepIQCoordinatorT, bed: SleepIQBed, ) -> None: """Initialize the SleepIQ sensor entity.""" @@ -61,14 +65,14 @@ class SleepIQBedEntity(CoordinatorEntity): """Update sensor attributes.""" -class SleepIQSleeperEntity(SleepIQBedEntity): +class SleepIQSleeperEntity(SleepIQBedEntity[_SleepIQCoordinatorT]): """Implementation of a SleepIQ sensor.""" _attr_icon = ICON_OCCUPIED def __init__( self, - coordinator: DataUpdateCoordinator, + coordinator: _SleepIQCoordinatorT, bed: SleepIQBed, sleeper: SleepIQSleeper, name: str, diff --git a/homeassistant/components/sleepiq/light.py b/homeassistant/components/sleepiq/light.py index e0b98a37362..e684d383b40 100644 --- a/homeassistant/components/sleepiq/light.py +++ b/homeassistant/components/sleepiq/light.py @@ -8,10 +8,9 @@ from homeassistant.components.light import ColorMode, LightEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from .const import DOMAIN -from .coordinator import SleepIQData +from .coordinator import SleepIQData, SleepIQDataUpdateCoordinator from .entity import SleepIQBedEntity _LOGGER = logging.getLogger(__name__) @@ -31,14 +30,17 @@ async def async_setup_entry( ) -class SleepIQLightEntity(SleepIQBedEntity, LightEntity): +class SleepIQLightEntity(SleepIQBedEntity[SleepIQDataUpdateCoordinator], LightEntity): """Representation of a light.""" _attr_color_mode = ColorMode.ONOFF _attr_supported_color_modes = {ColorMode.ONOFF} def __init__( - self, coordinator: DataUpdateCoordinator, bed: SleepIQBed, light: SleepIQLight + self, + coordinator: SleepIQDataUpdateCoordinator, + bed: SleepIQBed, + light: SleepIQLight, ) -> None: """Initialize the light.""" self.light = light diff --git a/homeassistant/components/sleepiq/number.py b/homeassistant/components/sleepiq/number.py index 1f8ef80a1f1..14af25019b8 100644 --- a/homeassistant/components/sleepiq/number.py +++ b/homeassistant/components/sleepiq/number.py @@ -11,10 +11,9 @@ from homeassistant.components.number import NumberEntity, NumberEntityDescriptio from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from .const import ACTUATOR, DOMAIN, ENTITY_TYPES, FIRMNESS, ICON_OCCUPIED -from .coordinator import SleepIQData +from .coordinator import SleepIQData, SleepIQDataUpdateCoordinator from .entity import SleepIQBedEntity @@ -130,7 +129,7 @@ async def async_setup_entry( async_add_entities(entities) -class SleepIQNumberEntity(SleepIQBedEntity, NumberEntity): +class SleepIQNumberEntity(SleepIQBedEntity[SleepIQDataUpdateCoordinator], NumberEntity): """Representation of a SleepIQ number entity.""" entity_description: SleepIQNumberEntityDescription @@ -138,7 +137,7 @@ class SleepIQNumberEntity(SleepIQBedEntity, NumberEntity): def __init__( self, - coordinator: DataUpdateCoordinator, + coordinator: SleepIQDataUpdateCoordinator, bed: SleepIQBed, device: Any, description: SleepIQNumberEntityDescription, diff --git a/homeassistant/components/sleepiq/select.py b/homeassistant/components/sleepiq/select.py index 0cbf1671e2b..184e57541c6 100644 --- a/homeassistant/components/sleepiq/select.py +++ b/homeassistant/components/sleepiq/select.py @@ -7,10 +7,9 @@ from homeassistant.components.select import SelectEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from .const import DOMAIN -from .coordinator import SleepIQData +from .coordinator import SleepIQData, SleepIQDataUpdateCoordinator from .entity import SleepIQBedEntity @@ -28,13 +27,16 @@ async def async_setup_entry( ) -class SleepIQSelectEntity(SleepIQBedEntity, SelectEntity): +class SleepIQSelectEntity(SleepIQBedEntity[SleepIQDataUpdateCoordinator], SelectEntity): """Representation of a SleepIQ select entity.""" _attr_options = list(BED_PRESETS) def __init__( - self, coordinator: DataUpdateCoordinator, bed: SleepIQBed, preset: SleepIQPreset + self, + coordinator: SleepIQDataUpdateCoordinator, + bed: SleepIQBed, + preset: SleepIQPreset, ) -> None: """Initialize the select entity.""" self.preset = preset diff --git a/homeassistant/components/sleepiq/sensor.py b/homeassistant/components/sleepiq/sensor.py index 71618dab056..c463c80224e 100644 --- a/homeassistant/components/sleepiq/sensor.py +++ b/homeassistant/components/sleepiq/sensor.py @@ -7,10 +7,9 @@ from homeassistant.components.sensor import SensorEntity, SensorStateClass from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from .const import DOMAIN, PRESSURE, SLEEP_NUMBER -from .coordinator import SleepIQData +from .coordinator import SleepIQData, SleepIQDataUpdateCoordinator from .entity import SleepIQSleeperEntity SENSORS = [PRESSURE, SLEEP_NUMBER] @@ -31,14 +30,16 @@ async def async_setup_entry( ) -class SleepIQSensorEntity(SleepIQSleeperEntity, SensorEntity): +class SleepIQSensorEntity( + SleepIQSleeperEntity[SleepIQDataUpdateCoordinator], SensorEntity +): """Representation of an SleepIQ Entity with CoordinatorEntity.""" _attr_icon = "mdi:bed" def __init__( self, - coordinator: DataUpdateCoordinator, + coordinator: SleepIQDataUpdateCoordinator, bed: SleepIQBed, sleeper: SleepIQSleeper, sensor_type: str, diff --git a/homeassistant/components/sleepiq/switch.py b/homeassistant/components/sleepiq/switch.py index ebc0f720b43..62ad72d9db4 100644 --- a/homeassistant/components/sleepiq/switch.py +++ b/homeassistant/components/sleepiq/switch.py @@ -28,7 +28,9 @@ async def async_setup_entry( ) -class SleepNumberPrivateSwitch(SleepIQBedEntity, SwitchEntity): +class SleepNumberPrivateSwitch( + SleepIQBedEntity[SleepIQPauseUpdateCoordinator], SwitchEntity +): """Representation of SleepIQ privacy mode.""" def __init__(