From 24e0c0b092f5592d75fd2855fe17cc16ce57e316 Mon Sep 17 00:00:00 2001 From: Keilin Bickar Date: Thu, 3 Mar 2022 15:27:22 -0500 Subject: [PATCH] Add pressure sensor for SleepIQ (#67574) --- homeassistant/components/sleepiq/const.py | 7 +++- homeassistant/components/sleepiq/sensor.py | 15 +++++--- tests/components/sleepiq/conftest.py | 2 ++ tests/components/sleepiq/test_sensor.py | 42 ++++++++++++++++++++-- 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/sleepiq/const.py b/homeassistant/components/sleepiq/const.py index 63e86270925..c1c28a7b5a8 100644 --- a/homeassistant/components/sleepiq/const.py +++ b/homeassistant/components/sleepiq/const.py @@ -9,7 +9,12 @@ ICON_EMPTY = "mdi:bed-empty" ICON_OCCUPIED = "mdi:bed" IS_IN_BED = "is_in_bed" 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" RIGHT = "right" diff --git a/homeassistant/components/sleepiq/sensor.py b/homeassistant/components/sleepiq/sensor.py index cb9bf91cf8a..b101aee8a6e 100644 --- a/homeassistant/components/sleepiq/sensor.py +++ b/homeassistant/components/sleepiq/sensor.py @@ -9,10 +9,12 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback 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 .entity import SleepIQSleeperEntity +SENSORS = [PRESSURE, SLEEP_NUMBER] + async def async_setup_entry( hass: HomeAssistant, @@ -22,13 +24,14 @@ async def async_setup_entry( """Set up the SleepIQ bed sensors.""" data: SleepIQData = hass.data[DOMAIN][entry.entry_id] 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 sleeper in bed.sleepers + for sensor_type in SENSORS ) -class SleepNumberSensorEntity(SleepIQSleeperEntity, SensorEntity): +class SleepIQSensorEntity(SleepIQSleeperEntity, SensorEntity): """Representation of an SleepIQ Entity with CoordinatorEntity.""" _attr_icon = "mdi:bed" @@ -38,11 +41,13 @@ class SleepNumberSensorEntity(SleepIQSleeperEntity, SensorEntity): coordinator: DataUpdateCoordinator, bed: SleepIQBed, sleeper: SleepIQSleeper, + sensor_type: str, ) -> None: """Initialize the sensor.""" - super().__init__(coordinator, bed, sleeper, SLEEP_NUMBER) + self.sensor_type = sensor_type + super().__init__(coordinator, bed, sleeper, sensor_type) @callback def _async_update_attrs(self) -> None: """Update sensor attributes.""" - self._attr_native_value = self.sleeper.sleep_number + self._attr_native_value = getattr(self.sleeper, self.sensor_type) diff --git a/tests/components/sleepiq/conftest.py b/tests/components/sleepiq/conftest.py index caf65a99b3d..5b51b0f4670 100644 --- a/tests/components/sleepiq/conftest.py +++ b/tests/components/sleepiq/conftest.py @@ -48,11 +48,13 @@ def mock_asyncsleepiq(): sleeper_l.name = SLEEPER_L_NAME sleeper_l.in_bed = True sleeper_l.sleep_number = 40 + sleeper_l.pressure = 1000 sleeper_r.side = "R" sleeper_r.name = SLEEPER_R_NAME sleeper_r.in_bed = False sleeper_r.sleep_number = 80 + sleeper_r.pressure = 1400 bed.foundation = create_autospec(SleepIQFoundation) light_1 = create_autospec(SleepIQLight) diff --git a/tests/components/sleepiq/test_sensor.py b/tests/components/sleepiq/test_sensor.py index 26ddc9aa485..c2d8648ebd5 100644 --- a/tests/components/sleepiq/test_sensor.py +++ b/tests/components/sleepiq/test_sensor.py @@ -15,8 +15,8 @@ from tests.components.sleepiq.conftest import ( ) -async def test_sensors(hass, mock_asyncsleepiq): - """Test the SleepIQ binary sensors for a bed with two sides.""" +async def test_sleepnumber_sensors(hass, mock_asyncsleepiq): + """Test the SleepIQ sleepnumber for a bed with two sides.""" entry = await setup_platform(hass, DOMAIN) entity_registry = er.async_get(hass) @@ -51,3 +51,41 @@ async def test_sensors(hass, mock_asyncsleepiq): ) assert entry 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"