2019-02-13 21:21:14 +01:00
|
|
|
"""Support for deCONZ binary sensors."""
|
2019-05-27 06:56:00 +02:00
|
|
|
from pydeconz.sensor import Presence, Vibration
|
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorDevice
|
2019-09-14 19:15:18 +02:00
|
|
|
from homeassistant.const import ATTR_TEMPERATURE
|
2018-01-01 17:08:13 +01:00
|
|
|
from homeassistant.core import callback
|
2018-05-05 16:11:00 +02:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2019-04-05 02:48:24 +02:00
|
|
|
from .const import ATTR_DARK, ATTR_ON, NEW_SENSOR
|
2019-01-16 08:33:04 +01:00
|
|
|
from .deconz_device import DeconzDevice
|
2019-12-05 06:17:18 +01:00
|
|
|
from .gateway import DeconzEntityHandler, get_gateway_from_config_entry
|
2019-01-15 19:29:56 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
ATTR_ORIENTATION = "orientation"
|
|
|
|
ATTR_TILTANGLE = "tiltangle"
|
|
|
|
ATTR_VIBRATIONSTRENGTH = "vibrationstrength"
|
2019-03-26 07:43:58 +01:00
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2019-05-27 06:56:00 +02:00
|
|
|
"""Old way of setting up deCONZ platforms."""
|
2018-04-23 18:00:16 +02:00
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2018-04-23 18:00:16 +02:00
|
|
|
"""Set up the deCONZ binary sensor."""
|
2019-04-05 02:48:24 +02:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2018-11-05 16:21:44 +01:00
|
|
|
|
2019-09-06 01:38:00 +02:00
|
|
|
entity_handler = DeconzEntityHandler(gateway)
|
|
|
|
|
2018-05-05 16:11:00 +02:00
|
|
|
@callback
|
2019-11-01 22:31:22 +01:00
|
|
|
def async_add_sensor(sensors, new=True):
|
2018-05-05 16:11:00 +02:00
|
|
|
"""Add binary sensor from deCONZ."""
|
|
|
|
entities = []
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2018-05-05 16:11:00 +02:00
|
|
|
for sensor in sensors:
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2019-11-01 22:31:22 +01:00
|
|
|
if new and sensor.BINARY:
|
2019-09-06 01:38:00 +02:00
|
|
|
new_sensor = DeconzBinarySensor(sensor, gateway)
|
|
|
|
entity_handler.add_entity(new_sensor)
|
|
|
|
entities.append(new_sensor)
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
async_add_entities(entities, True)
|
2018-05-29 16:09:53 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
gateway.listeners.append(
|
|
|
|
async_dispatcher_connect(
|
2019-09-06 01:38:00 +02:00
|
|
|
hass, gateway.async_signal_new_device(NEW_SENSOR), async_add_sensor
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
|
|
|
)
|
2018-05-05 16:11:00 +02:00
|
|
|
|
2018-11-05 16:21:44 +01:00
|
|
|
async_add_sensor(gateway.api.sensors.values())
|
2018-01-01 17:08:13 +01:00
|
|
|
|
|
|
|
|
2019-01-16 08:33:04 +01:00
|
|
|
class DeconzBinarySensor(DeconzDevice, BinarySensorDevice):
|
|
|
|
"""Representation of a deCONZ binary sensor."""
|
2018-08-29 23:18:20 +02:00
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
@callback
|
2019-05-27 06:56:00 +02:00
|
|
|
def async_update_callback(self, force_update=False):
|
|
|
|
"""Update the sensor's state."""
|
|
|
|
changed = set(self._device.changed_keys)
|
2019-09-14 19:15:18 +02:00
|
|
|
keys = {"on", "reachable", "state"}
|
2019-05-27 06:56:00 +02:00
|
|
|
if force_update or any(key in changed for key in keys):
|
2018-01-01 17:08:13 +01:00
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if sensor is on."""
|
2019-01-16 08:33:04 +01:00
|
|
|
return self._device.is_tripped
|
2018-01-30 23:42:24 +01:00
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
2018-01-19 07:36:29 +01:00
|
|
|
"""Return the class of the sensor."""
|
2019-05-27 06:56:00 +02:00
|
|
|
return self._device.SENSOR_CLASS
|
2018-01-01 17:08:13 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend."""
|
2019-05-27 06:56:00 +02:00
|
|
|
return self._device.SENSOR_ICON
|
2018-01-01 17:08:13 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the sensor."""
|
2018-03-15 04:07:37 +01:00
|
|
|
attr = {}
|
2019-05-27 06:56:00 +02:00
|
|
|
|
2019-01-16 08:33:04 +01:00
|
|
|
if self._device.on is not None:
|
|
|
|
attr[ATTR_ON] = self._device.on
|
2019-05-27 06:56:00 +02:00
|
|
|
|
|
|
|
if self._device.secondary_temperature is not None:
|
|
|
|
attr[ATTR_TEMPERATURE] = self._device.secondary_temperature
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if self._device.type in Presence.ZHATYPE and self._device.dark is not None:
|
2019-01-16 08:33:04 +01:00
|
|
|
attr[ATTR_DARK] = self._device.dark
|
2019-05-27 06:56:00 +02:00
|
|
|
|
|
|
|
elif self._device.type in Vibration.ZHATYPE:
|
2019-03-26 07:43:58 +01:00
|
|
|
attr[ATTR_ORIENTATION] = self._device.orientation
|
|
|
|
attr[ATTR_TILTANGLE] = self._device.tiltangle
|
|
|
|
attr[ATTR_VIBRATIONSTRENGTH] = self._device.vibrationstrength
|
2019-05-27 06:56:00 +02:00
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
return attr
|