From 9b3392bdbcd82015ed31d3a50a517e4e22905684 Mon Sep 17 00:00:00 2001 From: RyanClark123 Date: Tue, 16 May 2023 15:06:01 +0100 Subject: [PATCH] Added new sensors ###Feature * Added new sensors, binary sensor awake, is on if baby awake, off otherwise. Sensor sleep state, shows baby sleep state, options are awake, light sleep, deep sleep --- README.md | 2 +- custom_components/owlet/binary_sensor.py | 18 +++++++++++++- custom_components/owlet/const.py | 1 + custom_components/owlet/manifest.json | 2 +- custom_components/owlet/sensor.py | 31 +++++++++++++++++++++++- info.md | 2 -- 6 files changed, 50 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 642990b..dc1d9c0 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ This integration provides the following entities: ## Options -- Seconds between polling - Number of seconds between each call for data from the owlet cloud service, default is 10 seconds. +- Seconds between polling - Number of seconds between each call for data from the owlet cloud service, default is 5 seconds. --- diff --git a/custom_components/owlet/binary_sensor.py b/custom_components/owlet/binary_sensor.py index a356524..0302d14 100644 --- a/custom_components/owlet/binary_sensor.py +++ b/custom_components/owlet/binary_sensor.py @@ -86,6 +86,12 @@ SENSORS: tuple[OwletBinarySensorEntityDescription, ...] = ( device_class=BinarySensorDeviceClass.POWER, element="sock_off", ), + OwletBinarySensorEntityDescription( + key="awake", + name="Awake", + element="sleep_state", + icon="mdi:sleep", + ), ) @@ -119,4 +125,14 @@ class OwletBinarySensor(OwletBaseEntity, BinarySensorEntity): @property def is_on(self) -> bool: """Return true if the binary sensor is on.""" - return self.sock.properties[self.entity_description.element] + state = self.sock.properties[self.entity_description.element] + + if self.entity_description.element == "sleep_state": + if self.sock.properties["charging"]: + return None + if state in [8, 15]: + state = False + else: + state = True + + return state diff --git a/custom_components/owlet/const.py b/custom_components/owlet/const.py index c91d26f..b6789f5 100644 --- a/custom_components/owlet/const.py +++ b/custom_components/owlet/const.py @@ -8,3 +8,4 @@ CONF_OWLET_REFRESH = "refresh" SUPPORTED_VERSIONS = [3] POLLING_INTERVAL = 5 MANUFACTURER = "Owlet Baby Care" +SLEEP_STATES = {1, "Awake", 8, "Light Sleep", 15, "Deep Sleep"} diff --git a/custom_components/owlet/manifest.json b/custom_components/owlet/manifest.json index 3ed5a51..cda4a71 100644 --- a/custom_components/owlet/manifest.json +++ b/custom_components/owlet/manifest.json @@ -12,5 +12,5 @@ "requirements": [ "pyowletapi==2023.5.23" ], - "version":"2023.5.1" + "version":"2023.5.2" } diff --git a/custom_components/owlet/sensor.py b/custom_components/owlet/sensor.py index db0f7c5..a103c12 100644 --- a/custom_components/owlet/sensor.py +++ b/custom_components/owlet/sensor.py @@ -18,7 +18,7 @@ from homeassistant.const import ( UnitOfTemperature, ) -from .const import DOMAIN +from .const import DOMAIN, SLEEP_STATES from .coordinator import OwletCoordinator from .entity import OwletBaseEntity @@ -107,6 +107,7 @@ async def async_setup_entry( coordinator: OwletCoordinator = hass.data[DOMAIN][config_entry.entry_id] entities = [OwletSensor(coordinator, sensor) for sensor in SENSORS] + entities.append(OwletSleepStateSensor(coordinator)) async_add_entities(entities) @@ -142,3 +143,31 @@ class OwletSensor(OwletBaseEntity, SensorEntity): return None return self.sock.properties[self.entity_description.element] + + +class OwletSleepStateSensor(OwletBaseEntity, SensorEntity): + """Representation of an Owlet sleep state sensor.""" + + def __init__( + self, + coordinator: OwletCoordinator, + ) -> None: + """Initialize the sensor.""" + super().__init__(coordinator) + self._attr_unique_id = f"{self.sock.serial}-Sleep State" + self._attr_icon = "mdi:sleep" + self._attr_device_class = SensorDeviceClass.ENUM + self._attr_translation_key = "sleepstate" + self._attr_name = "Sleep State" + + @property + def native_value(self): + """Return sensor value""" + if self.sock.properties["charging"]: + return None + + return SLEEP_STATES[self.sock.properties["sleep_state"]] + + @property + def options(self) -> list[str]: + return ["Awake", "Light Sleep", "Deep Sleep"] diff --git a/info.md b/info.md index 6e442f9..820dc85 100644 --- a/info.md +++ b/info.md @@ -19,8 +19,6 @@ If you have a smart sock 2 and would like to contribute then please do so. 3. Hard refresh browser cache. 4. [![Add Integration][add-integration-badge]][add-integration] or in the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Owlet Smart Sock". -{% endif %} - ---