Split platform attributes in withings (#84099)
This commit is contained in:
parent
186e3a6d98
commit
f5a8ce4aca
5 changed files with 372 additions and 424 deletions
|
@ -1,8 +1,9 @@
|
||||||
"""Sensors flow for Withings."""
|
"""Sensors flow for Withings."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from withings_api.common import NotifyAppli
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
DOMAIN as BINARY_SENSOR_DOMAIN,
|
|
||||||
BinarySensorDeviceClass,
|
BinarySensorDeviceClass,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
)
|
)
|
||||||
|
@ -10,7 +11,26 @@ from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .common import BaseWithingsSensor, async_create_entities
|
from .common import (
|
||||||
|
BaseWithingsSensor,
|
||||||
|
UpdateType,
|
||||||
|
WithingsAttribute,
|
||||||
|
async_get_data_manager,
|
||||||
|
)
|
||||||
|
from .const import Measurement
|
||||||
|
|
||||||
|
BINARY_SENSORS = [
|
||||||
|
# Webhook measurements.
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.IN_BED,
|
||||||
|
NotifyAppli.BED_IN,
|
||||||
|
"In bed",
|
||||||
|
"",
|
||||||
|
"mdi:bed",
|
||||||
|
True,
|
||||||
|
UpdateType.WEBHOOK,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
|
@ -19,9 +39,12 @@ async def async_setup_entry(
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the sensor config entry."""
|
"""Set up the sensor config entry."""
|
||||||
entities = await async_create_entities(
|
data_manager = await async_get_data_manager(hass, entry)
|
||||||
hass, entry, WithingsHealthBinarySensor, BINARY_SENSOR_DOMAIN
|
|
||||||
)
|
entities = [
|
||||||
|
WithingsHealthBinarySensor(data_manager, attribute)
|
||||||
|
for attribute in BINARY_SENSORS
|
||||||
|
]
|
||||||
|
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
|
|
@ -30,17 +30,9 @@ from withings_api.common import (
|
||||||
from homeassistant.backports.enum import StrEnum
|
from homeassistant.backports.enum import StrEnum
|
||||||
from homeassistant.components import webhook
|
from homeassistant.components import webhook
|
||||||
from homeassistant.components.application_credentials import AuthImplementation
|
from homeassistant.components.application_credentials import AuthImplementation
|
||||||
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
|
||||||
from homeassistant.components.http import HomeAssistantView
|
from homeassistant.components.http import HomeAssistantView
|
||||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import CONF_WEBHOOK_ID
|
||||||
CONF_WEBHOOK_ID,
|
|
||||||
MASS_KILOGRAMS,
|
|
||||||
PERCENTAGE,
|
|
||||||
SPEED_METERS_PER_SECOND,
|
|
||||||
TIME_SECONDS,
|
|
||||||
)
|
|
||||||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import config_entry_oauth2_flow
|
from homeassistant.helpers import config_entry_oauth2_flow
|
||||||
|
@ -90,19 +82,10 @@ class WithingsAttribute:
|
||||||
friendly_name: str
|
friendly_name: str
|
||||||
unit_of_measurement: str
|
unit_of_measurement: str
|
||||||
icon: str | None
|
icon: str | None
|
||||||
platform: str
|
|
||||||
enabled_by_default: bool
|
enabled_by_default: bool
|
||||||
update_type: UpdateType
|
update_type: UpdateType
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class WithingsData:
|
|
||||||
"""Represents value and meta-data from the withings service."""
|
|
||||||
|
|
||||||
attribute: WithingsAttribute
|
|
||||||
value: Any
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class WebhookConfig:
|
class WebhookConfig:
|
||||||
"""Config for a webhook."""
|
"""Config for a webhook."""
|
||||||
|
@ -120,351 +103,6 @@ class StateData:
|
||||||
state: Any
|
state: Any
|
||||||
|
|
||||||
|
|
||||||
WITHINGS_ATTRIBUTES = [
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.WEIGHT_KG,
|
|
||||||
MeasureType.WEIGHT,
|
|
||||||
"Weight",
|
|
||||||
MASS_KILOGRAMS,
|
|
||||||
"mdi:weight-kilogram",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.FAT_MASS_KG,
|
|
||||||
MeasureType.FAT_MASS_WEIGHT,
|
|
||||||
"Fat Mass",
|
|
||||||
MASS_KILOGRAMS,
|
|
||||||
"mdi:weight-kilogram",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.FAT_FREE_MASS_KG,
|
|
||||||
MeasureType.FAT_FREE_MASS,
|
|
||||||
"Fat Free Mass",
|
|
||||||
MASS_KILOGRAMS,
|
|
||||||
"mdi:weight-kilogram",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.MUSCLE_MASS_KG,
|
|
||||||
MeasureType.MUSCLE_MASS,
|
|
||||||
"Muscle Mass",
|
|
||||||
MASS_KILOGRAMS,
|
|
||||||
"mdi:weight-kilogram",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.BONE_MASS_KG,
|
|
||||||
MeasureType.BONE_MASS,
|
|
||||||
"Bone Mass",
|
|
||||||
MASS_KILOGRAMS,
|
|
||||||
"mdi:weight-kilogram",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.HEIGHT_M,
|
|
||||||
MeasureType.HEIGHT,
|
|
||||||
"Height",
|
|
||||||
const.UOM_LENGTH_M,
|
|
||||||
"mdi:ruler",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.TEMP_C,
|
|
||||||
MeasureType.TEMPERATURE,
|
|
||||||
"Temperature",
|
|
||||||
const.UOM_TEMP_C,
|
|
||||||
"mdi:thermometer",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.BODY_TEMP_C,
|
|
||||||
MeasureType.BODY_TEMPERATURE,
|
|
||||||
"Body Temperature",
|
|
||||||
const.UOM_TEMP_C,
|
|
||||||
"mdi:thermometer",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SKIN_TEMP_C,
|
|
||||||
MeasureType.SKIN_TEMPERATURE,
|
|
||||||
"Skin Temperature",
|
|
||||||
const.UOM_TEMP_C,
|
|
||||||
"mdi:thermometer",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.FAT_RATIO_PCT,
|
|
||||||
MeasureType.FAT_RATIO,
|
|
||||||
"Fat Ratio",
|
|
||||||
PERCENTAGE,
|
|
||||||
None,
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.DIASTOLIC_MMHG,
|
|
||||||
MeasureType.DIASTOLIC_BLOOD_PRESSURE,
|
|
||||||
"Diastolic Blood Pressure",
|
|
||||||
const.UOM_MMHG,
|
|
||||||
None,
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SYSTOLIC_MMGH,
|
|
||||||
MeasureType.SYSTOLIC_BLOOD_PRESSURE,
|
|
||||||
"Systolic Blood Pressure",
|
|
||||||
const.UOM_MMHG,
|
|
||||||
None,
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.HEART_PULSE_BPM,
|
|
||||||
MeasureType.HEART_RATE,
|
|
||||||
"Heart Pulse",
|
|
||||||
const.UOM_BEATS_PER_MINUTE,
|
|
||||||
"mdi:heart-pulse",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SPO2_PCT,
|
|
||||||
MeasureType.SP02,
|
|
||||||
"SP02",
|
|
||||||
PERCENTAGE,
|
|
||||||
None,
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.HYDRATION,
|
|
||||||
MeasureType.HYDRATION,
|
|
||||||
"Hydration",
|
|
||||||
MASS_KILOGRAMS,
|
|
||||||
"mdi:water",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.PWV,
|
|
||||||
MeasureType.PULSE_WAVE_VELOCITY,
|
|
||||||
"Pulse Wave Velocity",
|
|
||||||
SPEED_METERS_PER_SECOND,
|
|
||||||
None,
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_BREATHING_DISTURBANCES_INTENSITY,
|
|
||||||
GetSleepSummaryField.BREATHING_DISTURBANCES_INTENSITY,
|
|
||||||
"Breathing disturbances intensity",
|
|
||||||
"",
|
|
||||||
"",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_DEEP_DURATION_SECONDS,
|
|
||||||
GetSleepSummaryField.DEEP_SLEEP_DURATION,
|
|
||||||
"Deep sleep",
|
|
||||||
TIME_SECONDS,
|
|
||||||
"mdi:sleep",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_TOSLEEP_DURATION_SECONDS,
|
|
||||||
GetSleepSummaryField.DURATION_TO_SLEEP,
|
|
||||||
"Time to sleep",
|
|
||||||
TIME_SECONDS,
|
|
||||||
"mdi:sleep",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_TOWAKEUP_DURATION_SECONDS,
|
|
||||||
GetSleepSummaryField.DURATION_TO_WAKEUP,
|
|
||||||
"Time to wakeup",
|
|
||||||
TIME_SECONDS,
|
|
||||||
"mdi:sleep-off",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_HEART_RATE_AVERAGE,
|
|
||||||
GetSleepSummaryField.HR_AVERAGE,
|
|
||||||
"Average heart rate",
|
|
||||||
const.UOM_BEATS_PER_MINUTE,
|
|
||||||
"mdi:heart-pulse",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_HEART_RATE_MAX,
|
|
||||||
GetSleepSummaryField.HR_MAX,
|
|
||||||
"Maximum heart rate",
|
|
||||||
const.UOM_BEATS_PER_MINUTE,
|
|
||||||
"mdi:heart-pulse",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_HEART_RATE_MIN,
|
|
||||||
GetSleepSummaryField.HR_MIN,
|
|
||||||
"Minimum heart rate",
|
|
||||||
const.UOM_BEATS_PER_MINUTE,
|
|
||||||
"mdi:heart-pulse",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_LIGHT_DURATION_SECONDS,
|
|
||||||
GetSleepSummaryField.LIGHT_SLEEP_DURATION,
|
|
||||||
"Light sleep",
|
|
||||||
TIME_SECONDS,
|
|
||||||
"mdi:sleep",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_REM_DURATION_SECONDS,
|
|
||||||
GetSleepSummaryField.REM_SLEEP_DURATION,
|
|
||||||
"REM sleep",
|
|
||||||
TIME_SECONDS,
|
|
||||||
"mdi:sleep",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_RESPIRATORY_RATE_AVERAGE,
|
|
||||||
GetSleepSummaryField.RR_AVERAGE,
|
|
||||||
"Average respiratory rate",
|
|
||||||
const.UOM_BREATHS_PER_MINUTE,
|
|
||||||
None,
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_RESPIRATORY_RATE_MAX,
|
|
||||||
GetSleepSummaryField.RR_MAX,
|
|
||||||
"Maximum respiratory rate",
|
|
||||||
const.UOM_BREATHS_PER_MINUTE,
|
|
||||||
None,
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_RESPIRATORY_RATE_MIN,
|
|
||||||
GetSleepSummaryField.RR_MIN,
|
|
||||||
"Minimum respiratory rate",
|
|
||||||
const.UOM_BREATHS_PER_MINUTE,
|
|
||||||
None,
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_SCORE,
|
|
||||||
GetSleepSummaryField.SLEEP_SCORE,
|
|
||||||
"Sleep score",
|
|
||||||
const.SCORE_POINTS,
|
|
||||||
"mdi:medal",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_SNORING,
|
|
||||||
GetSleepSummaryField.SNORING,
|
|
||||||
"Snoring",
|
|
||||||
"",
|
|
||||||
None,
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_SNORING_EPISODE_COUNT,
|
|
||||||
GetSleepSummaryField.SNORING_EPISODE_COUNT,
|
|
||||||
"Snoring episode count",
|
|
||||||
"",
|
|
||||||
None,
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_WAKEUP_COUNT,
|
|
||||||
GetSleepSummaryField.WAKEUP_COUNT,
|
|
||||||
"Wakeup count",
|
|
||||||
const.UOM_FREQUENCY,
|
|
||||||
"mdi:sleep-off",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.SLEEP_WAKEUP_DURATION_SECONDS,
|
|
||||||
GetSleepSummaryField.WAKEUP_DURATION,
|
|
||||||
"Wakeup time",
|
|
||||||
TIME_SECONDS,
|
|
||||||
"mdi:sleep-off",
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
False,
|
|
||||||
UpdateType.POLL,
|
|
||||||
),
|
|
||||||
# Webhook measurements.
|
|
||||||
WithingsAttribute(
|
|
||||||
Measurement.IN_BED,
|
|
||||||
NotifyAppli.BED_IN,
|
|
||||||
"In bed",
|
|
||||||
"",
|
|
||||||
"mdi:bed",
|
|
||||||
BINARY_SENSOR_DOMAIN,
|
|
||||||
True,
|
|
||||||
UpdateType.WEBHOOK,
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
WITHINGS_MEASURE_TYPE_MAP: dict[
|
WITHINGS_MEASURE_TYPE_MAP: dict[
|
||||||
NotifyAppli | GetSleepSummaryField | MeasureType, Measurement
|
NotifyAppli | GetSleepSummaryField | MeasureType, Measurement
|
||||||
] = {
|
] = {
|
||||||
|
@ -776,7 +414,7 @@ class DataManager:
|
||||||
|
|
||||||
raise exception
|
raise exception
|
||||||
|
|
||||||
async def _async_get_all_data(self) -> dict[MeasureType, Any] | None:
|
async def _async_get_all_data(self) -> dict[Measurement, Any] | None:
|
||||||
_LOGGER.info("Updating all withings data")
|
_LOGGER.info("Updating all withings data")
|
||||||
return {
|
return {
|
||||||
**await self.async_get_measures(),
|
**await self.async_get_measures(),
|
||||||
|
@ -1072,28 +710,6 @@ def async_remove_data_manager(hass: HomeAssistant, config_entry: ConfigEntry) ->
|
||||||
del hass.data[const.DOMAIN][config_entry.entry_id][const.DATA_MANAGER]
|
del hass.data[const.DOMAIN][config_entry.entry_id][const.DATA_MANAGER]
|
||||||
|
|
||||||
|
|
||||||
async def async_create_entities(
|
|
||||||
hass: HomeAssistant,
|
|
||||||
entry: ConfigEntry,
|
|
||||||
create_func: Callable[[DataManager, WithingsAttribute], Entity],
|
|
||||||
platform: str,
|
|
||||||
) -> list[Entity]:
|
|
||||||
"""Create withings entities from config entry."""
|
|
||||||
data_manager = await async_get_data_manager(hass, entry)
|
|
||||||
|
|
||||||
return [
|
|
||||||
create_func(data_manager, attribute)
|
|
||||||
for attribute in get_platform_attributes(platform)
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def get_platform_attributes(platform: str) -> tuple[WithingsAttribute, ...]:
|
|
||||||
"""Get withings attributes used for a specific platform."""
|
|
||||||
return tuple(
|
|
||||||
attribute for attribute in WITHINGS_ATTRIBUTES if attribute.platform == platform
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class WithingsLocalOAuth2Implementation(AuthImplementation):
|
class WithingsLocalOAuth2Implementation(AuthImplementation):
|
||||||
"""Oauth2 implementation that only uses the external url."""
|
"""Oauth2 implementation that only uses the external url."""
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,335 @@
|
||||||
"""Sensors flow for Withings."""
|
"""Sensors flow for Withings."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from withings_api.common import GetSleepSummaryField, MeasureType
|
||||||
DOMAIN as SENSOR_DOMAIN,
|
|
||||||
SensorEntity,
|
from homeassistant.components.sensor import SensorEntity, SensorStateClass
|
||||||
SensorStateClass,
|
|
||||||
)
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.const import (
|
||||||
|
MASS_KILOGRAMS,
|
||||||
|
PERCENTAGE,
|
||||||
|
SPEED_METERS_PER_SECOND,
|
||||||
|
TIME_SECONDS,
|
||||||
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .common import BaseWithingsSensor, async_create_entities
|
from .common import (
|
||||||
|
BaseWithingsSensor,
|
||||||
|
UpdateType,
|
||||||
|
WithingsAttribute,
|
||||||
|
async_get_data_manager,
|
||||||
|
)
|
||||||
|
from .const import (
|
||||||
|
SCORE_POINTS,
|
||||||
|
UOM_BEATS_PER_MINUTE,
|
||||||
|
UOM_BREATHS_PER_MINUTE,
|
||||||
|
UOM_FREQUENCY,
|
||||||
|
UOM_LENGTH_M,
|
||||||
|
UOM_MMHG,
|
||||||
|
UOM_TEMP_C,
|
||||||
|
Measurement,
|
||||||
|
)
|
||||||
|
|
||||||
|
SENSORS = [
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.WEIGHT_KG,
|
||||||
|
MeasureType.WEIGHT,
|
||||||
|
"Weight",
|
||||||
|
MASS_KILOGRAMS,
|
||||||
|
"mdi:weight-kilogram",
|
||||||
|
True,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.FAT_MASS_KG,
|
||||||
|
MeasureType.FAT_MASS_WEIGHT,
|
||||||
|
"Fat Mass",
|
||||||
|
MASS_KILOGRAMS,
|
||||||
|
"mdi:weight-kilogram",
|
||||||
|
True,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.FAT_FREE_MASS_KG,
|
||||||
|
MeasureType.FAT_FREE_MASS,
|
||||||
|
"Fat Free Mass",
|
||||||
|
MASS_KILOGRAMS,
|
||||||
|
"mdi:weight-kilogram",
|
||||||
|
True,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.MUSCLE_MASS_KG,
|
||||||
|
MeasureType.MUSCLE_MASS,
|
||||||
|
"Muscle Mass",
|
||||||
|
MASS_KILOGRAMS,
|
||||||
|
"mdi:weight-kilogram",
|
||||||
|
True,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.BONE_MASS_KG,
|
||||||
|
MeasureType.BONE_MASS,
|
||||||
|
"Bone Mass",
|
||||||
|
MASS_KILOGRAMS,
|
||||||
|
"mdi:weight-kilogram",
|
||||||
|
True,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.HEIGHT_M,
|
||||||
|
MeasureType.HEIGHT,
|
||||||
|
"Height",
|
||||||
|
UOM_LENGTH_M,
|
||||||
|
"mdi:ruler",
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.TEMP_C,
|
||||||
|
MeasureType.TEMPERATURE,
|
||||||
|
"Temperature",
|
||||||
|
UOM_TEMP_C,
|
||||||
|
"mdi:thermometer",
|
||||||
|
True,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.BODY_TEMP_C,
|
||||||
|
MeasureType.BODY_TEMPERATURE,
|
||||||
|
"Body Temperature",
|
||||||
|
UOM_TEMP_C,
|
||||||
|
"mdi:thermometer",
|
||||||
|
True,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SKIN_TEMP_C,
|
||||||
|
MeasureType.SKIN_TEMPERATURE,
|
||||||
|
"Skin Temperature",
|
||||||
|
UOM_TEMP_C,
|
||||||
|
"mdi:thermometer",
|
||||||
|
True,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.FAT_RATIO_PCT,
|
||||||
|
MeasureType.FAT_RATIO,
|
||||||
|
"Fat Ratio",
|
||||||
|
PERCENTAGE,
|
||||||
|
None,
|
||||||
|
True,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.DIASTOLIC_MMHG,
|
||||||
|
MeasureType.DIASTOLIC_BLOOD_PRESSURE,
|
||||||
|
"Diastolic Blood Pressure",
|
||||||
|
UOM_MMHG,
|
||||||
|
None,
|
||||||
|
True,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SYSTOLIC_MMGH,
|
||||||
|
MeasureType.SYSTOLIC_BLOOD_PRESSURE,
|
||||||
|
"Systolic Blood Pressure",
|
||||||
|
UOM_MMHG,
|
||||||
|
None,
|
||||||
|
True,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.HEART_PULSE_BPM,
|
||||||
|
MeasureType.HEART_RATE,
|
||||||
|
"Heart Pulse",
|
||||||
|
UOM_BEATS_PER_MINUTE,
|
||||||
|
"mdi:heart-pulse",
|
||||||
|
True,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SPO2_PCT,
|
||||||
|
MeasureType.SP02,
|
||||||
|
"SP02",
|
||||||
|
PERCENTAGE,
|
||||||
|
None,
|
||||||
|
True,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.HYDRATION,
|
||||||
|
MeasureType.HYDRATION,
|
||||||
|
"Hydration",
|
||||||
|
MASS_KILOGRAMS,
|
||||||
|
"mdi:water",
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.PWV,
|
||||||
|
MeasureType.PULSE_WAVE_VELOCITY,
|
||||||
|
"Pulse Wave Velocity",
|
||||||
|
SPEED_METERS_PER_SECOND,
|
||||||
|
None,
|
||||||
|
True,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_BREATHING_DISTURBANCES_INTENSITY,
|
||||||
|
GetSleepSummaryField.BREATHING_DISTURBANCES_INTENSITY,
|
||||||
|
"Breathing disturbances intensity",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_DEEP_DURATION_SECONDS,
|
||||||
|
GetSleepSummaryField.DEEP_SLEEP_DURATION,
|
||||||
|
"Deep sleep",
|
||||||
|
TIME_SECONDS,
|
||||||
|
"mdi:sleep",
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_TOSLEEP_DURATION_SECONDS,
|
||||||
|
GetSleepSummaryField.DURATION_TO_SLEEP,
|
||||||
|
"Time to sleep",
|
||||||
|
TIME_SECONDS,
|
||||||
|
"mdi:sleep",
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_TOWAKEUP_DURATION_SECONDS,
|
||||||
|
GetSleepSummaryField.DURATION_TO_WAKEUP,
|
||||||
|
"Time to wakeup",
|
||||||
|
TIME_SECONDS,
|
||||||
|
"mdi:sleep-off",
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_HEART_RATE_AVERAGE,
|
||||||
|
GetSleepSummaryField.HR_AVERAGE,
|
||||||
|
"Average heart rate",
|
||||||
|
UOM_BEATS_PER_MINUTE,
|
||||||
|
"mdi:heart-pulse",
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_HEART_RATE_MAX,
|
||||||
|
GetSleepSummaryField.HR_MAX,
|
||||||
|
"Maximum heart rate",
|
||||||
|
UOM_BEATS_PER_MINUTE,
|
||||||
|
"mdi:heart-pulse",
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_HEART_RATE_MIN,
|
||||||
|
GetSleepSummaryField.HR_MIN,
|
||||||
|
"Minimum heart rate",
|
||||||
|
UOM_BEATS_PER_MINUTE,
|
||||||
|
"mdi:heart-pulse",
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_LIGHT_DURATION_SECONDS,
|
||||||
|
GetSleepSummaryField.LIGHT_SLEEP_DURATION,
|
||||||
|
"Light sleep",
|
||||||
|
TIME_SECONDS,
|
||||||
|
"mdi:sleep",
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_REM_DURATION_SECONDS,
|
||||||
|
GetSleepSummaryField.REM_SLEEP_DURATION,
|
||||||
|
"REM sleep",
|
||||||
|
TIME_SECONDS,
|
||||||
|
"mdi:sleep",
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_RESPIRATORY_RATE_AVERAGE,
|
||||||
|
GetSleepSummaryField.RR_AVERAGE,
|
||||||
|
"Average respiratory rate",
|
||||||
|
UOM_BREATHS_PER_MINUTE,
|
||||||
|
None,
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_RESPIRATORY_RATE_MAX,
|
||||||
|
GetSleepSummaryField.RR_MAX,
|
||||||
|
"Maximum respiratory rate",
|
||||||
|
UOM_BREATHS_PER_MINUTE,
|
||||||
|
None,
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_RESPIRATORY_RATE_MIN,
|
||||||
|
GetSleepSummaryField.RR_MIN,
|
||||||
|
"Minimum respiratory rate",
|
||||||
|
UOM_BREATHS_PER_MINUTE,
|
||||||
|
None,
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_SCORE,
|
||||||
|
GetSleepSummaryField.SLEEP_SCORE,
|
||||||
|
"Sleep score",
|
||||||
|
SCORE_POINTS,
|
||||||
|
"mdi:medal",
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_SNORING,
|
||||||
|
GetSleepSummaryField.SNORING,
|
||||||
|
"Snoring",
|
||||||
|
"",
|
||||||
|
None,
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_SNORING_EPISODE_COUNT,
|
||||||
|
GetSleepSummaryField.SNORING_EPISODE_COUNT,
|
||||||
|
"Snoring episode count",
|
||||||
|
"",
|
||||||
|
None,
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_WAKEUP_COUNT,
|
||||||
|
GetSleepSummaryField.WAKEUP_COUNT,
|
||||||
|
"Wakeup count",
|
||||||
|
UOM_FREQUENCY,
|
||||||
|
"mdi:sleep-off",
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
WithingsAttribute(
|
||||||
|
Measurement.SLEEP_WAKEUP_DURATION_SECONDS,
|
||||||
|
GetSleepSummaryField.WAKEUP_DURATION,
|
||||||
|
"Wakeup time",
|
||||||
|
TIME_SECONDS,
|
||||||
|
"mdi:sleep-off",
|
||||||
|
False,
|
||||||
|
UpdateType.POLL,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
|
@ -19,12 +338,9 @@ async def async_setup_entry(
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the sensor config entry."""
|
"""Set up the sensor config entry."""
|
||||||
entities = await async_create_entities(
|
data_manager = await async_get_data_manager(hass, entry)
|
||||||
hass,
|
|
||||||
entry,
|
entities = [WithingsHealthSensor(data_manager, attribute) for attribute in SENSORS]
|
||||||
WithingsHealthSensor,
|
|
||||||
SENSOR_DOMAIN,
|
|
||||||
)
|
|
||||||
|
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,8 @@
|
||||||
from withings_api.common import NotifyAppli
|
from withings_api.common import NotifyAppli
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
||||||
from homeassistant.components.withings.common import (
|
from homeassistant.components.withings.binary_sensor import BINARY_SENSORS
|
||||||
WITHINGS_ATTRIBUTES,
|
from homeassistant.components.withings.common import WithingsAttribute
|
||||||
WithingsAttribute,
|
|
||||||
)
|
|
||||||
from homeassistant.components.withings.const import Measurement
|
from homeassistant.components.withings.const import Measurement
|
||||||
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
|
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
@ -15,9 +13,7 @@ from homeassistant.helpers.entity_registry import EntityRegistry
|
||||||
from .common import ComponentFactory, async_get_entity_id, new_profile_config
|
from .common import ComponentFactory, async_get_entity_id, new_profile_config
|
||||||
|
|
||||||
WITHINGS_MEASUREMENTS_MAP: dict[Measurement, WithingsAttribute] = {
|
WITHINGS_MEASUREMENTS_MAP: dict[Measurement, WithingsAttribute] = {
|
||||||
attr.measurement: attr
|
attr.measurement: attr for attr in BINARY_SENSORS
|
||||||
for attr in WITHINGS_ATTRIBUTES
|
|
||||||
if attr.platform == BINARY_SENSOR_DOMAIN
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,13 +18,9 @@ from withings_api.common import (
|
||||||
)
|
)
|
||||||
|
|
||||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||||
from homeassistant.components.withings.common import (
|
from homeassistant.components.withings.common import WithingsAttribute
|
||||||
WITHINGS_ATTRIBUTES,
|
|
||||||
WithingsAttribute,
|
|
||||||
get_platform_attributes,
|
|
||||||
)
|
|
||||||
from homeassistant.components.withings.const import Measurement
|
from homeassistant.components.withings.const import Measurement
|
||||||
from homeassistant.const import Platform
|
from homeassistant.components.withings.sensor import SENSORS
|
||||||
from homeassistant.core import HomeAssistant, State
|
from homeassistant.core import HomeAssistant, State
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
from homeassistant.helpers.entity_registry import EntityRegistry
|
from homeassistant.helpers.entity_registry import EntityRegistry
|
||||||
|
@ -33,9 +29,7 @@ from homeassistant.util import dt as dt_util
|
||||||
from .common import ComponentFactory, async_get_entity_id, new_profile_config
|
from .common import ComponentFactory, async_get_entity_id, new_profile_config
|
||||||
|
|
||||||
WITHINGS_MEASUREMENTS_MAP: dict[Measurement, WithingsAttribute] = {
|
WITHINGS_MEASUREMENTS_MAP: dict[Measurement, WithingsAttribute] = {
|
||||||
attr.measurement: attr
|
attr.measurement: attr for attr in SENSORS
|
||||||
for attr in WITHINGS_ATTRIBUTES
|
|
||||||
if attr.platform == SENSOR_DOMAIN
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PERSON0 = new_profile_config(
|
PERSON0 = new_profile_config(
|
||||||
|
@ -296,7 +290,10 @@ EXPECTED_DATA = (
|
||||||
|
|
||||||
|
|
||||||
def async_assert_state_equals(
|
def async_assert_state_equals(
|
||||||
entity_id: str, state_obj: State, expected: Any, attribute: WithingsAttribute
|
entity_id: str,
|
||||||
|
state_obj: State,
|
||||||
|
expected: Any,
|
||||||
|
attribute: WithingsAttribute,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Assert at given state matches what is expected."""
|
"""Assert at given state matches what is expected."""
|
||||||
assert state_obj, f"Expected entity {entity_id} to exist but it did not"
|
assert state_obj, f"Expected entity {entity_id} to exist but it did not"
|
||||||
|
@ -316,7 +313,7 @@ async def test_sensor_default_enabled_entities(
|
||||||
await component_factory.configure_component(profile_configs=(PERSON0,))
|
await component_factory.configure_component(profile_configs=(PERSON0,))
|
||||||
|
|
||||||
# Assert entities should not exist yet.
|
# Assert entities should not exist yet.
|
||||||
for attribute in get_platform_attributes(Platform.SENSOR):
|
for attribute in SENSORS:
|
||||||
assert not await async_get_entity_id(
|
assert not await async_get_entity_id(
|
||||||
hass, attribute, PERSON0.user_id, SENSOR_DOMAIN
|
hass, attribute, PERSON0.user_id, SENSOR_DOMAIN
|
||||||
)
|
)
|
||||||
|
@ -325,7 +322,7 @@ async def test_sensor_default_enabled_entities(
|
||||||
await component_factory.setup_profile(PERSON0.user_id)
|
await component_factory.setup_profile(PERSON0.user_id)
|
||||||
|
|
||||||
# Assert entities should exist.
|
# Assert entities should exist.
|
||||||
for attribute in get_platform_attributes(Platform.SENSOR):
|
for attribute in SENSORS:
|
||||||
entity_id = await async_get_entity_id(
|
entity_id = await async_get_entity_id(
|
||||||
hass, attribute, PERSON0.user_id, SENSOR_DOMAIN
|
hass, attribute, PERSON0.user_id, SENSOR_DOMAIN
|
||||||
)
|
)
|
||||||
|
@ -368,7 +365,7 @@ async def test_all_entities(
|
||||||
await component_factory.configure_component(profile_configs=(PERSON0,))
|
await component_factory.configure_component(profile_configs=(PERSON0,))
|
||||||
|
|
||||||
# Assert entities should not exist yet.
|
# Assert entities should not exist yet.
|
||||||
for attribute in get_platform_attributes(Platform.SENSOR):
|
for attribute in SENSORS:
|
||||||
assert not await async_get_entity_id(
|
assert not await async_get_entity_id(
|
||||||
hass, attribute, PERSON0.user_id, SENSOR_DOMAIN
|
hass, attribute, PERSON0.user_id, SENSOR_DOMAIN
|
||||||
)
|
)
|
||||||
|
@ -377,7 +374,7 @@ async def test_all_entities(
|
||||||
await component_factory.setup_profile(PERSON0.user_id)
|
await component_factory.setup_profile(PERSON0.user_id)
|
||||||
|
|
||||||
# Assert entities should exist.
|
# Assert entities should exist.
|
||||||
for attribute in get_platform_attributes(Platform.SENSOR):
|
for attribute in SENSORS:
|
||||||
entity_id = await async_get_entity_id(
|
entity_id = await async_get_entity_id(
|
||||||
hass, attribute, PERSON0.user_id, SENSOR_DOMAIN
|
hass, attribute, PERSON0.user_id, SENSOR_DOMAIN
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue