5e17ecdeb2
Just a suggestion! The O2 Sat 10m average reports `255%` when not charging, and before it can calculate the 10m average. Suggest changing to report `None` until the average can be displayed. This way the graphs/data aren't skewed by the 255 value.
174 lines
5.3 KiB
Python
174 lines
5.3 KiB
Python
"""Support for Owlet sensors."""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from homeassistant.components.sensor import (
|
|
SensorDeviceClass,
|
|
SensorEntity,
|
|
SensorEntityDescription,
|
|
SensorStateClass,
|
|
)
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import (
|
|
PERCENTAGE,
|
|
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
|
UnitOfTemperature,
|
|
UnitOfTime,
|
|
)
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.typing import StateType
|
|
|
|
from .const import DOMAIN, SLEEP_STATES
|
|
from .coordinator import OwletCoordinator
|
|
from .entity import OwletBaseEntity
|
|
|
|
|
|
@dataclass
|
|
class OwletSensorEntityDescription(SensorEntityDescription):
|
|
"""Represent the owlet sensor entity description."""
|
|
|
|
|
|
SENSORS: tuple[OwletSensorEntityDescription, ...] = (
|
|
OwletSensorEntityDescription(
|
|
key="battery_percentage",
|
|
translation_key="batterypercent",
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
device_class=SensorDeviceClass.BATTERY,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
),
|
|
OwletSensorEntityDescription(
|
|
key="oxygen_saturation",
|
|
translation_key="o2saturation",
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
icon="mdi:leaf",
|
|
),
|
|
OwletSensorEntityDescription(
|
|
key="heart_rate",
|
|
translation_key="heartrate",
|
|
native_unit_of_measurement="bpm",
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
icon="mdi:heart-pulse",
|
|
),
|
|
OwletSensorEntityDescription(
|
|
key="battery_minutes",
|
|
translation_key="batterymin",
|
|
native_unit_of_measurement=UnitOfTime.MINUTES,
|
|
device_class=SensorDeviceClass.DURATION,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
),
|
|
OwletSensorEntityDescription(
|
|
key="signal_strength",
|
|
translation_key="signalstrength",
|
|
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
|
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
),
|
|
OwletSensorEntityDescription(
|
|
key="skin_temperature",
|
|
translation_key="skintemp",
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
),
|
|
OwletSensorEntityDescription(
|
|
key="sleep_state",
|
|
translation_key="sleepstate",
|
|
device_class=SensorDeviceClass.ENUM,
|
|
),
|
|
OwletSensorEntityDescription(
|
|
key="movement",
|
|
translation_key="movement",
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
icon="mdi:cursor-move",
|
|
entity_registry_enabled_default=False,
|
|
),
|
|
OwletSensorEntityDescription(
|
|
key="oxygen_10_av",
|
|
translation_key="o2saturation10a",
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
icon="mdi:leaf",
|
|
),
|
|
OwletSensorEntityDescription(
|
|
key="movement_bucket",
|
|
translation_key="movementbucket",
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
icon="mdi:bucket-outline",
|
|
entity_registry_enabled_default=False,
|
|
),
|
|
)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
config_entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Set up the owlet sensors from config entry."""
|
|
|
|
coordinators: list[OwletCoordinator] = list(
|
|
hass.data[DOMAIN][config_entry.entry_id].values()
|
|
)
|
|
|
|
sensors = []
|
|
|
|
for coordinator in coordinators:
|
|
for sensor in SENSORS:
|
|
if sensor.key in coordinator.sock.properties:
|
|
sensors.append(OwletSensor(coordinator, sensor))
|
|
|
|
async_add_entities(sensors)
|
|
|
|
|
|
class OwletSensor(OwletBaseEntity, SensorEntity):
|
|
"""Representation of an Owlet sensor."""
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: OwletCoordinator,
|
|
description: OwletSensorEntityDescription,
|
|
) -> None:
|
|
"""Initialize the sensor."""
|
|
super().__init__(coordinator)
|
|
self.entity_description: OwletSensorEntityDescription = description
|
|
self._attr_unique_id = f"{self.sock.serial}-{description.key}"
|
|
|
|
@property
|
|
def native_value(self) -> StateType:
|
|
"""Return sensor value."""
|
|
|
|
if (
|
|
self.entity_description.key
|
|
in [
|
|
"heart_rate",
|
|
"battery_minutes",
|
|
"oxygen_saturation",
|
|
"skin_temperature",
|
|
"oxygen_10_av",
|
|
"sleep_state",
|
|
]
|
|
and self.sock.properties["charging"]
|
|
):
|
|
return None
|
|
|
|
if self.entity_description.key == "sleep_state":
|
|
return SLEEP_STATES[self.sock.properties["sleep_state"]]
|
|
|
|
if self.entity_description.key == "oxygen_10_av":
|
|
val = self.sock.properties[self.entity_description.key]
|
|
if val is None or not isinstance(val, (int, float)) or val < 0 or val > 100:
|
|
return None
|
|
return val
|
|
|
|
return self.sock.properties[self.entity_description.key]
|
|
|
|
@property
|
|
def options(self) -> list[str] | None:
|
|
"""Set options for sleep state."""
|
|
if self.entity_description.key != "sleep_state":
|
|
return None
|
|
return list(SLEEP_STATES.values())
|