2019-07-09 08:29:06 +00:00
|
|
|
"""Support for Notion sensors."""
|
2024-03-08 14:04:07 +00:00
|
|
|
|
2023-04-21 23:52:57 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2024-02-04 21:35:08 +00:00
|
|
|
from aionotion.listener.models import ListenerKind
|
2023-04-21 23:52:57 +00:00
|
|
|
|
2021-08-30 17:07:05 +00:00
|
|
|
from homeassistant.components.sensor import (
|
2021-12-15 20:46:48 +00:00
|
|
|
SensorDeviceClass,
|
2021-08-30 17:07:05 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2021-12-15 20:46:48 +00:00
|
|
|
SensorStateClass,
|
2021-08-30 17:07:05 +00:00
|
|
|
)
|
2020-08-21 14:10:12 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-12-20 17:39:09 +00:00
|
|
|
from homeassistant.const import UnitOfTemperature
|
2023-05-14 16:07:15 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-03-17 11:01:40 +00:00
|
|
|
|
2020-08-21 14:10:12 +00:00
|
|
|
from . import NotionEntity
|
2023-07-02 18:13:18 +00:00
|
|
|
from .const import DOMAIN, SENSOR_MOLD, SENSOR_TEMPERATURE
|
2024-03-12 20:51:13 +00:00
|
|
|
from .coordinator import NotionDataUpdateCoordinator
|
2024-01-17 01:00:45 +00:00
|
|
|
from .model import NotionEntityDescription
|
2023-04-21 23:52:57 +00:00
|
|
|
|
|
|
|
|
2024-01-17 01:00:45 +00:00
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
|
|
class NotionSensorDescription(SensorEntityDescription, NotionEntityDescription):
|
2023-04-21 23:52:57 +00:00
|
|
|
"""Describe a Notion sensor."""
|
|
|
|
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2021-08-25 08:42:57 +00:00
|
|
|
SENSOR_DESCRIPTIONS = (
|
2023-07-02 18:13:18 +00:00
|
|
|
NotionSensorDescription(
|
|
|
|
key=SENSOR_MOLD,
|
2023-07-03 16:31:07 +00:00
|
|
|
translation_key="mold_risk",
|
2023-07-02 18:13:18 +00:00
|
|
|
listener_kind=ListenerKind.MOLD,
|
|
|
|
),
|
2023-04-21 23:52:57 +00:00
|
|
|
NotionSensorDescription(
|
2021-08-25 08:42:57 +00:00
|
|
|
key=SENSOR_TEMPERATURE,
|
2021-12-15 20:46:48 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2022-12-20 17:39:09 +00:00
|
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
2021-12-15 20:46:48 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2023-04-21 23:52:57 +00:00
|
|
|
listener_kind=ListenerKind.TEMPERATURE,
|
2021-08-25 08:42:57 +00:00
|
|
|
),
|
|
|
|
)
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2020-08-21 14:10:12 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
2021-04-30 18:38:59 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2021-07-07 22:39:52 +00:00
|
|
|
) -> None:
|
2019-07-09 08:29:06 +00:00
|
|
|
"""Set up Notion sensors based on a config entry."""
|
2024-03-12 20:51:13 +00:00
|
|
|
coordinator: NotionDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2021-08-25 08:42:57 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
2019-07-09 08:29:06 +00:00
|
|
|
NotionSensor(
|
2020-08-21 14:10:12 +00:00
|
|
|
coordinator,
|
2023-04-21 23:52:57 +00:00
|
|
|
listener_id,
|
|
|
|
sensor.uuid,
|
|
|
|
sensor.bridge.id,
|
2021-08-25 08:42:57 +00:00
|
|
|
description,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2023-04-21 23:52:57 +00:00
|
|
|
for listener_id, listener in coordinator.data.listeners.items()
|
2021-08-25 08:42:57 +00:00
|
|
|
for description in SENSOR_DESCRIPTIONS
|
2024-02-04 21:35:08 +00:00
|
|
|
if description.listener_kind.value == listener.definition_id
|
2023-04-21 23:52:57 +00:00
|
|
|
and (sensor := coordinator.data.sensors[listener.sensor_id])
|
2021-08-25 08:42:57 +00:00
|
|
|
]
|
|
|
|
)
|
2019-07-09 08:29:06 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
class NotionSensor(NotionEntity, SensorEntity):
|
2019-07-09 08:29:06 +00:00
|
|
|
"""Define a Notion sensor."""
|
|
|
|
|
2023-05-14 16:07:15 +00:00
|
|
|
@property
|
|
|
|
def native_unit_of_measurement(self) -> str | None:
|
|
|
|
"""Return the unit of measurement of the sensor."""
|
2024-02-04 21:35:08 +00:00
|
|
|
if self.listener.definition_id == ListenerKind.TEMPERATURE.value:
|
2023-05-14 16:07:15 +00:00
|
|
|
if not self.coordinator.data.user_preferences:
|
|
|
|
return None
|
|
|
|
if self.coordinator.data.user_preferences.celsius_enabled:
|
|
|
|
return UnitOfTemperature.CELSIUS
|
|
|
|
return UnitOfTemperature.FAHRENHEIT
|
|
|
|
return None
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2023-05-14 16:07:15 +00:00
|
|
|
@property
|
|
|
|
def native_value(self) -> str | None:
|
2023-07-02 18:13:18 +00:00
|
|
|
"""Return the value reported by the sensor."""
|
2023-05-14 16:07:15 +00:00
|
|
|
if not self.listener.status_localized:
|
|
|
|
return None
|
2024-02-04 21:35:08 +00:00
|
|
|
if self.listener.definition_id == ListenerKind.TEMPERATURE.value:
|
2023-07-02 18:13:18 +00:00
|
|
|
# The Notion API only returns a localized string for temperature (e.g.
|
|
|
|
# "70°"); we simply remove the degree symbol:
|
|
|
|
return self.listener.status_localized.state[:-1]
|
|
|
|
return self.listener.status_localized.state
|