Add generics to Withings (#102770)

This commit is contained in:
Joost Lekkerkerker 2023-10-25 12:00:12 +02:00 committed by GitHub
parent 267721af43
commit 45e4f71d1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 33 deletions

View File

@ -65,13 +65,13 @@ def get_event_name(category: WorkoutCategory) -> str:
return name.replace("_", " ") return name.replace("_", " ")
class WithingsWorkoutCalendarEntity(CalendarEntity, WithingsEntity): class WithingsWorkoutCalendarEntity(
CalendarEntity, WithingsEntity[WithingsWorkoutDataUpdateCoordinator]
):
"""A calendar entity.""" """A calendar entity."""
_attr_translation_key = "workout" _attr_translation_key = "workout"
coordinator: WithingsWorkoutDataUpdateCoordinator
def __init__( def __init__(
self, client: WithingsClient, coordinator: WithingsWorkoutDataUpdateCoordinator self, client: WithingsClient, coordinator: WithingsWorkoutDataUpdateCoordinator
) -> None: ) -> None:

View File

@ -1,21 +1,25 @@
"""Base entity for Withings.""" """Base entity for Withings."""
from __future__ import annotations from __future__ import annotations
from typing import TypeVar
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN from .const import DOMAIN
from .coordinator import WithingsDataUpdateCoordinator from .coordinator import WithingsDataUpdateCoordinator
_T = TypeVar("_T", bound=WithingsDataUpdateCoordinator)
class WithingsEntity(CoordinatorEntity[WithingsDataUpdateCoordinator]):
class WithingsEntity(CoordinatorEntity[_T]):
"""Base class for withings entities.""" """Base class for withings entities."""
_attr_has_entity_name = True _attr_has_entity_name = True
def __init__( def __init__(
self, self,
coordinator: WithingsDataUpdateCoordinator, coordinator: _T,
key: str, key: str,
) -> None: ) -> None:
"""Initialize the Withings entity.""" """Initialize the Withings entity."""

View File

@ -4,6 +4,7 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime from datetime import datetime
from typing import Generic, TypeVar
from aiowithings import ( from aiowithings import (
Activity, Activity,
@ -787,26 +788,33 @@ async def async_setup_entry(
async_add_entities(entities) 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.""" """Implementation of a Withings sensor."""
entity_description: _ED
def __init__( def __init__(
self, self,
coordinator: WithingsDataUpdateCoordinator, coordinator: _T,
entity_description: SensorEntityDescription, entity_description: _ED,
) -> None: ) -> None:
"""Initialize sensor.""" """Initialize sensor."""
super().__init__(coordinator, entity_description.key) super().__init__(coordinator, entity_description.key)
self.entity_description = entity_description self.entity_description = entity_description
class WithingsMeasurementSensor(WithingsSensor): class WithingsMeasurementSensor(
WithingsSensor[
WithingsMeasurementDataUpdateCoordinator,
WithingsMeasurementSensorEntityDescription,
]
):
"""Implementation of a Withings measurement sensor.""" """Implementation of a Withings measurement sensor."""
coordinator: WithingsMeasurementDataUpdateCoordinator
entity_description: WithingsMeasurementSensorEntityDescription
@property @property
def native_value(self) -> float: def native_value(self) -> float:
"""Return the state of the entity.""" """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.""" """Implementation of a Withings sleep sensor."""
coordinator: WithingsSleepDataUpdateCoordinator
entity_description: WithingsSleepSensorEntityDescription
@property @property
def native_value(self) -> StateType: def native_value(self) -> StateType:
"""Return the state of the entity.""" """Return the state of the entity."""
@ -836,13 +845,14 @@ class WithingsSleepSensor(WithingsSensor):
return self.entity_description.value_fn(self.coordinator.data) return self.entity_description.value_fn(self.coordinator.data)
class WithingsGoalsSensor(WithingsSensor): class WithingsGoalsSensor(
WithingsSensor[
WithingsGoalsDataUpdateCoordinator,
WithingsGoalsSensorEntityDescription,
]
):
"""Implementation of a Withings goals sensor.""" """Implementation of a Withings goals sensor."""
coordinator: WithingsGoalsDataUpdateCoordinator
entity_description: WithingsGoalsSensorEntityDescription
@property @property
def native_value(self) -> StateType: def native_value(self) -> StateType:
"""Return the state of the entity.""" """Return the state of the entity."""
@ -850,13 +860,14 @@ class WithingsGoalsSensor(WithingsSensor):
return self.entity_description.value_fn(self.coordinator.data) return self.entity_description.value_fn(self.coordinator.data)
class WithingsActivitySensor(WithingsSensor): class WithingsActivitySensor(
WithingsSensor[
WithingsActivityDataUpdateCoordinator,
WithingsActivitySensorEntityDescription,
]
):
"""Implementation of a Withings activity sensor.""" """Implementation of a Withings activity sensor."""
coordinator: WithingsActivityDataUpdateCoordinator
entity_description: WithingsActivitySensorEntityDescription
@property @property
def native_value(self) -> StateType: def native_value(self) -> StateType:
"""Return the state of the entity.""" """Return the state of the entity."""
@ -870,13 +881,14 @@ class WithingsActivitySensor(WithingsSensor):
return dt_util.start_of_local_day() return dt_util.start_of_local_day()
class WithingsWorkoutSensor(WithingsSensor): class WithingsWorkoutSensor(
WithingsSensor[
WithingsWorkoutDataUpdateCoordinator,
WithingsWorkoutSensorEntityDescription,
]
):
"""Implementation of a Withings workout sensor.""" """Implementation of a Withings workout sensor."""
coordinator: WithingsWorkoutDataUpdateCoordinator
entity_description: WithingsWorkoutSensorEntityDescription
@property @property
def native_value(self) -> StateType: def native_value(self) -> StateType:
"""Return the state of the entity.""" """Return the state of the entity."""