2021-02-02 01:51:20 +00:00
|
|
|
"""Support for Honeywell Lyric sensor platform."""
|
2021-09-29 14:15:36 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Callable
|
2021-08-25 17:29:34 +00:00
|
|
|
from dataclasses import dataclass
|
2021-02-02 01:51:20 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2022-12-27 20:24:33 +00:00
|
|
|
from aiolyric import Lyric
|
2021-02-02 01:51:20 +00:00
|
|
|
from aiolyric.objects.device import LyricDevice
|
|
|
|
from aiolyric.objects.location import LyricLocation
|
|
|
|
|
2021-08-25 17:29:34 +00:00
|
|
|
from homeassistant.components.sensor import (
|
2021-12-15 15:37:35 +00:00
|
|
|
SensorDeviceClass,
|
2021-08-25 17:29:34 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2021-12-15 15:37:35 +00:00
|
|
|
SensorStateClass,
|
2021-08-25 17:29:34 +00:00
|
|
|
)
|
2021-02-02 01:51:20 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-08-18 08:52:22 +00:00
|
|
|
from homeassistant.const import PERCENTAGE, UnitOfTemperature
|
2021-04-23 07:55:20 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-01-07 15:27:49 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-08-25 17:29:34 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2021-02-02 01:51:20 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|
|
|
from homeassistant.util import dt as dt_util
|
|
|
|
|
|
|
|
from . import LyricDeviceEntity
|
|
|
|
from .const import (
|
|
|
|
DOMAIN,
|
|
|
|
PRESET_HOLD_UNTIL,
|
|
|
|
PRESET_NO_HOLD,
|
|
|
|
PRESET_PERMANENT_HOLD,
|
|
|
|
PRESET_TEMPORARY_HOLD,
|
|
|
|
PRESET_VACATION_HOLD,
|
|
|
|
)
|
|
|
|
|
|
|
|
LYRIC_SETPOINT_STATUS_NAMES = {
|
|
|
|
PRESET_NO_HOLD: "Following Schedule",
|
|
|
|
PRESET_PERMANENT_HOLD: "Held Permanently",
|
|
|
|
PRESET_TEMPORARY_HOLD: "Held Temporarily",
|
|
|
|
PRESET_VACATION_HOLD: "Holiday",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-19 02:28:13 +00:00
|
|
|
@dataclass(frozen=True)
|
2023-09-18 10:42:31 +00:00
|
|
|
class LyricSensorEntityDescriptionMixin:
|
|
|
|
"""Mixin for required keys."""
|
|
|
|
|
|
|
|
value_fn: Callable[[LyricDevice], StateType | datetime]
|
|
|
|
suitable_fn: Callable[[LyricDevice], bool]
|
|
|
|
|
|
|
|
|
2023-12-19 02:28:13 +00:00
|
|
|
@dataclass(frozen=True)
|
2023-09-18 10:42:31 +00:00
|
|
|
class LyricSensorEntityDescription(
|
|
|
|
SensorEntityDescription, LyricSensorEntityDescriptionMixin
|
|
|
|
):
|
2021-08-25 17:29:34 +00:00
|
|
|
"""Class describing Honeywell Lyric sensor entities."""
|
|
|
|
|
2023-09-18 10:42:31 +00:00
|
|
|
|
|
|
|
DEVICE_SENSORS: list[LyricSensorEntityDescription] = [
|
|
|
|
LyricSensorEntityDescription(
|
|
|
|
key="indoor_temperature",
|
|
|
|
translation_key="indoor_temperature",
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
value_fn=lambda device: device.indoorTemperature,
|
|
|
|
suitable_fn=lambda device: device.indoorTemperature,
|
|
|
|
),
|
|
|
|
LyricSensorEntityDescription(
|
|
|
|
key="indoor_humidity",
|
|
|
|
translation_key="indoor_humidity",
|
|
|
|
device_class=SensorDeviceClass.HUMIDITY,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
value_fn=lambda device: device.indoorHumidity,
|
|
|
|
suitable_fn=lambda device: device.indoorHumidity,
|
|
|
|
),
|
|
|
|
LyricSensorEntityDescription(
|
|
|
|
key="outdoor_temperature",
|
|
|
|
translation_key="outdoor_temperature",
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
value_fn=lambda device: device.outdoorTemperature,
|
|
|
|
suitable_fn=lambda device: device.outdoorTemperature,
|
|
|
|
),
|
|
|
|
LyricSensorEntityDescription(
|
|
|
|
key="outdoor_humidity",
|
|
|
|
translation_key="outdoor_humidity",
|
|
|
|
device_class=SensorDeviceClass.HUMIDITY,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
value_fn=lambda device: device.displayedOutdoorHumidity,
|
|
|
|
suitable_fn=lambda device: device.displayedOutdoorHumidity,
|
|
|
|
),
|
|
|
|
LyricSensorEntityDescription(
|
|
|
|
key="next_period_time",
|
|
|
|
translation_key="next_period_time",
|
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
|
|
|
value_fn=lambda device: get_datetime_from_future_time(
|
|
|
|
device.changeableValues.nextPeriodTime
|
|
|
|
),
|
2023-09-19 09:40:05 +00:00
|
|
|
suitable_fn=lambda device: (
|
|
|
|
device.changeableValues and device.changeableValues.nextPeriodTime
|
|
|
|
),
|
2023-09-18 10:42:31 +00:00
|
|
|
),
|
|
|
|
LyricSensorEntityDescription(
|
|
|
|
key="setpoint_status",
|
|
|
|
translation_key="setpoint_status",
|
|
|
|
value_fn=lambda device: get_setpoint_status(
|
|
|
|
device.changeableValues.thermostatSetpointStatus,
|
|
|
|
device.changeableValues.nextPeriodTime,
|
|
|
|
),
|
2023-09-19 09:40:05 +00:00
|
|
|
suitable_fn=lambda device: (
|
|
|
|
device.changeableValues and device.changeableValues.thermostatSetpointStatus
|
|
|
|
),
|
2023-09-18 10:42:31 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def get_setpoint_status(status: str, time: str) -> str | None:
|
|
|
|
"""Get status of the setpoint."""
|
|
|
|
if status == PRESET_HOLD_UNTIL:
|
|
|
|
return f"Held until {time}"
|
2023-09-19 09:40:05 +00:00
|
|
|
return LYRIC_SETPOINT_STATUS_NAMES.get(status)
|
2021-08-25 17:29:34 +00:00
|
|
|
|
|
|
|
|
2022-07-05 18:24:18 +00:00
|
|
|
def get_datetime_from_future_time(time_str: str) -> datetime:
|
2021-08-25 17:29:34 +00:00
|
|
|
"""Get datetime from future time provided."""
|
2022-07-05 18:24:18 +00:00
|
|
|
time = dt_util.parse_time(time_str)
|
|
|
|
if time is None:
|
|
|
|
raise ValueError(f"Unable to parse time {time_str}")
|
2021-08-25 17:29:34 +00:00
|
|
|
now = dt_util.utcnow()
|
|
|
|
if time <= now.time():
|
|
|
|
now = now + timedelta(days=1)
|
|
|
|
return dt_util.as_utc(datetime.combine(now.date(), time))
|
|
|
|
|
|
|
|
|
2021-02-02 01:51:20 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-07 15:27:49 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2021-02-02 01:51:20 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up the Honeywell Lyric sensor platform based on a config entry."""
|
2022-12-27 20:24:33 +00:00
|
|
|
coordinator: DataUpdateCoordinator[Lyric] = hass.data[DOMAIN][entry.entry_id]
|
2021-02-02 01:51:20 +00:00
|
|
|
|
|
|
|
entities = []
|
|
|
|
|
|
|
|
for location in coordinator.data.locations:
|
|
|
|
for device in location.devices:
|
2023-09-18 10:42:31 +00:00
|
|
|
for device_sensor in DEVICE_SENSORS:
|
|
|
|
if device_sensor.suitable_fn(device):
|
2021-08-25 17:29:34 +00:00
|
|
|
entities.append(
|
|
|
|
LyricSensor(
|
|
|
|
coordinator,
|
2023-09-18 10:42:31 +00:00
|
|
|
device_sensor,
|
2021-08-25 17:29:34 +00:00
|
|
|
location,
|
|
|
|
device,
|
|
|
|
)
|
|
|
|
)
|
2021-02-02 01:51:20 +00:00
|
|
|
|
2023-09-18 10:42:31 +00:00
|
|
|
async_add_entities(entities)
|
2021-02-02 01:51:20 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
class LyricSensor(LyricDeviceEntity, SensorEntity):
|
2021-08-25 17:29:34 +00:00
|
|
|
"""Define a Honeywell Lyric sensor."""
|
2021-02-02 01:51:20 +00:00
|
|
|
|
2021-08-25 17:29:34 +00:00
|
|
|
entity_description: LyricSensorEntityDescription
|
2021-02-02 01:51:20 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2022-12-27 20:24:33 +00:00
|
|
|
coordinator: DataUpdateCoordinator[Lyric],
|
2021-08-25 17:29:34 +00:00
|
|
|
description: LyricSensorEntityDescription,
|
2021-02-02 01:51:20 +00:00
|
|
|
location: LyricLocation,
|
|
|
|
device: LyricDevice,
|
|
|
|
) -> None:
|
2021-08-25 17:29:34 +00:00
|
|
|
"""Initialize."""
|
2021-02-02 01:51:20 +00:00
|
|
|
super().__init__(
|
|
|
|
coordinator,
|
|
|
|
location,
|
|
|
|
device,
|
2023-09-18 10:42:31 +00:00
|
|
|
f"{device.macID}_{description.key}",
|
2021-02-02 01:51:20 +00:00
|
|
|
)
|
2021-08-25 17:29:34 +00:00
|
|
|
self.entity_description = description
|
2023-09-18 10:42:31 +00:00
|
|
|
if description.device_class == SensorDeviceClass.TEMPERATURE:
|
|
|
|
if device.units == "Fahrenheit":
|
|
|
|
self._attr_native_unit_of_measurement = UnitOfTemperature.FAHRENHEIT
|
|
|
|
else:
|
|
|
|
self._attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
|
2021-02-02 01:51:20 +00:00
|
|
|
|
|
|
|
@property
|
2023-09-18 10:42:31 +00:00
|
|
|
def native_value(self) -> StateType | datetime:
|
2021-08-25 17:29:34 +00:00
|
|
|
"""Return the state."""
|
2023-09-18 10:42:31 +00:00
|
|
|
return self.entity_description.value_fn(self.device)
|