2019-08-31 12:30:59 +00:00
|
|
|
"""Sensors flow for Withings."""
|
2019-10-24 16:41:04 +00:00
|
|
|
from typing import Callable, List, Union
|
|
|
|
|
|
|
|
from withings_api.common import (
|
|
|
|
GetSleepSummaryField,
|
|
|
|
MeasureGetMeasResponse,
|
2019-12-10 20:54:50 +00:00
|
|
|
MeasureGroupAttribs,
|
|
|
|
MeasureType,
|
2019-10-24 16:41:04 +00:00
|
|
|
SleepGetResponse,
|
|
|
|
SleepGetSummaryResponse,
|
|
|
|
SleepState,
|
2019-12-10 20:54:50 +00:00
|
|
|
get_measure_value,
|
2019-10-24 16:41:04 +00:00
|
|
|
)
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-02-25 01:52:14 +00:00
|
|
|
from homeassistant.const import MASS_KILOGRAMS, SPEED_METERS_PER_SECOND, TIME_SECONDS
|
2019-10-24 16:41:04 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-12-10 20:54:50 +00:00
|
|
|
from homeassistant.helpers import config_entry_oauth2_flow
|
2019-08-31 12:30:59 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.util import slugify
|
|
|
|
|
|
|
|
from . import const
|
|
|
|
from .common import _LOGGER, WithingsDataManager, get_data_manager
|
|
|
|
|
|
|
|
# There's only 3 calls (per profile) made to the withings api every 5
|
|
|
|
# minutes (see throttle values). This component wouldn't benefit
|
|
|
|
# much from parallel updates.
|
|
|
|
PARALLEL_UPDATES = 1
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
2019-10-24 16:41:04 +00:00
|
|
|
hass: HomeAssistant,
|
2019-08-31 12:30:59 +00:00
|
|
|
entry: ConfigEntry,
|
2019-10-24 16:41:04 +00:00
|
|
|
async_add_entities: Callable[[List[Entity], bool], None],
|
|
|
|
) -> None:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Set up the sensor config entry."""
|
2019-10-24 16:41:04 +00:00
|
|
|
implementation = await config_entry_oauth2_flow.async_get_config_entry_implementation(
|
|
|
|
hass, entry
|
|
|
|
)
|
2019-08-31 12:30:59 +00:00
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
data_manager = get_data_manager(hass, entry, implementation)
|
|
|
|
user_id = entry.data["token"]["userid"]
|
2019-08-31 12:30:59 +00:00
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
entities = create_sensor_entities(data_manager, user_id)
|
|
|
|
async_add_entities(entities, True)
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WithingsAttribute:
|
|
|
|
"""Base class for modeling withing data."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
measurement: str,
|
|
|
|
measure_type,
|
|
|
|
friendly_name: str,
|
|
|
|
unit_of_measurement: str,
|
|
|
|
icon: str,
|
|
|
|
) -> None:
|
2019-12-10 22:25:06 +00:00
|
|
|
"""Initialize attribute."""
|
2019-08-31 12:30:59 +00:00
|
|
|
self.measurement = measurement
|
|
|
|
self.measure_type = measure_type
|
|
|
|
self.friendly_name = friendly_name
|
|
|
|
self.unit_of_measurement = unit_of_measurement
|
|
|
|
self.icon = icon
|
|
|
|
|
|
|
|
|
|
|
|
class WithingsMeasureAttribute(WithingsAttribute):
|
|
|
|
"""Model measure attributes."""
|
|
|
|
|
|
|
|
|
|
|
|
class WithingsSleepStateAttribute(WithingsAttribute):
|
|
|
|
"""Model sleep data attributes."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, measurement: str, friendly_name: str, unit_of_measurement: str, icon: str
|
|
|
|
) -> None:
|
2019-12-10 22:25:06 +00:00
|
|
|
"""Initialize sleep state attribute."""
|
2019-08-31 12:30:59 +00:00
|
|
|
super().__init__(measurement, None, friendly_name, unit_of_measurement, icon)
|
|
|
|
|
|
|
|
|
|
|
|
class WithingsSleepSummaryAttribute(WithingsAttribute):
|
|
|
|
"""Models sleep summary attributes."""
|
|
|
|
|
|
|
|
|
|
|
|
WITHINGS_ATTRIBUTES = [
|
|
|
|
WithingsMeasureAttribute(
|
|
|
|
const.MEAS_WEIGHT_KG,
|
2019-10-24 16:41:04 +00:00
|
|
|
MeasureType.WEIGHT,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Weight",
|
2020-02-25 01:52:14 +00:00
|
|
|
MASS_KILOGRAMS,
|
2019-08-31 12:30:59 +00:00
|
|
|
"mdi:weight-kilogram",
|
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
|
|
|
const.MEAS_FAT_MASS_KG,
|
2019-10-24 16:41:04 +00:00
|
|
|
MeasureType.FAT_MASS_WEIGHT,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Fat Mass",
|
2020-02-25 01:52:14 +00:00
|
|
|
MASS_KILOGRAMS,
|
2019-08-31 12:30:59 +00:00
|
|
|
"mdi:weight-kilogram",
|
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
|
|
|
const.MEAS_FAT_FREE_MASS_KG,
|
2019-10-24 16:41:04 +00:00
|
|
|
MeasureType.FAT_FREE_MASS,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Fat Free Mass",
|
2020-02-25 01:52:14 +00:00
|
|
|
MASS_KILOGRAMS,
|
2019-08-31 12:30:59 +00:00
|
|
|
"mdi:weight-kilogram",
|
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
|
|
|
const.MEAS_MUSCLE_MASS_KG,
|
2019-10-24 16:41:04 +00:00
|
|
|
MeasureType.MUSCLE_MASS,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Muscle Mass",
|
2020-02-25 01:52:14 +00:00
|
|
|
MASS_KILOGRAMS,
|
2019-08-31 12:30:59 +00:00
|
|
|
"mdi:weight-kilogram",
|
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
|
|
|
const.MEAS_BONE_MASS_KG,
|
2019-10-24 16:41:04 +00:00
|
|
|
MeasureType.BONE_MASS,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Bone Mass",
|
2020-02-25 01:52:14 +00:00
|
|
|
MASS_KILOGRAMS,
|
2019-08-31 12:30:59 +00:00
|
|
|
"mdi:weight-kilogram",
|
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
|
|
|
const.MEAS_HEIGHT_M,
|
2019-10-24 16:41:04 +00:00
|
|
|
MeasureType.HEIGHT,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Height",
|
|
|
|
const.UOM_LENGTH_M,
|
|
|
|
"mdi:ruler",
|
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
|
|
|
const.MEAS_TEMP_C,
|
2019-10-24 16:41:04 +00:00
|
|
|
MeasureType.TEMPERATURE,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Temperature",
|
|
|
|
const.UOM_TEMP_C,
|
|
|
|
"mdi:thermometer",
|
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
|
|
|
const.MEAS_BODY_TEMP_C,
|
2019-10-24 16:41:04 +00:00
|
|
|
MeasureType.BODY_TEMPERATURE,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Body Temperature",
|
|
|
|
const.UOM_TEMP_C,
|
|
|
|
"mdi:thermometer",
|
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
|
|
|
const.MEAS_SKIN_TEMP_C,
|
2019-10-24 16:41:04 +00:00
|
|
|
MeasureType.SKIN_TEMPERATURE,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Skin Temperature",
|
|
|
|
const.UOM_TEMP_C,
|
|
|
|
"mdi:thermometer",
|
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
|
|
|
const.MEAS_FAT_RATIO_PCT,
|
2019-10-24 16:41:04 +00:00
|
|
|
MeasureType.FAT_RATIO,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Fat Ratio",
|
|
|
|
const.UOM_PERCENT,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
|
|
|
const.MEAS_DIASTOLIC_MMHG,
|
2019-10-24 16:41:04 +00:00
|
|
|
MeasureType.DIASTOLIC_BLOOD_PRESSURE,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Diastolic Blood Pressure",
|
|
|
|
const.UOM_MMHG,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
|
|
|
const.MEAS_SYSTOLIC_MMGH,
|
2019-10-24 16:41:04 +00:00
|
|
|
MeasureType.SYSTOLIC_BLOOD_PRESSURE,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Systolic Blood Pressure",
|
|
|
|
const.UOM_MMHG,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
|
|
|
const.MEAS_HEART_PULSE_BPM,
|
2019-10-24 16:41:04 +00:00
|
|
|
MeasureType.HEART_RATE,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Heart Pulse",
|
|
|
|
const.UOM_BEATS_PER_MINUTE,
|
|
|
|
"mdi:heart-pulse",
|
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
2019-10-24 16:41:04 +00:00
|
|
|
const.MEAS_SPO2_PCT, MeasureType.SP02, "SP02", const.UOM_PERCENT, None
|
2019-08-31 12:30:59 +00:00
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
2020-01-12 01:58:06 +00:00
|
|
|
const.MEAS_HYDRATION,
|
|
|
|
MeasureType.HYDRATION,
|
|
|
|
"Hydration",
|
|
|
|
const.UOM_PERCENT,
|
|
|
|
"mdi:water",
|
2019-08-31 12:30:59 +00:00
|
|
|
),
|
|
|
|
WithingsMeasureAttribute(
|
|
|
|
const.MEAS_PWV,
|
2019-10-24 16:41:04 +00:00
|
|
|
MeasureType.PULSE_WAVE_VELOCITY,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Pulse Wave Velocity",
|
2020-02-25 01:52:14 +00:00
|
|
|
SPEED_METERS_PER_SECOND,
|
2019-08-31 12:30:59 +00:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
WithingsSleepStateAttribute(
|
|
|
|
const.MEAS_SLEEP_STATE, "Sleep state", None, "mdi:sleep"
|
|
|
|
),
|
|
|
|
WithingsSleepSummaryAttribute(
|
|
|
|
const.MEAS_SLEEP_WAKEUP_DURATION_SECONDS,
|
2019-10-24 16:41:04 +00:00
|
|
|
GetSleepSummaryField.WAKEUP_DURATION.value,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Wakeup time",
|
2020-02-23 20:09:24 +00:00
|
|
|
TIME_SECONDS,
|
2019-08-31 12:30:59 +00:00
|
|
|
"mdi:sleep-off",
|
|
|
|
),
|
|
|
|
WithingsSleepSummaryAttribute(
|
|
|
|
const.MEAS_SLEEP_LIGHT_DURATION_SECONDS,
|
2019-10-24 16:41:04 +00:00
|
|
|
GetSleepSummaryField.LIGHT_SLEEP_DURATION.value,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Light sleep",
|
2020-02-23 20:09:24 +00:00
|
|
|
TIME_SECONDS,
|
2019-08-31 12:30:59 +00:00
|
|
|
"mdi:sleep",
|
|
|
|
),
|
|
|
|
WithingsSleepSummaryAttribute(
|
|
|
|
const.MEAS_SLEEP_DEEP_DURATION_SECONDS,
|
2019-10-24 16:41:04 +00:00
|
|
|
GetSleepSummaryField.DEEP_SLEEP_DURATION.value,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Deep sleep",
|
2020-02-23 20:09:24 +00:00
|
|
|
TIME_SECONDS,
|
2019-08-31 12:30:59 +00:00
|
|
|
"mdi:sleep",
|
|
|
|
),
|
|
|
|
WithingsSleepSummaryAttribute(
|
|
|
|
const.MEAS_SLEEP_REM_DURATION_SECONDS,
|
2019-10-24 16:41:04 +00:00
|
|
|
GetSleepSummaryField.REM_SLEEP_DURATION.value,
|
2019-08-31 12:30:59 +00:00
|
|
|
"REM sleep",
|
2020-02-23 20:09:24 +00:00
|
|
|
TIME_SECONDS,
|
2019-08-31 12:30:59 +00:00
|
|
|
"mdi:sleep",
|
|
|
|
),
|
|
|
|
WithingsSleepSummaryAttribute(
|
|
|
|
const.MEAS_SLEEP_WAKEUP_COUNT,
|
2019-10-24 16:41:04 +00:00
|
|
|
GetSleepSummaryField.WAKEUP_COUNT.value,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Wakeup count",
|
|
|
|
const.UOM_FREQUENCY,
|
|
|
|
"mdi:sleep-off",
|
|
|
|
),
|
|
|
|
WithingsSleepSummaryAttribute(
|
|
|
|
const.MEAS_SLEEP_TOSLEEP_DURATION_SECONDS,
|
2019-10-24 16:41:04 +00:00
|
|
|
GetSleepSummaryField.DURATION_TO_SLEEP.value,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Time to sleep",
|
2020-02-23 20:09:24 +00:00
|
|
|
TIME_SECONDS,
|
2019-08-31 12:30:59 +00:00
|
|
|
"mdi:sleep",
|
|
|
|
),
|
|
|
|
WithingsSleepSummaryAttribute(
|
|
|
|
const.MEAS_SLEEP_TOWAKEUP_DURATION_SECONDS,
|
2019-10-24 16:41:04 +00:00
|
|
|
GetSleepSummaryField.DURATION_TO_WAKEUP.value,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Time to wakeup",
|
2020-02-23 20:09:24 +00:00
|
|
|
TIME_SECONDS,
|
2019-08-31 12:30:59 +00:00
|
|
|
"mdi:sleep-off",
|
|
|
|
),
|
|
|
|
WithingsSleepSummaryAttribute(
|
|
|
|
const.MEAS_SLEEP_HEART_RATE_AVERAGE,
|
2019-10-24 16:41:04 +00:00
|
|
|
GetSleepSummaryField.HR_AVERAGE.value,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Average heart rate",
|
|
|
|
const.UOM_BEATS_PER_MINUTE,
|
|
|
|
"mdi:heart-pulse",
|
|
|
|
),
|
|
|
|
WithingsSleepSummaryAttribute(
|
|
|
|
const.MEAS_SLEEP_HEART_RATE_MIN,
|
2019-10-24 16:41:04 +00:00
|
|
|
GetSleepSummaryField.HR_MIN.value,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Minimum heart rate",
|
|
|
|
const.UOM_BEATS_PER_MINUTE,
|
|
|
|
"mdi:heart-pulse",
|
|
|
|
),
|
|
|
|
WithingsSleepSummaryAttribute(
|
|
|
|
const.MEAS_SLEEP_HEART_RATE_MAX,
|
2019-10-24 16:41:04 +00:00
|
|
|
GetSleepSummaryField.HR_MAX.value,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Maximum heart rate",
|
|
|
|
const.UOM_BEATS_PER_MINUTE,
|
|
|
|
"mdi:heart-pulse",
|
|
|
|
),
|
|
|
|
WithingsSleepSummaryAttribute(
|
|
|
|
const.MEAS_SLEEP_RESPIRATORY_RATE_AVERAGE,
|
2019-10-24 16:41:04 +00:00
|
|
|
GetSleepSummaryField.RR_AVERAGE.value,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Average respiratory rate",
|
|
|
|
const.UOM_BREATHS_PER_MINUTE,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
WithingsSleepSummaryAttribute(
|
|
|
|
const.MEAS_SLEEP_RESPIRATORY_RATE_MIN,
|
2019-10-24 16:41:04 +00:00
|
|
|
GetSleepSummaryField.RR_MIN.value,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Minimum respiratory rate",
|
|
|
|
const.UOM_BREATHS_PER_MINUTE,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
WithingsSleepSummaryAttribute(
|
|
|
|
const.MEAS_SLEEP_RESPIRATORY_RATE_MAX,
|
2019-10-24 16:41:04 +00:00
|
|
|
GetSleepSummaryField.RR_MAX.value,
|
2019-08-31 12:30:59 +00:00
|
|
|
"Maximum respiratory rate",
|
|
|
|
const.UOM_BREATHS_PER_MINUTE,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
WITHINGS_MEASUREMENTS_MAP = {attr.measurement: attr for attr in WITHINGS_ATTRIBUTES}
|
|
|
|
|
|
|
|
|
|
|
|
class WithingsHealthSensor(Entity):
|
|
|
|
"""Implementation of a Withings sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
2019-10-24 16:41:04 +00:00
|
|
|
self,
|
|
|
|
data_manager: WithingsDataManager,
|
|
|
|
attribute: WithingsAttribute,
|
|
|
|
user_id: str,
|
2019-08-31 12:30:59 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize the Withings sensor."""
|
|
|
|
self._data_manager = data_manager
|
|
|
|
self._attribute = attribute
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
self._slug = self._data_manager.slug
|
2019-10-24 16:41:04 +00:00
|
|
|
self._user_id = user_id
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the name of the sensor."""
|
2019-09-03 19:12:51 +00:00
|
|
|
return f"Withings {self._attribute.measurement} {self._slug}"
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Return a unique, Home Assistant friendly identifier for this entity."""
|
2019-08-31 12:30:59 +00:00
|
|
|
return "withings_{}_{}_{}".format(
|
|
|
|
self._slug, self._user_id, slugify(self._attribute.measurement)
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2019-10-24 16:41:04 +00:00
|
|
|
def state(self) -> Union[str, int, float, None]:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self) -> str:
|
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
|
|
|
return self._attribute.unit_of_measurement
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self) -> str:
|
|
|
|
"""Icon to use in the frontend, if any."""
|
|
|
|
return self._attribute.icon
|
|
|
|
|
|
|
|
@property
|
2019-10-24 16:41:04 +00:00
|
|
|
def device_state_attributes(self) -> None:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Get withings attributes."""
|
|
|
|
return self._attribute.__dict__
|
|
|
|
|
|
|
|
async def async_update(self) -> None:
|
|
|
|
"""Update the data."""
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Async update slug: %s, measurement: %s, user_id: %s",
|
|
|
|
self._slug,
|
|
|
|
self._attribute.measurement,
|
|
|
|
self._user_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
if isinstance(self._attribute, WithingsMeasureAttribute):
|
|
|
|
_LOGGER.debug("Updating measures state")
|
|
|
|
await self._data_manager.update_measures()
|
|
|
|
await self.async_update_measure(self._data_manager.measures)
|
|
|
|
|
|
|
|
elif isinstance(self._attribute, WithingsSleepStateAttribute):
|
|
|
|
_LOGGER.debug("Updating sleep state")
|
|
|
|
await self._data_manager.update_sleep()
|
|
|
|
await self.async_update_sleep_state(self._data_manager.sleep)
|
|
|
|
|
|
|
|
elif isinstance(self._attribute, WithingsSleepSummaryAttribute):
|
|
|
|
_LOGGER.debug("Updating sleep summary state")
|
|
|
|
await self._data_manager.update_sleep_summary()
|
|
|
|
await self.async_update_sleep_summary(self._data_manager.sleep_summary)
|
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
async def async_update_measure(self, data: MeasureGetMeasResponse) -> None:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Update the measures data."""
|
|
|
|
measure_type = self._attribute.measure_type
|
|
|
|
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Finding the unambiguous measure group with measure_type: %s", measure_type
|
|
|
|
)
|
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
value = get_measure_value(data, measure_type, MeasureGroupAttribs.UNAMBIGUOUS)
|
2019-08-31 12:30:59 +00:00
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
if value is None:
|
|
|
|
_LOGGER.debug("Could not find a value, setting state to %s", None)
|
2019-08-31 12:30:59 +00:00
|
|
|
self._state = None
|
|
|
|
return
|
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
self._state = round(value, 2)
|
|
|
|
|
|
|
|
async def async_update_sleep_state(self, data: SleepGetResponse) -> None:
|
|
|
|
"""Update the sleep state data."""
|
2019-08-31 12:30:59 +00:00
|
|
|
if not data.series:
|
2019-10-08 05:22:13 +00:00
|
|
|
_LOGGER.debug("No sleep data, setting state to %s", None)
|
2019-08-31 12:30:59 +00:00
|
|
|
self._state = None
|
|
|
|
return
|
|
|
|
|
2019-12-10 20:54:50 +00:00
|
|
|
sorted_series = sorted(data.series, key=lambda serie: serie.startdate)
|
|
|
|
serie = sorted_series[len(sorted_series) - 1]
|
2019-10-24 16:41:04 +00:00
|
|
|
state = None
|
|
|
|
if serie.state == SleepState.AWAKE:
|
|
|
|
state = const.STATE_AWAKE
|
|
|
|
elif serie.state == SleepState.LIGHT:
|
|
|
|
state = const.STATE_LIGHT
|
|
|
|
elif serie.state == SleepState.DEEP:
|
|
|
|
state = const.STATE_DEEP
|
|
|
|
elif serie.state == SleepState.REM:
|
|
|
|
state = const.STATE_REM
|
2019-08-31 12:30:59 +00:00
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
self._state = state
|
2019-08-31 12:30:59 +00:00
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
async def async_update_sleep_summary(self, data: SleepGetSummaryResponse) -> None:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Update the sleep summary data."""
|
|
|
|
if not data.series:
|
2019-10-08 05:22:13 +00:00
|
|
|
_LOGGER.debug("Sleep data has no series, setting state to %s", None)
|
2019-08-31 12:30:59 +00:00
|
|
|
self._state = None
|
|
|
|
return
|
|
|
|
|
|
|
|
measurement = self._attribute.measurement
|
|
|
|
measure_type = self._attribute.measure_type
|
|
|
|
|
|
|
|
_LOGGER.debug("Determining total value for: %s", measurement)
|
|
|
|
total = 0
|
|
|
|
for serie in data.series:
|
2019-10-24 16:41:04 +00:00
|
|
|
data = serie.data
|
|
|
|
value = 0
|
|
|
|
if measure_type == GetSleepSummaryField.REM_SLEEP_DURATION.value:
|
|
|
|
value = data.remsleepduration
|
|
|
|
elif measure_type == GetSleepSummaryField.WAKEUP_DURATION.value:
|
|
|
|
value = data.wakeupduration
|
|
|
|
elif measure_type == GetSleepSummaryField.LIGHT_SLEEP_DURATION.value:
|
|
|
|
value = data.lightsleepduration
|
|
|
|
elif measure_type == GetSleepSummaryField.DEEP_SLEEP_DURATION.value:
|
|
|
|
value = data.deepsleepduration
|
|
|
|
elif measure_type == GetSleepSummaryField.WAKEUP_COUNT.value:
|
|
|
|
value = data.wakeupcount
|
|
|
|
elif measure_type == GetSleepSummaryField.DURATION_TO_SLEEP.value:
|
|
|
|
value = data.durationtosleep
|
|
|
|
elif measure_type == GetSleepSummaryField.DURATION_TO_WAKEUP.value:
|
|
|
|
value = data.durationtowakeup
|
|
|
|
elif measure_type == GetSleepSummaryField.HR_AVERAGE.value:
|
|
|
|
value = data.hr_average
|
|
|
|
elif measure_type == GetSleepSummaryField.HR_MIN.value:
|
|
|
|
value = data.hr_min
|
|
|
|
elif measure_type == GetSleepSummaryField.HR_MAX.value:
|
|
|
|
value = data.hr_max
|
|
|
|
elif measure_type == GetSleepSummaryField.RR_AVERAGE.value:
|
|
|
|
value = data.rr_average
|
|
|
|
elif measure_type == GetSleepSummaryField.RR_MIN.value:
|
|
|
|
value = data.rr_min
|
|
|
|
elif measure_type == GetSleepSummaryField.RR_MAX.value:
|
|
|
|
value = data.rr_max
|
|
|
|
|
|
|
|
# Sometimes a None is provided for value, default to 0.
|
|
|
|
total += value or 0
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
self._state = round(total, 4)
|
2019-10-24 16:41:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_sensor_entities(
|
|
|
|
data_manager: WithingsDataManager, user_id: str
|
|
|
|
) -> List[WithingsHealthSensor]:
|
|
|
|
"""Create sensor entities."""
|
|
|
|
entities = []
|
|
|
|
|
|
|
|
for attribute in WITHINGS_ATTRIBUTES:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Creating entity for measurement: %s, measure_type: %s,"
|
|
|
|
"friendly_name: %s, unit_of_measurement: %s",
|
|
|
|
attribute.measurement,
|
|
|
|
attribute.measure_type,
|
|
|
|
attribute.friendly_name,
|
|
|
|
attribute.unit_of_measurement,
|
|
|
|
)
|
|
|
|
|
|
|
|
entity = WithingsHealthSensor(data_manager, attribute, user_id)
|
|
|
|
|
|
|
|
entities.append(entity)
|
|
|
|
|
|
|
|
return entities
|