2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ sensors."""
|
2019-05-27 04:56:00 +00:00
|
|
|
from pydeconz.sensor import LightLevel, Switch
|
|
|
|
|
2018-05-05 13:37:40 +00:00
|
|
|
from homeassistant.const import (
|
2019-05-27 04:56:00 +00:00
|
|
|
ATTR_BATTERY_LEVEL, ATTR_TEMPERATURE, ATTR_VOLTAGE, DEVICE_CLASS_BATTERY)
|
2018-04-29 14:16:20 +00:00
|
|
|
from homeassistant.core import callback
|
2018-05-05 14:11:00 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2018-01-01 16:08:13 +00:00
|
|
|
from homeassistant.util import slugify
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
from .const import ATTR_DARK, ATTR_ON, NEW_SENSOR
|
2019-01-16 07:33:04 +00:00
|
|
|
from .deconz_device import DeconzDevice
|
2019-04-05 00:48:24 +00:00
|
|
|
from .gateway import get_gateway_from_config_entry
|
2019-01-15 18:29:56 +00:00
|
|
|
|
2018-03-15 03:07:37 +00:00
|
|
|
ATTR_CURRENT = 'current'
|
2018-04-13 06:58:57 +00:00
|
|
|
ATTR_DAYLIGHT = 'daylight'
|
2018-01-01 16:08:13 +00:00
|
|
|
ATTR_EVENT_ID = 'event_id'
|
|
|
|
|
|
|
|
|
2019-02-13 20:21:14 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass, config, async_add_entities, discovery_info=None):
|
2019-05-27 04:56:00 +00:00
|
|
|
"""Old way of setting up deCONZ platforms."""
|
2018-04-23 16:00:16 +00:00
|
|
|
pass
|
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2018-04-23 16:00:16 +00:00
|
|
|
"""Set up the deCONZ sensors."""
|
2019-04-05 00:48:24 +00:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2018-11-05 15:21:44 +00:00
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
@callback
|
|
|
|
def async_add_sensor(sensors):
|
|
|
|
"""Add sensors from deCONZ."""
|
|
|
|
entities = []
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
for sensor in sensors:
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2019-05-27 04:56:00 +00:00
|
|
|
if not sensor.BINARY and \
|
2019-04-05 00:48:24 +00:00
|
|
|
not (not gateway.allow_clip_sensor and
|
|
|
|
sensor.type.startswith('CLIP')):
|
|
|
|
|
2019-05-27 04:56:00 +00:00
|
|
|
if sensor.type in Switch.ZHATYPE:
|
2018-05-05 14:11:00 +00:00
|
|
|
if sensor.battery:
|
2018-11-05 15:21:44 +00:00
|
|
|
entities.append(DeconzBattery(sensor, gateway))
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
else:
|
2018-11-05 15:21:44 +00:00
|
|
|
entities.append(DeconzSensor(sensor, gateway))
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(entities, True)
|
2018-05-29 14:09:53 +00:00
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
gateway.listeners.append(async_dispatcher_connect(
|
|
|
|
hass, gateway.async_event_new_device(NEW_SENSOR), async_add_sensor))
|
2018-05-05 14:11:00 +00:00
|
|
|
|
2018-11-05 15:21:44 +00:00
|
|
|
async_add_sensor(gateway.api.sensors.values())
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
class DeconzSensor(DeconzDevice):
|
|
|
|
"""Representation of a deCONZ sensor."""
|
2018-08-29 21:18:20 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
@callback
|
2019-05-27 04:56:00 +00:00
|
|
|
def async_update_callback(self, force_update=False):
|
|
|
|
"""Update the sensor's state."""
|
|
|
|
changed = set(self._device.changed_keys)
|
|
|
|
keys = {'battery', 'on', 'reachable', 'state'}
|
|
|
|
if force_update or any(key in changed for key in keys):
|
2018-01-01 16:08:13 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2019-01-16 07:33:04 +00:00
|
|
|
return self._device.state
|
2018-01-30 22:42:24 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
2018-01-19 06:36:29 +00:00
|
|
|
"""Return the class of the sensor."""
|
2019-05-27 04:56:00 +00:00
|
|
|
return self._device.SENSOR_CLASS
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend."""
|
2019-05-27 04:56:00 +00:00
|
|
|
return self._device.SENSOR_ICON
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Return the unit of measurement of this sensor."""
|
2019-05-27 04:56:00 +00:00
|
|
|
return self._device.SENSOR_UNIT
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the sensor."""
|
2018-03-15 03:07:37 +00:00
|
|
|
attr = {}
|
2019-01-16 07:33:04 +00:00
|
|
|
if self._device.battery:
|
|
|
|
attr[ATTR_BATTERY_LEVEL] = self._device.battery
|
2019-05-27 04:56:00 +00:00
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
if self._device.on is not None:
|
|
|
|
attr[ATTR_ON] = self._device.on
|
2019-05-27 04:56:00 +00:00
|
|
|
|
|
|
|
if self._device.secondary_temperature is not None:
|
|
|
|
attr[ATTR_TEMPERATURE] = self._device.secondary_temperature
|
|
|
|
|
|
|
|
if self._device.type in LightLevel.ZHATYPE and \
|
|
|
|
self._device.dark is not None:
|
2019-01-16 07:33:04 +00:00
|
|
|
attr[ATTR_DARK] = self._device.dark
|
2019-05-27 04:56:00 +00:00
|
|
|
|
2019-06-14 18:14:45 +00:00
|
|
|
if self.unit_of_measurement == 'W':
|
2019-01-16 07:33:04 +00:00
|
|
|
attr[ATTR_CURRENT] = self._device.current
|
|
|
|
attr[ATTR_VOLTAGE] = self._device.voltage
|
2019-05-27 04:56:00 +00:00
|
|
|
|
|
|
|
if self._device.SENSOR_CLASS == 'daylight':
|
2019-01-16 07:33:04 +00:00
|
|
|
attr[ATTR_DAYLIGHT] = self._device.daylight
|
2019-05-27 04:56:00 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
return attr
|
|
|
|
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
class DeconzBattery(DeconzDevice):
|
2018-01-01 16:08:13 +00:00
|
|
|
"""Battery class for when a device is only represented as an event."""
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
def __init__(self, device, gateway):
|
2018-01-01 16:08:13 +00:00
|
|
|
"""Register dispatcher callback for update of battery state."""
|
2019-01-16 07:33:04 +00:00
|
|
|
super().__init__(device, gateway)
|
2018-11-05 15:21:44 +00:00
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
self._name = '{} {}'.format(self._device.name, 'Battery Level')
|
2018-01-01 16:08:13 +00:00
|
|
|
self._unit_of_measurement = "%"
|
|
|
|
|
|
|
|
@callback
|
2019-05-27 04:56:00 +00:00
|
|
|
def async_update_callback(self, force_update=False):
|
2018-01-01 16:08:13 +00:00
|
|
|
"""Update the battery's state, if needed."""
|
2019-05-27 04:56:00 +00:00
|
|
|
changed = set(self._device.changed_keys)
|
|
|
|
keys = {'battery', 'reachable'}
|
|
|
|
if force_update or any(key in changed for key in keys):
|
2018-01-01 16:08:13 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the battery."""
|
2019-01-16 07:33:04 +00:00
|
|
|
return self._device.battery
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the battery."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
2018-01-19 06:36:29 +00:00
|
|
|
"""Return the class of the sensor."""
|
2018-05-05 13:37:40 +00:00
|
|
|
return DEVICE_CLASS_BATTERY
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement of this entity."""
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the battery."""
|
|
|
|
attr = {
|
2019-01-16 07:33:04 +00:00
|
|
|
ATTR_EVENT_ID: slugify(self._device.name),
|
2018-01-01 16:08:13 +00:00
|
|
|
}
|
|
|
|
return attr
|