2020-01-31 16:33:00 +00:00
|
|
|
"""Support for Amcrest IP camera binary sensors."""
|
2021-03-23 13:36:43 +00:00
|
|
|
from contextlib import suppress
|
2019-04-09 13:21:47 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
2019-05-31 20:41:48 +00:00
|
|
|
from amcrest import AmcrestError
|
2020-06-25 06:20:39 +00:00
|
|
|
import voluptuous as vol
|
2019-05-31 20:41:48 +00:00
|
|
|
|
2019-04-09 13:21:47 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_CLASS_CONNECTIVITY,
|
|
|
|
DEVICE_CLASS_MOTION,
|
2020-07-05 22:54:17 +00:00
|
|
|
DEVICE_CLASS_SOUND,
|
2020-04-23 19:57:07 +00:00
|
|
|
BinarySensorEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-12-09 12:57:24 +00:00
|
|
|
from homeassistant.const import CONF_BINARY_SENSORS, CONF_NAME
|
2020-03-20 04:07:21 +00:00
|
|
|
from homeassistant.core import callback
|
2019-06-08 04:46:49 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2020-06-25 06:20:39 +00:00
|
|
|
from homeassistant.util import Throttle
|
2019-04-25 05:39:49 +00:00
|
|
|
|
2019-06-08 04:46:49 +00:00
|
|
|
from .const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
BINARY_SENSOR_SCAN_INTERVAL_SECS,
|
|
|
|
DATA_AMCREST,
|
|
|
|
DEVICES,
|
2020-03-20 04:07:21 +00:00
|
|
|
SENSOR_DEVICE_CLASS,
|
|
|
|
SENSOR_EVENT_CODE,
|
|
|
|
SENSOR_NAME,
|
|
|
|
SERVICE_EVENT,
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_UPDATE,
|
|
|
|
)
|
2019-06-08 04:46:49 +00:00
|
|
|
from .helpers import log_update_error, service_signal
|
2019-04-09 13:21:47 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-04-25 05:39:49 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=BINARY_SENSOR_SCAN_INTERVAL_SECS)
|
2020-06-25 06:20:39 +00:00
|
|
|
_ONLINE_SCAN_INTERVAL = timedelta(seconds=60 - BINARY_SENSOR_SCAN_INTERVAL_SECS)
|
2019-04-25 05:39:49 +00:00
|
|
|
|
2020-07-05 22:54:17 +00:00
|
|
|
BINARY_SENSOR_AUDIO_DETECTED = "audio_detected"
|
|
|
|
BINARY_SENSOR_AUDIO_DETECTED_POLLED = "audio_detected_polled"
|
2019-07-31 19:25:30 +00:00
|
|
|
BINARY_SENSOR_MOTION_DETECTED = "motion_detected"
|
2020-06-25 06:20:39 +00:00
|
|
|
BINARY_SENSOR_MOTION_DETECTED_POLLED = "motion_detected_polled"
|
2019-07-31 19:25:30 +00:00
|
|
|
BINARY_SENSOR_ONLINE = "online"
|
2021-03-18 11:32:08 +00:00
|
|
|
BINARY_SENSOR_CROSSLINE_DETECTED = "crossline_detected"
|
|
|
|
BINARY_SENSOR_CROSSLINE_DETECTED_POLLED = "crossline_detected_polled"
|
2020-06-25 06:20:39 +00:00
|
|
|
BINARY_POLLED_SENSORS = [
|
2020-07-05 22:54:17 +00:00
|
|
|
BINARY_SENSOR_AUDIO_DETECTED_POLLED,
|
2020-06-25 06:20:39 +00:00
|
|
|
BINARY_SENSOR_MOTION_DETECTED_POLLED,
|
|
|
|
BINARY_SENSOR_ONLINE,
|
|
|
|
]
|
2020-07-05 22:54:17 +00:00
|
|
|
_AUDIO_DETECTED_PARAMS = ("Audio Detected", DEVICE_CLASS_SOUND, "AudioMutation")
|
2020-06-25 06:20:39 +00:00
|
|
|
_MOTION_DETECTED_PARAMS = ("Motion Detected", DEVICE_CLASS_MOTION, "VideoMotion")
|
2021-03-18 11:32:08 +00:00
|
|
|
_CROSSLINE_DETECTED_PARAMS = (
|
|
|
|
"CrossLine Detected",
|
|
|
|
DEVICE_CLASS_MOTION,
|
|
|
|
"CrossLineDetection",
|
|
|
|
)
|
2021-08-10 12:57:57 +00:00
|
|
|
RAW_BINARY_SENSORS = {
|
2020-07-05 22:54:17 +00:00
|
|
|
BINARY_SENSOR_AUDIO_DETECTED: _AUDIO_DETECTED_PARAMS,
|
|
|
|
BINARY_SENSOR_AUDIO_DETECTED_POLLED: _AUDIO_DETECTED_PARAMS,
|
2020-06-25 06:20:39 +00:00
|
|
|
BINARY_SENSOR_MOTION_DETECTED: _MOTION_DETECTED_PARAMS,
|
|
|
|
BINARY_SENSOR_MOTION_DETECTED_POLLED: _MOTION_DETECTED_PARAMS,
|
2021-03-18 11:32:08 +00:00
|
|
|
BINARY_SENSOR_CROSSLINE_DETECTED: _CROSSLINE_DETECTED_PARAMS,
|
|
|
|
BINARY_SENSOR_CROSSLINE_DETECTED_POLLED: _CROSSLINE_DETECTED_PARAMS,
|
2020-03-20 04:07:21 +00:00
|
|
|
BINARY_SENSOR_ONLINE: ("Online", DEVICE_CLASS_CONNECTIVITY, None),
|
|
|
|
}
|
|
|
|
BINARY_SENSORS = {
|
|
|
|
k: dict(zip((SENSOR_NAME, SENSOR_DEVICE_CLASS, SENSOR_EVENT_CODE), v))
|
2021-08-10 12:57:57 +00:00
|
|
|
for k, v in RAW_BINARY_SENSORS.items()
|
2019-04-25 05:39:49 +00:00
|
|
|
}
|
2020-06-25 06:20:39 +00:00
|
|
|
_EXCLUSIVE_OPTIONS = [
|
|
|
|
{BINARY_SENSOR_MOTION_DETECTED, BINARY_SENSOR_MOTION_DETECTED_POLLED},
|
2021-03-18 11:32:08 +00:00
|
|
|
{BINARY_SENSOR_CROSSLINE_DETECTED, BINARY_SENSOR_CROSSLINE_DETECTED_POLLED},
|
2020-06-25 06:20:39 +00:00
|
|
|
]
|
2019-04-09 13:21:47 +00:00
|
|
|
|
2020-03-20 04:07:21 +00:00
|
|
|
_UPDATE_MSG = "Updating %s binary sensor"
|
|
|
|
|
2019-04-09 13:21:47 +00:00
|
|
|
|
2020-06-25 06:20:39 +00:00
|
|
|
def check_binary_sensors(value):
|
|
|
|
"""Validate binary sensor configurations."""
|
|
|
|
for exclusive_options in _EXCLUSIVE_OPTIONS:
|
|
|
|
if len(set(value) & exclusive_options) > 1:
|
|
|
|
raise vol.Invalid(
|
|
|
|
f"must contain at most one of {', '.join(exclusive_options)}."
|
|
|
|
)
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2019-04-09 13:21:47 +00:00
|
|
|
"""Set up a binary sensor for an Amcrest IP Camera."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
2019-04-25 05:39:49 +00:00
|
|
|
name = discovery_info[CONF_NAME]
|
2019-06-08 04:46:49 +00:00
|
|
|
device = hass.data[DATA_AMCREST][DEVICES][name]
|
2019-04-25 05:39:49 +00:00
|
|
|
async_add_entities(
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
|
|
|
AmcrestBinarySensor(name, device, sensor_type)
|
|
|
|
for sensor_type in discovery_info[CONF_BINARY_SENSORS]
|
|
|
|
],
|
|
|
|
True,
|
|
|
|
)
|
2019-04-09 13:21:47 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class AmcrestBinarySensor(BinarySensorEntity):
|
2019-04-09 13:21:47 +00:00
|
|
|
"""Binary sensor for Amcrest camera."""
|
|
|
|
|
2019-04-25 05:39:49 +00:00
|
|
|
def __init__(self, name, device, sensor_type):
|
2019-04-09 13:21:47 +00:00
|
|
|
"""Initialize entity."""
|
2020-03-20 04:07:21 +00:00
|
|
|
self._name = f"{name} {BINARY_SENSORS[sensor_type][SENSOR_NAME]}"
|
2019-06-08 04:46:49 +00:00
|
|
|
self._signal_name = name
|
2019-04-25 05:39:49 +00:00
|
|
|
self._api = device.api
|
2019-04-09 13:21:47 +00:00
|
|
|
self._sensor_type = sensor_type
|
|
|
|
self._state = None
|
2020-03-20 04:07:21 +00:00
|
|
|
self._device_class = BINARY_SENSORS[sensor_type][SENSOR_DEVICE_CLASS]
|
|
|
|
self._event_code = BINARY_SENSORS[sensor_type][SENSOR_EVENT_CODE]
|
|
|
|
self._unsub_dispatcher = []
|
2019-06-08 04:46:49 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Return True if entity has to be polled for state."""
|
2020-06-25 06:20:39 +00:00
|
|
|
return self._sensor_type in BINARY_POLLED_SENSORS
|
2019-04-09 13:21:47 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return entity name."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return if entity is on."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return device class."""
|
2019-06-08 04:46:49 +00:00
|
|
|
return self._device_class
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self._sensor_type == BINARY_SENSOR_ONLINE or self._api.available
|
2019-04-09 13:21:47 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update entity."""
|
2020-03-20 04:07:21 +00:00
|
|
|
if self._sensor_type == BINARY_SENSOR_ONLINE:
|
|
|
|
self._update_online()
|
|
|
|
else:
|
|
|
|
self._update_others()
|
|
|
|
|
2020-06-25 06:20:39 +00:00
|
|
|
@Throttle(_ONLINE_SCAN_INTERVAL)
|
2020-03-20 04:07:21 +00:00
|
|
|
def _update_online(self):
|
|
|
|
if not (self._api.available or self.is_on):
|
|
|
|
return
|
|
|
|
_LOGGER.debug(_UPDATE_MSG, self._name)
|
|
|
|
if self._api.available:
|
|
|
|
# Send a command to the camera to test if we can still communicate with it.
|
|
|
|
# Override of Http.command() in __init__.py will set self._api.available
|
|
|
|
# accordingly.
|
2021-03-23 13:36:43 +00:00
|
|
|
with suppress(AmcrestError):
|
|
|
|
self._api.current_time # pylint: disable=pointless-statement
|
2020-03-20 04:07:21 +00:00
|
|
|
self._state = self._api.available
|
|
|
|
|
|
|
|
def _update_others(self):
|
2019-06-08 04:46:49 +00:00
|
|
|
if not self.available:
|
|
|
|
return
|
2020-03-20 04:07:21 +00:00
|
|
|
_LOGGER.debug(_UPDATE_MSG, self._name)
|
2019-04-09 13:21:47 +00:00
|
|
|
|
|
|
|
try:
|
2020-03-20 04:07:21 +00:00
|
|
|
self._state = "channels" in self._api.event_channels_happened(
|
|
|
|
self._event_code
|
|
|
|
)
|
2019-04-09 13:21:47 +00:00
|
|
|
except AmcrestError as error:
|
2019-07-31 19:25:30 +00:00
|
|
|
log_update_error(_LOGGER, "update", self.name, "binary sensor", error)
|
2019-06-08 04:46:49 +00:00
|
|
|
|
|
|
|
async def async_on_demand_update(self):
|
|
|
|
"""Update state."""
|
2020-06-25 06:20:39 +00:00
|
|
|
if self._sensor_type == BINARY_SENSOR_ONLINE:
|
|
|
|
_LOGGER.debug(_UPDATE_MSG, self._name)
|
|
|
|
self._state = self._api.available
|
|
|
|
self.async_write_ha_state()
|
|
|
|
return
|
2019-06-08 04:46:49 +00:00
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
2020-03-20 04:07:21 +00:00
|
|
|
@callback
|
|
|
|
def async_event_received(self, start):
|
|
|
|
"""Update state from received event."""
|
|
|
|
_LOGGER.debug(_UPDATE_MSG, self._name)
|
|
|
|
self._state = start
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2019-06-08 04:46:49 +00:00
|
|
|
async def async_added_to_hass(self):
|
2020-03-20 04:07:21 +00:00
|
|
|
"""Subscribe to signals."""
|
|
|
|
self._unsub_dispatcher.append(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
|
|
|
service_signal(SERVICE_UPDATE, self._signal_name),
|
|
|
|
self.async_on_demand_update,
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-06-25 06:20:39 +00:00
|
|
|
if self._event_code and self._sensor_type not in BINARY_POLLED_SENSORS:
|
2020-03-20 04:07:21 +00:00
|
|
|
self._unsub_dispatcher.append(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
|
|
|
service_signal(SERVICE_EVENT, self._signal_name, self._event_code),
|
|
|
|
self.async_event_received,
|
|
|
|
)
|
|
|
|
)
|
2019-06-08 04:46:49 +00:00
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self):
|
|
|
|
"""Disconnect from update signal."""
|
2020-03-20 04:07:21 +00:00
|
|
|
for unsub_dispatcher in self._unsub_dispatcher:
|
|
|
|
unsub_dispatcher()
|