2019-02-13 20:21:14 +00:00
|
|
|
"""Support for ZoneMinder camera streaming."""
|
2017-02-21 06:17:11 +00:00
|
|
|
import logging
|
|
|
|
|
2019-03-17 03:44:05 +00:00
|
|
|
from homeassistant.components.mjpeg.camera import (
|
2019-01-03 18:56:36 +00:00
|
|
|
CONF_MJPEG_URL, CONF_STILL_IMAGE_URL, MjpegCamera, filter_urllib3_logging)
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_VERIFY_SSL
|
|
|
|
|
|
|
|
from . import DOMAIN as ZONEMINDER_DOMAIN
|
2017-02-21 06:17:11 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-09-15 06:44:48 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the ZoneMinder cameras."""
|
2019-01-03 18:56:36 +00:00
|
|
|
filter_urllib3_logging()
|
2018-09-15 06:44:48 +00:00
|
|
|
cameras = []
|
2019-01-16 09:15:13 +00:00
|
|
|
for zm_client in hass.data[ZONEMINDER_DOMAIN].values():
|
|
|
|
monitors = zm_client.get_monitors()
|
|
|
|
if not monitors:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Could not fetch monitors from ZoneMinder host: %s"
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
for monitor in monitors:
|
|
|
|
_LOGGER.info("Initializing camera %s", monitor.id)
|
|
|
|
cameras.append(ZoneMinderCamera(monitor, zm_client.verify_ssl))
|
2018-09-15 06:44:48 +00:00
|
|
|
add_entities(cameras)
|
2017-03-21 06:25:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ZoneMinderCamera(MjpegCamera):
|
|
|
|
"""Representation of a ZoneMinder Monitor Stream."""
|
|
|
|
|
2018-12-18 10:22:47 +00:00
|
|
|
def __init__(self, monitor, verify_ssl):
|
2017-03-21 06:25:10 +00:00
|
|
|
"""Initialize as a subclass of MjpegCamera."""
|
2018-09-15 06:44:48 +00:00
|
|
|
device_info = {
|
|
|
|
CONF_NAME: monitor.name,
|
|
|
|
CONF_MJPEG_URL: monitor.mjpeg_image_url,
|
2018-12-18 10:22:47 +00:00
|
|
|
CONF_STILL_IMAGE_URL: monitor.still_image_url,
|
|
|
|
CONF_VERIFY_SSL: verify_ssl
|
2018-09-15 06:44:48 +00:00
|
|
|
}
|
2018-09-15 09:26:29 +00:00
|
|
|
super().__init__(device_info)
|
2017-03-21 06:25:10 +00:00
|
|
|
self._is_recording = None
|
2018-12-14 07:10:54 +00:00
|
|
|
self._is_available = None
|
2018-09-15 06:44:48 +00:00
|
|
|
self._monitor = monitor
|
2017-03-21 06:25:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Update the recording state periodically."""
|
|
|
|
return True
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update our recording state from the ZM API."""
|
2018-09-15 06:44:48 +00:00
|
|
|
_LOGGER.debug("Updating camera state for monitor %i", self._monitor.id)
|
|
|
|
self._is_recording = self._monitor.is_recording
|
2018-12-14 07:10:54 +00:00
|
|
|
self._is_available = self._monitor.is_available
|
2017-03-21 06:25:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_recording(self):
|
|
|
|
"""Return whether the monitor is in alarm mode."""
|
|
|
|
return self._is_recording
|
2018-12-14 07:10:54 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self._is_available
|