hass-core/homeassistant/components/notion/sensor.py
Aaron Bach c2e843cbc3 Add support for Notion Home Monitoring (#24634)
* Add support for Notion Home Monitoring

* Updated coverage

* Removed auto-generated translations

* Stale docstrings

* Corrected hardware version

* Fixed binary sensor representation

* Cleanup and update protection

* Updated log message

* Cleaned up is_on

* Updated docstring

* Modified which data is updated during async_update

* Added more checks during update

* More cleanup

* Fixed unhandled exception

* Owner-requested changes (round 1)

* Fixed incorrect scan interval retrieval

* Ugh

* Removed unnecessary import

* Simplified everything via dict lookups

* Ensure bridges are properly registered

* Fixed tests

* Added catch for invalid credentials

* Ensure bridge ID is updated as necessary

* Updated method name

* Simplified bridge update

* Add support for updating bridge via_device_id

* Device update guard clause

* Removed excess whitespace

* Whitespace

* Owner comments

* Member comments
2019-07-09 10:29:06 +02:00

81 lines
2.1 KiB
Python

"""Support for Notion sensors."""
import logging
from . import SENSOR_TEMPERATURE, SENSOR_TYPES, NotionEntity
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():
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_list.append(
NotionSensor(
notion,
task_id,
sensor['id'],
sensor['bridge']['id'],
sensor['system_id'],
name,
device_class,
unit
))
async_add_entities(sensor_list, True)
class NotionSensor(NotionEntity):
"""Define a Notion sensor."""
def __init__(
self,
notion,
task_id,
sensor_id,
bridge_id,
system_id,
name,
device_class,
unit):
"""Initialize the entity."""
super().__init__(
notion,
task_id,
sensor_id,
bridge_id,
system_id,
name,
device_class)
self._unit = unit
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return self._unit
async def async_update(self):
"""Fetch new state data for the sensor."""
task = self._notion.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], task['task_type'])