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