mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Add pressure sensor for SleepIQ (#67574)
This commit is contained in:
parent
423c14e2a1
commit
24e0c0b092
@ -9,7 +9,12 @@ ICON_EMPTY = "mdi:bed-empty"
|
|||||||
ICON_OCCUPIED = "mdi:bed"
|
ICON_OCCUPIED = "mdi:bed"
|
||||||
IS_IN_BED = "is_in_bed"
|
IS_IN_BED = "is_in_bed"
|
||||||
SLEEP_NUMBER = "sleep_number"
|
SLEEP_NUMBER = "sleep_number"
|
||||||
SENSOR_TYPES = {SLEEP_NUMBER: "SleepNumber", IS_IN_BED: "Is In Bed"}
|
PRESSURE = "pressure"
|
||||||
|
SENSOR_TYPES = {
|
||||||
|
SLEEP_NUMBER: "SleepNumber",
|
||||||
|
IS_IN_BED: "Is In Bed",
|
||||||
|
PRESSURE: "Pressure",
|
||||||
|
}
|
||||||
|
|
||||||
LEFT = "left"
|
LEFT = "left"
|
||||||
RIGHT = "right"
|
RIGHT = "right"
|
||||||
|
@ -9,10 +9,12 @@ from homeassistant.core import HomeAssistant, callback
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||||
|
|
||||||
from .const import DOMAIN, SLEEP_NUMBER
|
from .const import DOMAIN, PRESSURE, SLEEP_NUMBER
|
||||||
from .coordinator import SleepIQData
|
from .coordinator import SleepIQData
|
||||||
from .entity import SleepIQSleeperEntity
|
from .entity import SleepIQSleeperEntity
|
||||||
|
|
||||||
|
SENSORS = [PRESSURE, SLEEP_NUMBER]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
@ -22,13 +24,14 @@ async def async_setup_entry(
|
|||||||
"""Set up the SleepIQ bed sensors."""
|
"""Set up the SleepIQ bed sensors."""
|
||||||
data: SleepIQData = hass.data[DOMAIN][entry.entry_id]
|
data: SleepIQData = hass.data[DOMAIN][entry.entry_id]
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
SleepNumberSensorEntity(data.data_coordinator, bed, sleeper)
|
SleepIQSensorEntity(data.data_coordinator, bed, sleeper, sensor_type)
|
||||||
for bed in data.client.beds.values()
|
for bed in data.client.beds.values()
|
||||||
for sleeper in bed.sleepers
|
for sleeper in bed.sleepers
|
||||||
|
for sensor_type in SENSORS
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class SleepNumberSensorEntity(SleepIQSleeperEntity, SensorEntity):
|
class SleepIQSensorEntity(SleepIQSleeperEntity, SensorEntity):
|
||||||
"""Representation of an SleepIQ Entity with CoordinatorEntity."""
|
"""Representation of an SleepIQ Entity with CoordinatorEntity."""
|
||||||
|
|
||||||
_attr_icon = "mdi:bed"
|
_attr_icon = "mdi:bed"
|
||||||
@ -38,11 +41,13 @@ class SleepNumberSensorEntity(SleepIQSleeperEntity, SensorEntity):
|
|||||||
coordinator: DataUpdateCoordinator,
|
coordinator: DataUpdateCoordinator,
|
||||||
bed: SleepIQBed,
|
bed: SleepIQBed,
|
||||||
sleeper: SleepIQSleeper,
|
sleeper: SleepIQSleeper,
|
||||||
|
sensor_type: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator, bed, sleeper, SLEEP_NUMBER)
|
self.sensor_type = sensor_type
|
||||||
|
super().__init__(coordinator, bed, sleeper, sensor_type)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_update_attrs(self) -> None:
|
def _async_update_attrs(self) -> None:
|
||||||
"""Update sensor attributes."""
|
"""Update sensor attributes."""
|
||||||
self._attr_native_value = self.sleeper.sleep_number
|
self._attr_native_value = getattr(self.sleeper, self.sensor_type)
|
||||||
|
@ -48,11 +48,13 @@ def mock_asyncsleepiq():
|
|||||||
sleeper_l.name = SLEEPER_L_NAME
|
sleeper_l.name = SLEEPER_L_NAME
|
||||||
sleeper_l.in_bed = True
|
sleeper_l.in_bed = True
|
||||||
sleeper_l.sleep_number = 40
|
sleeper_l.sleep_number = 40
|
||||||
|
sleeper_l.pressure = 1000
|
||||||
|
|
||||||
sleeper_r.side = "R"
|
sleeper_r.side = "R"
|
||||||
sleeper_r.name = SLEEPER_R_NAME
|
sleeper_r.name = SLEEPER_R_NAME
|
||||||
sleeper_r.in_bed = False
|
sleeper_r.in_bed = False
|
||||||
sleeper_r.sleep_number = 80
|
sleeper_r.sleep_number = 80
|
||||||
|
sleeper_r.pressure = 1400
|
||||||
|
|
||||||
bed.foundation = create_autospec(SleepIQFoundation)
|
bed.foundation = create_autospec(SleepIQFoundation)
|
||||||
light_1 = create_autospec(SleepIQLight)
|
light_1 = create_autospec(SleepIQLight)
|
||||||
|
@ -15,8 +15,8 @@ from tests.components.sleepiq.conftest import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_sensors(hass, mock_asyncsleepiq):
|
async def test_sleepnumber_sensors(hass, mock_asyncsleepiq):
|
||||||
"""Test the SleepIQ binary sensors for a bed with two sides."""
|
"""Test the SleepIQ sleepnumber for a bed with two sides."""
|
||||||
entry = await setup_platform(hass, DOMAIN)
|
entry = await setup_platform(hass, DOMAIN)
|
||||||
entity_registry = er.async_get(hass)
|
entity_registry = er.async_get(hass)
|
||||||
|
|
||||||
@ -51,3 +51,41 @@ async def test_sensors(hass, mock_asyncsleepiq):
|
|||||||
)
|
)
|
||||||
assert entry
|
assert entry
|
||||||
assert entry.unique_id == f"{BED_ID}_{SLEEPER_R_NAME}_sleep_number"
|
assert entry.unique_id == f"{BED_ID}_{SLEEPER_R_NAME}_sleep_number"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_pressure_sensors(hass, mock_asyncsleepiq):
|
||||||
|
"""Test the SleepIQ pressure for a bed with two sides."""
|
||||||
|
entry = await setup_platform(hass, DOMAIN)
|
||||||
|
entity_registry = er.async_get(hass)
|
||||||
|
|
||||||
|
state = hass.states.get(
|
||||||
|
f"sensor.sleepnumber_{BED_NAME_LOWER}_{SLEEPER_L_NAME_LOWER}_pressure"
|
||||||
|
)
|
||||||
|
assert state.state == "1000"
|
||||||
|
assert state.attributes.get(ATTR_ICON) == "mdi:bed"
|
||||||
|
assert (
|
||||||
|
state.attributes.get(ATTR_FRIENDLY_NAME)
|
||||||
|
== f"SleepNumber {BED_NAME} {SLEEPER_L_NAME} Pressure"
|
||||||
|
)
|
||||||
|
|
||||||
|
entry = entity_registry.async_get(
|
||||||
|
f"sensor.sleepnumber_{BED_NAME_LOWER}_{SLEEPER_L_NAME_LOWER}_pressure"
|
||||||
|
)
|
||||||
|
assert entry
|
||||||
|
assert entry.unique_id == f"{BED_ID}_{SLEEPER_L_NAME}_pressure"
|
||||||
|
|
||||||
|
state = hass.states.get(
|
||||||
|
f"sensor.sleepnumber_{BED_NAME_LOWER}_{SLEEPER_R_NAME_LOWER}_pressure"
|
||||||
|
)
|
||||||
|
assert state.state == "1400"
|
||||||
|
assert state.attributes.get(ATTR_ICON) == "mdi:bed"
|
||||||
|
assert (
|
||||||
|
state.attributes.get(ATTR_FRIENDLY_NAME)
|
||||||
|
== f"SleepNumber {BED_NAME} {SLEEPER_R_NAME} Pressure"
|
||||||
|
)
|
||||||
|
|
||||||
|
entry = entity_registry.async_get(
|
||||||
|
f"sensor.sleepnumber_{BED_NAME_LOWER}_{SLEEPER_R_NAME_LOWER}_pressure"
|
||||||
|
)
|
||||||
|
assert entry
|
||||||
|
assert entry.unique_id == f"{BED_ID}_{SLEEPER_R_NAME}_pressure"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user