2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Minut Point binary sensors."""
|
2018-11-19 11:52:21 +00:00
|
|
|
import logging
|
|
|
|
|
2018-12-13 15:40:56 +00:00
|
|
|
from homeassistant.components.binary_sensor import DOMAIN, BinarySensorDevice
|
2018-11-19 11:52:21 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import MinutPointEntity
|
|
|
|
from .const import DOMAIN as POINT_DOMAIN, POINT_DISCOVERY_NEW, SIGNAL_WEBHOOK
|
|
|
|
|
2018-11-19 11:52:21 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
EVENTS = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"battery": ("battery_low", ""), # On means low, Off means normal
|
|
|
|
"button_press": ( # On means the button was pressed, Off means normal
|
|
|
|
"short_button_press",
|
|
|
|
"",
|
|
|
|
),
|
|
|
|
"cold": ( # On means cold, Off means normal
|
|
|
|
"temperature_low",
|
|
|
|
"temperature_risen_normal",
|
|
|
|
),
|
|
|
|
"connectivity": ( # On means connected, Off means disconnected
|
|
|
|
"device_online",
|
|
|
|
"device_offline",
|
|
|
|
),
|
|
|
|
"dry": ( # On means too dry, Off means normal
|
|
|
|
"humidity_low",
|
|
|
|
"humidity_risen_normal",
|
|
|
|
),
|
|
|
|
"heat": ( # On means hot, Off means normal
|
|
|
|
"temperature_high",
|
|
|
|
"temperature_dropped_normal",
|
|
|
|
),
|
|
|
|
"moisture": ( # On means wet, Off means dry
|
|
|
|
"humidity_high",
|
|
|
|
"humidity_dropped_normal",
|
|
|
|
),
|
|
|
|
"sound": ( # On means sound detected, Off means no sound (clear)
|
|
|
|
"avg_sound_high",
|
|
|
|
"sound_level_dropped_normal",
|
|
|
|
),
|
|
|
|
"tamper": ("tamper", ""), # On means the point was removed or attached
|
2018-11-19 11:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up a Point's binary sensors based on a config entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-12-03 15:50:05 +00:00
|
|
|
async def async_discover_sensor(device_id):
|
|
|
|
"""Discover and add a discovered sensor."""
|
|
|
|
client = hass.data[POINT_DOMAIN][config_entry.entry_id]
|
|
|
|
async_add_entities(
|
2019-07-31 19:25:30 +00:00
|
|
|
(
|
|
|
|
MinutPointBinarySensor(client, device_id, device_class)
|
|
|
|
for device_class in EVENTS
|
|
|
|
),
|
|
|
|
True,
|
|
|
|
)
|
2018-12-03 15:50:05 +00:00
|
|
|
|
|
|
|
async_dispatcher_connect(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, POINT_DISCOVERY_NEW.format(DOMAIN, POINT_DOMAIN), async_discover_sensor
|
|
|
|
)
|
2018-11-19 11:52:21 +00:00
|
|
|
|
|
|
|
|
2018-11-22 15:43:10 +00:00
|
|
|
class MinutPointBinarySensor(MinutPointEntity, BinarySensorDevice):
|
2018-11-19 11:52:21 +00:00
|
|
|
"""The platform class required by Home Assistant."""
|
|
|
|
|
|
|
|
def __init__(self, point_client, device_id, device_class):
|
2019-02-14 04:35:12 +00:00
|
|
|
"""Initialize the binary sensor."""
|
2018-11-19 11:52:21 +00:00
|
|
|
super().__init__(point_client, device_id, device_class)
|
|
|
|
|
|
|
|
self._async_unsub_hook_dispatcher_connect = None
|
|
|
|
self._events = EVENTS[device_class]
|
|
|
|
self._is_on = None
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
2019-02-14 04:35:12 +00:00
|
|
|
"""Call when entity is added to HOme Assistant."""
|
2018-11-19 11:52:21 +00:00
|
|
|
await super().async_added_to_hass()
|
|
|
|
self._async_unsub_hook_dispatcher_connect = async_dispatcher_connect(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, SIGNAL_WEBHOOK, self._webhook_event
|
|
|
|
)
|
2018-11-19 11:52:21 +00:00
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self):
|
|
|
|
"""Disconnect dispatcher listener when removed."""
|
|
|
|
await super().async_will_remove_from_hass()
|
|
|
|
if self._async_unsub_hook_dispatcher_connect:
|
|
|
|
self._async_unsub_hook_dispatcher_connect()
|
|
|
|
|
2019-02-12 16:15:02 +00:00
|
|
|
async def _update_callback(self):
|
2018-11-19 11:52:21 +00:00
|
|
|
"""Update the value of the sensor."""
|
|
|
|
if not self.is_updated:
|
|
|
|
return
|
|
|
|
if self._events[0] in self.device.ongoing_events:
|
|
|
|
self._is_on = True
|
|
|
|
else:
|
|
|
|
self._is_on = None
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2018-11-19 11:52:21 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _webhook_event(self, data, webhook):
|
|
|
|
"""Process new event from the webhook."""
|
|
|
|
if self.device.webhook != webhook:
|
|
|
|
return
|
2019-07-31 19:25:30 +00:00
|
|
|
_type = data.get("event", {}).get("type")
|
|
|
|
_device_id = data.get("event", {}).get("device_id")
|
2019-02-14 04:27:17 +00:00
|
|
|
if _type not in self._events or _device_id != self.device.device_id:
|
2018-11-19 11:52:21 +00:00
|
|
|
return
|
2019-08-02 21:20:07 +00:00
|
|
|
_LOGGER.debug("Received webhook: %s", _type)
|
2018-11-19 11:52:21 +00:00
|
|
|
if _type == self._events[0]:
|
|
|
|
self._is_on = True
|
|
|
|
if _type == self._events[1]:
|
|
|
|
self._is_on = None
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2018-11-19 11:52:21 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return the state of the binary sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.device_class == "connectivity":
|
2018-11-19 11:52:21 +00:00
|
|
|
# connectivity is the other way around.
|
|
|
|
return not self._is_on
|
|
|
|
return self._is_on
|