Convert amcrest binary sensors from poll to stream (#32818)
* Convert amcrest binary sensors from poll to stream - Bump amcrest package to 1.6.0. - For online binary sensor poll camera periodically to test communications in case configuration & usage results in no other communication to camera. - Start a separate thread to call camera's event_stream method since it never returns. - Convert all received events into signals that cause corresponding sensors to update. - Use camera's generic event_channels_happened method to update sensors at startup, and whenever camera comes back online after being unavailable. * Changes per review * Changes per review 2 * Changes per review 3 - Move event stream decoding to amcrest package. - Change name of event processing threads so global counter is no longer required. - Bump amcrest package to 1.7.0.
This commit is contained in:
parent
d8e3e9abaa
commit
0ed7bc3b8e
6 changed files with 130 additions and 32 deletions
|
@ -10,12 +10,17 @@ from homeassistant.components.binary_sensor import (
|
|||
BinarySensorDevice,
|
||||
)
|
||||
from homeassistant.const import CONF_BINARY_SENSORS, CONF_NAME
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
|
||||
from .const import (
|
||||
BINARY_SENSOR_SCAN_INTERVAL_SECS,
|
||||
DATA_AMCREST,
|
||||
DEVICES,
|
||||
SENSOR_DEVICE_CLASS,
|
||||
SENSOR_EVENT_CODE,
|
||||
SENSOR_NAME,
|
||||
SERVICE_EVENT,
|
||||
SERVICE_UPDATE,
|
||||
)
|
||||
from .helpers import log_update_error, service_signal
|
||||
|
@ -26,11 +31,20 @@ SCAN_INTERVAL = timedelta(seconds=BINARY_SENSOR_SCAN_INTERVAL_SECS)
|
|||
|
||||
BINARY_SENSOR_MOTION_DETECTED = "motion_detected"
|
||||
BINARY_SENSOR_ONLINE = "online"
|
||||
# Binary sensor types are defined like: Name, device class
|
||||
BINARY_SENSORS = {
|
||||
BINARY_SENSOR_MOTION_DETECTED: ("Motion Detected", DEVICE_CLASS_MOTION),
|
||||
BINARY_SENSOR_ONLINE: ("Online", DEVICE_CLASS_CONNECTIVITY),
|
||||
BINARY_SENSOR_MOTION_DETECTED: (
|
||||
"Motion Detected",
|
||||
DEVICE_CLASS_MOTION,
|
||||
"VideoMotion",
|
||||
),
|
||||
BINARY_SENSOR_ONLINE: ("Online", DEVICE_CLASS_CONNECTIVITY, None),
|
||||
}
|
||||
BINARY_SENSORS = {
|
||||
k: dict(zip((SENSOR_NAME, SENSOR_DEVICE_CLASS, SENSOR_EVENT_CODE), v))
|
||||
for k, v in BINARY_SENSORS.items()
|
||||
}
|
||||
|
||||
_UPDATE_MSG = "Updating %s binary sensor"
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
|
@ -54,18 +68,19 @@ class AmcrestBinarySensor(BinarySensorDevice):
|
|||
|
||||
def __init__(self, name, device, sensor_type):
|
||||
"""Initialize entity."""
|
||||
self._name = f"{name} {BINARY_SENSORS[sensor_type][0]}"
|
||||
self._name = f"{name} {BINARY_SENSORS[sensor_type][SENSOR_NAME]}"
|
||||
self._signal_name = name
|
||||
self._api = device.api
|
||||
self._sensor_type = sensor_type
|
||||
self._state = None
|
||||
self._device_class = BINARY_SENSORS[sensor_type][1]
|
||||
self._unsub_dispatcher = None
|
||||
self._device_class = BINARY_SENSORS[sensor_type][SENSOR_DEVICE_CLASS]
|
||||
self._event_code = BINARY_SENSORS[sensor_type][SENSOR_EVENT_CODE]
|
||||
self._unsub_dispatcher = []
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return True if entity has to be polled for state."""
|
||||
return self._sensor_type != BINARY_SENSOR_ONLINE
|
||||
return self._sensor_type == BINARY_SENSOR_ONLINE
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
|
@ -89,16 +104,34 @@ class AmcrestBinarySensor(BinarySensorDevice):
|
|||
|
||||
def update(self):
|
||||
"""Update entity."""
|
||||
if self._sensor_type == BINARY_SENSOR_ONLINE:
|
||||
self._update_online()
|
||||
else:
|
||||
self._update_others()
|
||||
|
||||
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.
|
||||
try:
|
||||
self._api.current_time
|
||||
except AmcrestError:
|
||||
pass
|
||||
self._state = self._api.available
|
||||
|
||||
def _update_others(self):
|
||||
if not self.available:
|
||||
return
|
||||
_LOGGER.debug("Updating %s binary sensor", self._name)
|
||||
_LOGGER.debug(_UPDATE_MSG, self._name)
|
||||
|
||||
try:
|
||||
if self._sensor_type == BINARY_SENSOR_MOTION_DETECTED:
|
||||
self._state = self._api.is_motion_detected
|
||||
|
||||
elif self._sensor_type == BINARY_SENSOR_ONLINE:
|
||||
self._state = self._api.available
|
||||
self._state = "channels" in self._api.event_channels_happened(
|
||||
self._event_code
|
||||
)
|
||||
except AmcrestError as error:
|
||||
log_update_error(_LOGGER, "update", self.name, "binary sensor", error)
|
||||
|
||||
|
@ -106,14 +139,32 @@ class AmcrestBinarySensor(BinarySensorDevice):
|
|||
"""Update state."""
|
||||
self.async_schedule_update_ha_state(True)
|
||||
|
||||
@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()
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Subscribe to update signal."""
|
||||
self._unsub_dispatcher = async_dispatcher_connect(
|
||||
self.hass,
|
||||
service_signal(SERVICE_UPDATE, self._signal_name),
|
||||
self.async_on_demand_update,
|
||||
"""Subscribe to signals."""
|
||||
self._unsub_dispatcher.append(
|
||||
async_dispatcher_connect(
|
||||
self.hass,
|
||||
service_signal(SERVICE_UPDATE, self._signal_name),
|
||||
self.async_on_demand_update,
|
||||
)
|
||||
)
|
||||
if self._event_code:
|
||||
self._unsub_dispatcher.append(
|
||||
async_dispatcher_connect(
|
||||
self.hass,
|
||||
service_signal(SERVICE_EVENT, self._signal_name, self._event_code),
|
||||
self.async_event_received,
|
||||
)
|
||||
)
|
||||
|
||||
async def async_will_remove_from_hass(self):
|
||||
"""Disconnect from update signal."""
|
||||
self._unsub_dispatcher()
|
||||
for unsub_dispatcher in self._unsub_dispatcher:
|
||||
unsub_dispatcher()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue