core/homeassistant/components/axis/camera.py

117 lines
3.7 KiB
Python
Raw Normal View History

"""Support for Axis camera streaming."""
Axis component (#7381) * Added Axis hub, binary sensors and camera * Added Axis logo to static images * Added Axis logo to configurator Added Axis mdns discovery * Fixed flake8 and pylint comments * Missed a change from list to function call V5 of axis py * Added dependencies to requirements_all.txt * Clean up * Added files to coveragerc * Guide lines says to import function when needed, this makes Tox pass * Removed storing hass in config until at the end where I send it to axisdevice * Don't call update in the constructor * Don't keep hass private * Unnecessary lint ignore, following Baloobs suggestion of using NotImplementedError * Axis package not in pypi yet * Do not catch bare excepts. Device schema validations raise vol.Invalid. * setup_device still adds hass object to the config, so the need to remove it prior to writing config file still remains * Don't expect axis.conf contains correct values * Improved configuration validation * Trigger time better explains functionality than scan interval * Forgot to remove this earlier * Guideline says double qoutes for sentences * Return false from discovery if config file contains bad data * Keys in AXIS_DEVICES are serialnumber * Ordered imports in alphabetical order * Moved requirement to pypi * Moved update callback that handles trigger time to axis binary sensor * Renamed configurator instance to request_id since that is what it really is * Removed unnecessary configurator steps * Changed link in configurator to platform documentation * Add not-context-manager (#7523) * Add not-context-manager * Add missing comma * Threadsafe configurator (#7536) * Make Configurator thread safe, get_instance timing issues breaking configurator working on multiple devices * No blank lines allowed after function docstring * Fix comment Tox * Added Axis hub, binary sensors and camera * Added Axis logo to static images * Added Axis logo to configurator Added Axis mdns discovery * Fixed flake8 and pylint comments * Missed a change from list to function call V5 of axis py * Added dependencies to requirements_all.txt * Clean up * Added files to coveragerc * Guide lines says to import function when needed, this makes Tox pass * Removed storing hass in config until at the end where I send it to axisdevice * Don't call update in the constructor * Don't keep hass private * Unnecessary lint ignore, following Baloobs suggestion of using NotImplementedError * Axis package not in pypi yet * Do not catch bare excepts. Device schema validations raise vol.Invalid. * setup_device still adds hass object to the config, so the need to remove it prior to writing config file still remains * Don't expect axis.conf contains correct values * Improved configuration validation * Trigger time better explains functionality than scan interval * Forgot to remove this earlier * Guideline says double qoutes for sentences * Return false from discovery if config file contains bad data * Keys in AXIS_DEVICES are serialnumber * Ordered imports in alphabetical order * Moved requirement to pypi * Moved update callback that handles trigger time to axis binary sensor * Renamed configurator instance to request_id since that is what it really is * Removed unnecessary configurator steps * Changed link in configurator to platform documentation * No blank lines allowed after function docstring * No blank lines allowed after function docstring * Changed discovery to use axis instead of axis_mdns * Travis CI requested rerun of script/gen_requirements_all.py
2017-05-12 15:51:54 +00:00
from urllib.parse import urlencode
from homeassistant.components.camera import SUPPORT_STREAM
from homeassistant.components.mjpeg.camera import (
2019-07-31 19:25:30 +00:00
CONF_MJPEG_URL,
CONF_STILL_IMAGE_URL,
MjpegCamera,
filter_urllib3_logging,
)
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
CONF_AUTHENTICATION,
CONF_NAME,
CONF_PASSWORD,
CONF_USERNAME,
HTTP_DIGEST_AUTHENTICATION,
)
from homeassistant.helpers.dispatcher import async_dispatcher_connect
Axis component (#7381) * Added Axis hub, binary sensors and camera * Added Axis logo to static images * Added Axis logo to configurator Added Axis mdns discovery * Fixed flake8 and pylint comments * Missed a change from list to function call V5 of axis py * Added dependencies to requirements_all.txt * Clean up * Added files to coveragerc * Guide lines says to import function when needed, this makes Tox pass * Removed storing hass in config until at the end where I send it to axisdevice * Don't call update in the constructor * Don't keep hass private * Unnecessary lint ignore, following Baloobs suggestion of using NotImplementedError * Axis package not in pypi yet * Do not catch bare excepts. Device schema validations raise vol.Invalid. * setup_device still adds hass object to the config, so the need to remove it prior to writing config file still remains * Don't expect axis.conf contains correct values * Improved configuration validation * Trigger time better explains functionality than scan interval * Forgot to remove this earlier * Guideline says double qoutes for sentences * Return false from discovery if config file contains bad data * Keys in AXIS_DEVICES are serialnumber * Ordered imports in alphabetical order * Moved requirement to pypi * Moved update callback that handles trigger time to axis binary sensor * Renamed configurator instance to request_id since that is what it really is * Removed unnecessary configurator steps * Changed link in configurator to platform documentation * Add not-context-manager (#7523) * Add not-context-manager * Add missing comma * Threadsafe configurator (#7536) * Make Configurator thread safe, get_instance timing issues breaking configurator working on multiple devices * No blank lines allowed after function docstring * Fix comment Tox * Added Axis hub, binary sensors and camera * Added Axis logo to static images * Added Axis logo to configurator Added Axis mdns discovery * Fixed flake8 and pylint comments * Missed a change from list to function call V5 of axis py * Added dependencies to requirements_all.txt * Clean up * Added files to coveragerc * Guide lines says to import function when needed, this makes Tox pass * Removed storing hass in config until at the end where I send it to axisdevice * Don't call update in the constructor * Don't keep hass private * Unnecessary lint ignore, following Baloobs suggestion of using NotImplementedError * Axis package not in pypi yet * Do not catch bare excepts. Device schema validations raise vol.Invalid. * setup_device still adds hass object to the config, so the need to remove it prior to writing config file still remains * Don't expect axis.conf contains correct values * Improved configuration validation * Trigger time better explains functionality than scan interval * Forgot to remove this earlier * Guideline says double qoutes for sentences * Return false from discovery if config file contains bad data * Keys in AXIS_DEVICES are serialnumber * Ordered imports in alphabetical order * Moved requirement to pypi * Moved update callback that handles trigger time to axis binary sensor * Renamed configurator instance to request_id since that is what it really is * Removed unnecessary configurator steps * Changed link in configurator to platform documentation * No blank lines allowed after function docstring * No blank lines allowed after function docstring * Changed discovery to use axis instead of axis_mdns * Travis CI requested rerun of script/gen_requirements_all.py
2017-05-12 15:51:54 +00:00
from .axis_base import AxisEntityBase
from .const import DEFAULT_STREAM_PROFILE, DEFAULT_VIDEO_SOURCE, DOMAIN as AXIS_DOMAIN
Axis component (#7381) * Added Axis hub, binary sensors and camera * Added Axis logo to static images * Added Axis logo to configurator Added Axis mdns discovery * Fixed flake8 and pylint comments * Missed a change from list to function call V5 of axis py * Added dependencies to requirements_all.txt * Clean up * Added files to coveragerc * Guide lines says to import function when needed, this makes Tox pass * Removed storing hass in config until at the end where I send it to axisdevice * Don't call update in the constructor * Don't keep hass private * Unnecessary lint ignore, following Baloobs suggestion of using NotImplementedError * Axis package not in pypi yet * Do not catch bare excepts. Device schema validations raise vol.Invalid. * setup_device still adds hass object to the config, so the need to remove it prior to writing config file still remains * Don't expect axis.conf contains correct values * Improved configuration validation * Trigger time better explains functionality than scan interval * Forgot to remove this earlier * Guideline says double qoutes for sentences * Return false from discovery if config file contains bad data * Keys in AXIS_DEVICES are serialnumber * Ordered imports in alphabetical order * Moved requirement to pypi * Moved update callback that handles trigger time to axis binary sensor * Renamed configurator instance to request_id since that is what it really is * Removed unnecessary configurator steps * Changed link in configurator to platform documentation * Add not-context-manager (#7523) * Add not-context-manager * Add missing comma * Threadsafe configurator (#7536) * Make Configurator thread safe, get_instance timing issues breaking configurator working on multiple devices * No blank lines allowed after function docstring * Fix comment Tox * Added Axis hub, binary sensors and camera * Added Axis logo to static images * Added Axis logo to configurator Added Axis mdns discovery * Fixed flake8 and pylint comments * Missed a change from list to function call V5 of axis py * Added dependencies to requirements_all.txt * Clean up * Added files to coveragerc * Guide lines says to import function when needed, this makes Tox pass * Removed storing hass in config until at the end where I send it to axisdevice * Don't call update in the constructor * Don't keep hass private * Unnecessary lint ignore, following Baloobs suggestion of using NotImplementedError * Axis package not in pypi yet * Do not catch bare excepts. Device schema validations raise vol.Invalid. * setup_device still adds hass object to the config, so the need to remove it prior to writing config file still remains * Don't expect axis.conf contains correct values * Improved configuration validation * Trigger time better explains functionality than scan interval * Forgot to remove this earlier * Guideline says double qoutes for sentences * Return false from discovery if config file contains bad data * Keys in AXIS_DEVICES are serialnumber * Ordered imports in alphabetical order * Moved requirement to pypi * Moved update callback that handles trigger time to axis binary sensor * Renamed configurator instance to request_id since that is what it really is * Removed unnecessary configurator steps * Changed link in configurator to platform documentation * No blank lines allowed after function docstring * No blank lines allowed after function docstring * Changed discovery to use axis instead of axis_mdns * Travis CI requested rerun of script/gen_requirements_all.py
2017-05-12 15:51:54 +00:00
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Axis camera video stream."""
filter_urllib3_logging()
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
if not device.api.vapix.params.image_format:
return
async_add_entities([AxisCamera(device)])
class AxisCamera(AxisEntityBase, MjpegCamera):
"""Representation of a Axis camera."""
def __init__(self, device):
"""Initialize Axis Communications camera component."""
AxisEntityBase.__init__(self, device)
config = {
CONF_NAME: device.name,
CONF_USERNAME: device.username,
CONF_PASSWORD: device.password,
CONF_MJPEG_URL: self.mjpeg_source,
CONF_STILL_IMAGE_URL: self.image_source,
CONF_AUTHENTICATION: HTTP_DIGEST_AUTHENTICATION,
}
MjpegCamera.__init__(self, config)
async def async_added_to_hass(self):
"""Subscribe camera events."""
self.async_on_remove(
2019-07-31 19:25:30 +00:00
async_dispatcher_connect(
self.hass, self.device.signal_new_address, self._new_address
2019-07-31 19:25:30 +00:00
)
)
await super().async_added_to_hass()
@property
def supported_features(self) -> int:
"""Return supported features."""
return SUPPORT_STREAM
def _new_address(self) -> None:
"""Set new device address for video stream."""
self._mjpeg_url = self.mjpeg_source
self._still_image_url = self.image_source
@property
def unique_id(self) -> str:
"""Return a unique identifier for this device."""
return f"{self.device.unique_id}-camera"
@property
def image_source(self) -> str:
"""Return still image URL for device."""
options = self.generate_options(skip_stream_profile=True)
return f"http://{self.device.host}:{self.device.port}/axis-cgi/jpg/image.cgi{options}"
@property
def mjpeg_source(self) -> str:
"""Return mjpeg URL for device."""
options = self.generate_options()
return f"http://{self.device.host}:{self.device.port}/axis-cgi/mjpg/video.cgi{options}"
async def stream_source(self) -> str:
"""Return the stream source."""
options = self.generate_options(add_video_codec_h264=True)
return f"rtsp://{self.device.username}:{self.device.password}@{self.device.host}/axis-media/media.amp{options}"
def generate_options(
self, skip_stream_profile: bool = False, add_video_codec_h264: bool = False
) -> str:
"""Generate options for video stream."""
options_dict = {}
if add_video_codec_h264:
options_dict["videocodec"] = "h264"
if (
not skip_stream_profile
and self.device.option_stream_profile != DEFAULT_STREAM_PROFILE
):
options_dict["streamprofile"] = self.device.option_stream_profile
if self.device.option_video_source != DEFAULT_VIDEO_SOURCE:
options_dict["camera"] = self.device.option_video_source
if not options_dict:
return ""
return f"?{urlencode(options_dict)}"