mirror of
https://github.com/Electric-Special/ha-core.git
synced 2026-03-21 05:06:13 +01:00
Migrate SleepIQ sensors to entity descriptions (#163213)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
"""Support for SleepIQ Sensor."""
|
||||
"""Support for SleepIQ sensors."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
|
||||
from asyncsleepiq import SleepIQBed, SleepIQSleeper
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity, SensorStateClass
|
||||
from homeassistant.components.sensor import (
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
@@ -13,7 +20,28 @@ from .const import DOMAIN, PRESSURE, SLEEP_NUMBER
|
||||
from .coordinator import SleepIQData, SleepIQDataUpdateCoordinator
|
||||
from .entity import SleepIQSleeperEntity
|
||||
|
||||
SENSORS = [PRESSURE, SLEEP_NUMBER]
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class SleepIQSensorEntityDescription(SensorEntityDescription):
|
||||
"""Describes SleepIQ sensor entity."""
|
||||
|
||||
value_fn: Callable[[SleepIQSleeper], float | int | None]
|
||||
|
||||
|
||||
SENSORS: tuple[SleepIQSensorEntityDescription, ...] = (
|
||||
SleepIQSensorEntityDescription(
|
||||
key=PRESSURE,
|
||||
translation_key="pressure",
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value_fn=lambda sleeper: sleeper.pressure,
|
||||
),
|
||||
SleepIQSensorEntityDescription(
|
||||
key=SLEEP_NUMBER,
|
||||
translation_key="sleep_number",
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value_fn=lambda sleeper: sleeper.sleep_number,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
@@ -24,33 +52,32 @@ async def async_setup_entry(
|
||||
"""Set up the SleepIQ bed sensors."""
|
||||
data: SleepIQData = hass.data[DOMAIN][entry.entry_id]
|
||||
async_add_entities(
|
||||
SleepIQSensorEntity(data.data_coordinator, bed, sleeper, sensor_type)
|
||||
SleepIQSensorEntity(data.data_coordinator, bed, sleeper, description)
|
||||
for bed in data.client.beds.values()
|
||||
for sleeper in bed.sleepers
|
||||
for sensor_type in SENSORS
|
||||
for description in SENSORS
|
||||
)
|
||||
|
||||
|
||||
class SleepIQSensorEntity(
|
||||
SleepIQSleeperEntity[SleepIQDataUpdateCoordinator], SensorEntity
|
||||
):
|
||||
"""Representation of an SleepIQ Entity with CoordinatorEntity."""
|
||||
"""Representation of a SleepIQ sensor."""
|
||||
|
||||
_attr_icon = "mdi:bed"
|
||||
entity_description: SleepIQSensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: SleepIQDataUpdateCoordinator,
|
||||
bed: SleepIQBed,
|
||||
sleeper: SleepIQSleeper,
|
||||
sensor_type: str,
|
||||
description: SleepIQSensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
self.sensor_type = sensor_type
|
||||
self._attr_state_class = SensorStateClass.MEASUREMENT
|
||||
super().__init__(coordinator, bed, sleeper, sensor_type)
|
||||
self.entity_description = description
|
||||
super().__init__(coordinator, bed, sleeper, description.key)
|
||||
|
||||
@callback
|
||||
def _async_update_attrs(self) -> None:
|
||||
"""Update sensor attributes."""
|
||||
self._attr_native_value = getattr(self.sleeper, self.sensor_type)
|
||||
self._attr_native_value = self.entity_description.value_fn(self.sleeper)
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': None,
|
||||
'translation_key': 'pressure',
|
||||
'unique_id': '43219_pressure',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
@@ -85,7 +85,7 @@
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': None,
|
||||
'translation_key': 'sleep_number',
|
||||
'unique_id': '43219_sleep_number',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
@@ -138,7 +138,7 @@
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': None,
|
||||
'translation_key': 'pressure',
|
||||
'unique_id': '98765_pressure',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
@@ -191,7 +191,7 @@
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': None,
|
||||
'translation_key': 'sleep_number',
|
||||
'unique_id': '98765_sleep_number',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user