2019-07-09 08:29:06 +00:00
|
|
|
"""Support for Notion sensors."""
|
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
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2021-08-25 08:42:57 +00:00
|
|
|
SENSOR_DESCRIPTIONS = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
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,
|
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,
|
2019-07-09 08:29:06 +00:00
|
|
|
task_id,
|
2019-07-31 19:25:30 +00:00
|
|
|
sensor["id"],
|
|
|
|
sensor["bridge"]["id"],
|
|
|
|
sensor["system_id"],
|
2021-08-25 08:42:57 +00:00
|
|
|
description,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-08-25 08:42:57 +00:00
|
|
|
for task_id, task in coordinator.data["tasks"].items()
|
|
|
|
for description in SENSOR_DESCRIPTIONS
|
|
|
|
if description.key == task["task_type"]
|
|
|
|
and (sensor := coordinator.data["sensors"][task["sensor_id"]])
|
|
|
|
]
|
|
|
|
)
|
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."""
|
2021-07-01 09:24:19 +00:00
|
|
|
task = self.coordinator.data["tasks"][self._task_id]
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if task["task_type"] == SENSOR_TEMPERATURE:
|
2021-08-12 11:26:17 +00:00
|
|
|
self._attr_native_value = round(float(task["status"]["value"]), 1)
|
2019-07-09 08:29:06 +00:00
|
|
|
else:
|
2020-11-20 21:47:48 +00:00
|
|
|
LOGGER.error(
|
2019-07-31 19:25:30 +00:00
|
|
|
"Unknown task type: %s: %s",
|
2020-08-30 18:02:56 +00:00
|
|
|
self.coordinator.data["sensors"][self._sensor_id],
|
2019-07-31 19:25:30 +00:00
|
|
|
task["task_type"],
|
|
|
|
)
|