Update Notion to use a DataUpdateCoordinator (#38978)
* Update Notion to use a DataUpdateCoordinator * isort * Bug
This commit is contained in:
parent
6fd61be276
commit
31e0ddaec5
4 changed files with 191 additions and 199 deletions
|
@ -1,29 +1,37 @@
|
|||
"""Support for Notion sensors."""
|
||||
import logging
|
||||
from typing import Callable
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import TEMP_CELSIUS
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from . import SENSOR_TEMPERATURE, SENSOR_TYPES, NotionEntity
|
||||
from .const import DATA_CLIENT, DOMAIN
|
||||
from . import NotionEntity
|
||||
from .const import DATA_COORDINATOR, DOMAIN, SENSOR_TEMPERATURE
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
SENSOR_TYPES = {SENSOR_TEMPERATURE: ("Temperature", "temperature", TEMP_CELSIUS)}
|
||||
|
||||
async def async_setup_entry(hass, entry, async_add_entities):
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: Callable
|
||||
):
|
||||
"""Set up Notion sensors based on a config entry."""
|
||||
notion = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
|
||||
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id]
|
||||
|
||||
sensor_list = []
|
||||
for task_id, task in notion.tasks.items():
|
||||
for task_id, task in coordinator.data["tasks"].items():
|
||||
if task["task_type"] not in SENSOR_TYPES:
|
||||
continue
|
||||
|
||||
name, device_class, unit = SENSOR_TYPES[task["task_type"]]
|
||||
sensor = notion.sensors[task["sensor_id"]]
|
||||
sensor = coordinator.data["sensors"][task["sensor_id"]]
|
||||
|
||||
sensor_list.append(
|
||||
NotionSensor(
|
||||
notion,
|
||||
coordinator,
|
||||
task_id,
|
||||
sensor["id"],
|
||||
sensor["bridge"]["id"],
|
||||
|
@ -34,42 +42,50 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||
)
|
||||
)
|
||||
|
||||
async_add_entities(sensor_list, True)
|
||||
async_add_entities(sensor_list)
|
||||
|
||||
|
||||
class NotionSensor(NotionEntity):
|
||||
"""Define a Notion sensor."""
|
||||
|
||||
def __init__(
|
||||
self, notion, task_id, sensor_id, bridge_id, system_id, name, device_class, unit
|
||||
self,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
task_id: str,
|
||||
sensor_id: str,
|
||||
bridge_id: str,
|
||||
system_id: str,
|
||||
name: str,
|
||||
device_class: str,
|
||||
unit: str,
|
||||
):
|
||||
"""Initialize the entity."""
|
||||
super().__init__(
|
||||
notion, task_id, sensor_id, bridge_id, system_id, name, device_class
|
||||
coordinator, task_id, sensor_id, bridge_id, system_id, name, device_class
|
||||
)
|
||||
|
||||
self._unit = unit
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
def state(self) -> str:
|
||||
"""Return the state of the sensor."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
def unit_of_measurement(self) -> str:
|
||||
"""Return the unit of measurement."""
|
||||
return self._unit
|
||||
|
||||
@callback
|
||||
def update_from_latest_data(self):
|
||||
def _async_update_from_latest_data(self) -> None:
|
||||
"""Fetch new state data for the sensor."""
|
||||
task = self._notion.tasks[self._task_id]
|
||||
task = self._coordinator.data["tasks"][self._task_id]
|
||||
|
||||
if task["task_type"] == SENSOR_TEMPERATURE:
|
||||
self._state = round(float(task["status"]["value"]), 1)
|
||||
else:
|
||||
_LOGGER.error(
|
||||
"Unknown task type: %s: %s",
|
||||
self._notion.sensors[self._sensor_id],
|
||||
self._coordinator.data["sensors"][self._sensor_id],
|
||||
task["task_type"],
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue