2019-07-09 08:29:06 +00:00
|
|
|
"""Support for Notion sensors."""
|
2023-04-21 23:52:57 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
from aionotion.sensor.models import ListenerKind
|
|
|
|
|
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
|
2020-08-21 14:10:12 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
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
|
2021-11-14 18:08:35 +00:00
|
|
|
from .const import DOMAIN, LOGGER, SENSOR_TEMPERATURE
|
2023-04-21 23:52:57 +00:00
|
|
|
from .model import NotionEntityDescriptionMixin
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class NotionSensorDescription(SensorEntityDescription, NotionEntityDescriptionMixin):
|
|
|
|
"""Describe a Notion sensor."""
|
|
|
|
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2021-08-25 08:42:57 +00:00
|
|
|
SENSOR_DESCRIPTIONS = (
|
2023-04-21 23:52:57 +00:00
|
|
|
NotionSensorDescription(
|
2021-08-25 08:42:57 +00:00
|
|
|
key=SENSOR_TEMPERATURE,
|
|
|
|
name="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."""
|
2021-11-14 18:08:35 +00:00
|
|
|
coordinator = 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,
|
|
|
|
sensor.system_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
|
2023-04-21 23:52:57 +00:00
|
|
|
if description.listener_kind == listener.listener_kind
|
|
|
|
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."""
|
|
|
|
|
2020-03-17 11:01:40 +00:00
|
|
|
@callback
|
2020-08-21 14:10:12 +00:00
|
|
|
def _async_update_from_latest_data(self) -> None:
|
2019-07-09 08:29:06 +00:00
|
|
|
"""Fetch new state data for the sensor."""
|
2023-04-21 23:52:57 +00:00
|
|
|
listener = self.coordinator.data.listeners[self._listener_id]
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2023-04-21 23:52:57 +00:00
|
|
|
if listener.listener_kind == ListenerKind.TEMPERATURE:
|
2023-04-23 09:24:39 +00:00
|
|
|
self._attr_native_value = round(listener.status.temperature, 1) # type: ignore[attr-defined]
|
2019-07-09 08:29:06 +00:00
|
|
|
else:
|
2020-11-20 21:47:48 +00:00
|
|
|
LOGGER.error(
|
2023-04-21 23:52:57 +00:00
|
|
|
"Unknown listener type for sensor %s",
|
|
|
|
self.coordinator.data.sensors[self._sensor_id],
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|