2019-07-09 08:29:06 +00:00
|
|
|
"""Support for Notion binary sensors."""
|
|
|
|
import logging
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
2020-03-17 11:01:40 +00:00
|
|
|
from homeassistant.core import callback
|
2019-07-09 08:29:06 +00:00
|
|
|
|
|
|
|
from . import (
|
2019-07-31 19:25:30 +00:00
|
|
|
BINARY_SENSOR_TYPES,
|
|
|
|
SENSOR_BATTERY,
|
|
|
|
SENSOR_DOOR,
|
|
|
|
SENSOR_GARAGE_DOOR,
|
|
|
|
SENSOR_LEAK,
|
|
|
|
SENSOR_MISSING,
|
|
|
|
SENSOR_SAFE,
|
|
|
|
SENSOR_SLIDING,
|
|
|
|
SENSOR_SMOKE_CO,
|
|
|
|
SENSOR_WINDOW_HINGED_HORIZONTAL,
|
|
|
|
SENSOR_WINDOW_HINGED_VERTICAL,
|
|
|
|
NotionEntity,
|
|
|
|
)
|
2019-07-09 08:29:06 +00:00
|
|
|
from .const import DATA_CLIENT, DOMAIN
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
|
|
"""Set up Notion sensors based on a config entry."""
|
|
|
|
notion = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
|
|
|
|
|
|
|
|
sensor_list = []
|
|
|
|
for task_id, task in notion.tasks.items():
|
2019-07-31 19:25:30 +00:00
|
|
|
if task["task_type"] not in BINARY_SENSOR_TYPES:
|
2019-07-09 08:29:06 +00:00
|
|
|
continue
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
name, device_class = BINARY_SENSOR_TYPES[task["task_type"]]
|
|
|
|
sensor = notion.sensors[task["sensor_id"]]
|
2019-07-09 08:29:06 +00:00
|
|
|
|
|
|
|
sensor_list.append(
|
|
|
|
NotionBinarySensor(
|
|
|
|
notion,
|
|
|
|
task_id,
|
2019-07-31 19:25:30 +00:00
|
|
|
sensor["id"],
|
|
|
|
sensor["bridge"]["id"],
|
|
|
|
sensor["system_id"],
|
2019-07-09 08:29:06 +00:00
|
|
|
name,
|
2019-07-31 19:25:30 +00:00
|
|
|
device_class,
|
|
|
|
)
|
|
|
|
)
|
2019-07-09 08:29:06 +00:00
|
|
|
|
|
|
|
async_add_entities(sensor_list, True)
|
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class NotionBinarySensor(NotionEntity, BinarySensorEntity):
|
2019-07-09 08:29:06 +00:00
|
|
|
"""Define a Notion sensor."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return whether the sensor is on or off."""
|
|
|
|
task = self._notion.tasks[self._task_id]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if task["task_type"] == SENSOR_BATTERY:
|
|
|
|
return self._state != "battery_good"
|
|
|
|
if task["task_type"] in (
|
|
|
|
SENSOR_DOOR,
|
|
|
|
SENSOR_GARAGE_DOOR,
|
|
|
|
SENSOR_SAFE,
|
|
|
|
SENSOR_SLIDING,
|
|
|
|
SENSOR_WINDOW_HINGED_HORIZONTAL,
|
|
|
|
SENSOR_WINDOW_HINGED_VERTICAL,
|
|
|
|
):
|
|
|
|
return self._state != "closed"
|
|
|
|
if task["task_type"] == SENSOR_LEAK:
|
|
|
|
return self._state != "no_leak"
|
|
|
|
if task["task_type"] == SENSOR_MISSING:
|
|
|
|
return self._state == "not_missing"
|
|
|
|
if task["task_type"] == SENSOR_SMOKE_CO:
|
|
|
|
return self._state != "no_alarm"
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2020-03-17 11:01:40 +00:00
|
|
|
@callback
|
|
|
|
def update_from_latest_data(self):
|
2019-07-09 08:29:06 +00:00
|
|
|
"""Fetch new state data for the sensor."""
|
|
|
|
task = self._notion.tasks[self._task_id]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = task["status"]["value"]
|