1
mirror of https://github.com/home-assistant/core synced 2024-10-04 07:58:43 +02:00

Add pressure sensor for SleepIQ (#67574)

This commit is contained in:
Keilin Bickar 2022-03-03 15:27:22 -05:00 committed by GitHub
parent 423c14e2a1
commit 24e0c0b092
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 8 deletions

View File

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

View File

@ -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)

View File

@ -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)

View File

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