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
This commit is contained in:
RyanClark123
2023-05-16 15:06:01 +01:00
parent dc710a1783
commit 9b3392bdbc
6 changed files with 50 additions and 6 deletions
+17 -1
View File
@@ -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
+1
View File
@@ -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"}
+1 -1
View File
@@ -12,5 +12,5 @@
"requirements": [
"pyowletapi==2023.5.23"
],
"version":"2023.5.1"
"version":"2023.5.2"
}
+30 -1
View File
@@ -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"]