mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add generics to Withings (#102770)
This commit is contained in:
parent
267721af43
commit
45e4f71d1a
@ -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:
|
||||
|
@ -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."""
|
||||
|
@ -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."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user