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("_", " ")
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:

View File

@ -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."""

View File

@ -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."""