From 45e4f71d1ac5b9202061280ab4bf817ac5ad6633 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Wed, 25 Oct 2023 12:00:12 +0200 Subject: [PATCH] Add generics to Withings (#102770) --- homeassistant/components/withings/calendar.py | 6 +- homeassistant/components/withings/entity.py | 8 ++- homeassistant/components/withings/sensor.py | 68 +++++++++++-------- 3 files changed, 49 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/withings/calendar.py b/homeassistant/components/withings/calendar.py index 3ee2c7dae59..19572682d1a 100644 --- a/homeassistant/components/withings/calendar.py +++ b/homeassistant/components/withings/calendar.py @@ -65,13 +65,13 @@ def get_event_name(category: WorkoutCategory) -> str: return name.replace("_", " ") -class WithingsWorkoutCalendarEntity(CalendarEntity, WithingsEntity): +class WithingsWorkoutCalendarEntity( + CalendarEntity, WithingsEntity[WithingsWorkoutDataUpdateCoordinator] +): """A calendar entity.""" _attr_translation_key = "workout" - coordinator: WithingsWorkoutDataUpdateCoordinator - def __init__( self, client: WithingsClient, coordinator: WithingsWorkoutDataUpdateCoordinator ) -> None: diff --git a/homeassistant/components/withings/entity.py b/homeassistant/components/withings/entity.py index 8d2c815b340..7f3e694533c 100644 --- a/homeassistant/components/withings/entity.py +++ b/homeassistant/components/withings/entity.py @@ -1,21 +1,25 @@ """Base entity for Withings.""" from __future__ import annotations +from typing import TypeVar + from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DOMAIN from .coordinator import WithingsDataUpdateCoordinator +_T = TypeVar("_T", bound=WithingsDataUpdateCoordinator) -class WithingsEntity(CoordinatorEntity[WithingsDataUpdateCoordinator]): + +class WithingsEntity(CoordinatorEntity[_T]): """Base class for withings entities.""" _attr_has_entity_name = True def __init__( self, - coordinator: WithingsDataUpdateCoordinator, + coordinator: _T, key: str, ) -> None: """Initialize the Withings entity.""" diff --git a/homeassistant/components/withings/sensor.py b/homeassistant/components/withings/sensor.py index 4729671fa3b..fe4db35cf99 100644 --- a/homeassistant/components/withings/sensor.py +++ b/homeassistant/components/withings/sensor.py @@ -4,6 +4,7 @@ from __future__ import annotations from collections.abc import Callable from dataclasses import dataclass from datetime import datetime +from typing import Generic, TypeVar from aiowithings import ( Activity, @@ -787,26 +788,33 @@ async def async_setup_entry( async_add_entities(entities) -class WithingsSensor(WithingsEntity, SensorEntity): +_T = TypeVar("_T", bound=WithingsDataUpdateCoordinator) +_ED = TypeVar("_ED", bound=SensorEntityDescription) + + +class WithingsSensor(WithingsEntity[_T], SensorEntity, Generic[_T, _ED]): """Implementation of a Withings sensor.""" + entity_description: _ED + def __init__( self, - coordinator: WithingsDataUpdateCoordinator, - entity_description: SensorEntityDescription, + coordinator: _T, + entity_description: _ED, ) -> None: """Initialize sensor.""" super().__init__(coordinator, entity_description.key) self.entity_description = entity_description -class WithingsMeasurementSensor(WithingsSensor): +class WithingsMeasurementSensor( + WithingsSensor[ + WithingsMeasurementDataUpdateCoordinator, + WithingsMeasurementSensorEntityDescription, + ] +): """Implementation of a Withings measurement sensor.""" - coordinator: WithingsMeasurementDataUpdateCoordinator - - entity_description: WithingsMeasurementSensorEntityDescription - @property def native_value(self) -> float: """Return the state of the entity.""" @@ -821,13 +829,14 @@ class WithingsMeasurementSensor(WithingsSensor): ) -class WithingsSleepSensor(WithingsSensor): +class WithingsSleepSensor( + WithingsSensor[ + WithingsSleepDataUpdateCoordinator, + WithingsSleepSensorEntityDescription, + ] +): """Implementation of a Withings sleep sensor.""" - coordinator: WithingsSleepDataUpdateCoordinator - - entity_description: WithingsSleepSensorEntityDescription - @property def native_value(self) -> StateType: """Return the state of the entity.""" @@ -836,13 +845,14 @@ class WithingsSleepSensor(WithingsSensor): return self.entity_description.value_fn(self.coordinator.data) -class WithingsGoalsSensor(WithingsSensor): +class WithingsGoalsSensor( + WithingsSensor[ + WithingsGoalsDataUpdateCoordinator, + WithingsGoalsSensorEntityDescription, + ] +): """Implementation of a Withings goals sensor.""" - coordinator: WithingsGoalsDataUpdateCoordinator - - entity_description: WithingsGoalsSensorEntityDescription - @property def native_value(self) -> StateType: """Return the state of the entity.""" @@ -850,13 +860,14 @@ class WithingsGoalsSensor(WithingsSensor): return self.entity_description.value_fn(self.coordinator.data) -class WithingsActivitySensor(WithingsSensor): +class WithingsActivitySensor( + WithingsSensor[ + WithingsActivityDataUpdateCoordinator, + WithingsActivitySensorEntityDescription, + ] +): """Implementation of a Withings activity sensor.""" - coordinator: WithingsActivityDataUpdateCoordinator - - entity_description: WithingsActivitySensorEntityDescription - @property def native_value(self) -> StateType: """Return the state of the entity.""" @@ -870,13 +881,14 @@ class WithingsActivitySensor(WithingsSensor): return dt_util.start_of_local_day() -class WithingsWorkoutSensor(WithingsSensor): +class WithingsWorkoutSensor( + WithingsSensor[ + WithingsWorkoutDataUpdateCoordinator, + WithingsWorkoutSensorEntityDescription, + ] +): """Implementation of a Withings workout sensor.""" - coordinator: WithingsWorkoutDataUpdateCoordinator - - entity_description: WithingsWorkoutSensorEntityDescription - @property def native_value(self) -> StateType: """Return the state of the entity."""