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,11 +1,15 @@
|
|||
"""Support for Notion binary sensors."""
|
||||
import logging
|
||||
from typing import Callable
|
||||
|
||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
|
||||
from . import (
|
||||
BINARY_SENSOR_TYPES,
|
||||
from . import NotionEntity
|
||||
from .const import (
|
||||
DATA_COORDINATOR,
|
||||
DOMAIN,
|
||||
SENSOR_BATTERY,
|
||||
SENSOR_DOOR,
|
||||
SENSOR_GARAGE_DOOR,
|
||||
|
@ -16,28 +20,41 @@ from . import (
|
|||
SENSOR_SMOKE_CO,
|
||||
SENSOR_WINDOW_HINGED_HORIZONTAL,
|
||||
SENSOR_WINDOW_HINGED_VERTICAL,
|
||||
NotionEntity,
|
||||
)
|
||||
from .const import DATA_CLIENT, DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
BINARY_SENSOR_TYPES = {
|
||||
SENSOR_BATTERY: ("Low Battery", "battery"),
|
||||
SENSOR_DOOR: ("Door", "door"),
|
||||
SENSOR_GARAGE_DOOR: ("Garage Door", "garage_door"),
|
||||
SENSOR_LEAK: ("Leak Detector", "moisture"),
|
||||
SENSOR_MISSING: ("Missing", "connectivity"),
|
||||
SENSOR_SAFE: ("Safe", "door"),
|
||||
SENSOR_SLIDING: ("Sliding Door/Window", "door"),
|
||||
SENSOR_SMOKE_CO: ("Smoke/Carbon Monoxide Detector", "smoke"),
|
||||
SENSOR_WINDOW_HINGED_HORIZONTAL: ("Hinged Window", "window"),
|
||||
SENSOR_WINDOW_HINGED_VERTICAL: ("Hinged Window", "window"),
|
||||
}
|
||||
|
||||
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 BINARY_SENSOR_TYPES:
|
||||
continue
|
||||
|
||||
name, device_class = BINARY_SENSOR_TYPES[task["task_type"]]
|
||||
sensor = notion.sensors[task["sensor_id"]]
|
||||
sensor = coordinator.data["sensors"][task["sensor_id"]]
|
||||
|
||||
sensor_list.append(
|
||||
NotionBinarySensor(
|
||||
notion,
|
||||
coordinator,
|
||||
task_id,
|
||||
sensor["id"],
|
||||
sensor["bridge"]["id"],
|
||||
|
@ -47,16 +64,21 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||
)
|
||||
)
|
||||
|
||||
async_add_entities(sensor_list, True)
|
||||
async_add_entities(sensor_list)
|
||||
|
||||
|
||||
class NotionBinarySensor(NotionEntity, BinarySensorEntity):
|
||||
"""Define a Notion sensor."""
|
||||
|
||||
@callback
|
||||
def _async_update_from_latest_data(self) -> None:
|
||||
"""Fetch new state data for the sensor."""
|
||||
self._state = self._coordinator.data["tasks"][self._task_id]["status"]["value"]
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
def is_on(self) -> bool:
|
||||
"""Return whether the sensor is on or off."""
|
||||
task = self._notion.tasks[self._task_id]
|
||||
task = self._coordinator.data["tasks"][self._task_id]
|
||||
|
||||
if task["task_type"] == SENSOR_BATTERY:
|
||||
return self._state != "battery_good"
|
||||
|
@ -75,10 +97,3 @@ class NotionBinarySensor(NotionEntity, BinarySensorEntity):
|
|||
return self._state == "not_missing"
|
||||
if task["task_type"] == SENSOR_SMOKE_CO:
|
||||
return self._state != "no_alarm"
|
||||
|
||||
@callback
|
||||
def update_from_latest_data(self):
|
||||
"""Fetch new state data for the sensor."""
|
||||
task = self._notion.tasks[self._task_id]
|
||||
|
||||
self._state = task["status"]["value"]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue