mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 01:08:12 +00:00
Bump amcrest version and refactored to simplify code (#5499)
* Bumped version to Amcrest 1.1.3 and rebase self.base_url() * Refactored amcrest camera and amcrest sensor to simply object reference
This commit is contained in:
parent
59b0491d29
commit
ab19577322
@ -18,7 +18,7 @@ from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.aiohttp_client import (
|
||||
async_get_clientsession, async_aiohttp_proxy_stream)
|
||||
|
||||
REQUIREMENTS = ['amcrest==1.1.0']
|
||||
REQUIREMENTS = ['amcrest==1.1.3']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -62,13 +62,13 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up an Amcrest IP Camera."""
|
||||
from amcrest import AmcrestCamera
|
||||
data = AmcrestCamera(
|
||||
camera = AmcrestCamera(
|
||||
config.get(CONF_HOST), config.get(CONF_PORT),
|
||||
config.get(CONF_USERNAME), config.get(CONF_PASSWORD))
|
||||
config.get(CONF_USERNAME), config.get(CONF_PASSWORD)).camera
|
||||
|
||||
persistent_notification = loader.get_component('persistent_notification')
|
||||
try:
|
||||
data.camera.current_time
|
||||
camera.current_time
|
||||
# pylint: disable=broad-except
|
||||
except Exception as ex:
|
||||
_LOGGER.error("Unable to connect to Amcrest camera: %s", str(ex))
|
||||
@ -80,22 +80,18 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
notification_id=NOTIFICATION_ID)
|
||||
return False
|
||||
|
||||
add_devices([AmcrestCam(hass, config, data)])
|
||||
add_devices([AmcrestCam(hass, config, camera)])
|
||||
return True
|
||||
|
||||
|
||||
class AmcrestCam(Camera):
|
||||
"""An implementation of an Amcrest IP camera."""
|
||||
|
||||
def __init__(self, hass, device_info, data):
|
||||
def __init__(self, hass, device_info, camera):
|
||||
"""Initialize an Amcrest camera."""
|
||||
super(AmcrestCam, self).__init__()
|
||||
self._base_url = '%s://%s:%s/cgi-bin' % (
|
||||
'http',
|
||||
device_info.get(CONF_HOST),
|
||||
device_info.get(CONF_PORT)
|
||||
)
|
||||
self._data = data
|
||||
self._camera = camera
|
||||
self._base_url = self._camera.get_base_url()
|
||||
self._hass = hass
|
||||
self._name = device_info.get(CONF_NAME)
|
||||
self._resolution = RESOLUTION_LIST[device_info.get(CONF_RESOLUTION)]
|
||||
@ -110,7 +106,7 @@ class AmcrestCam(Camera):
|
||||
def camera_image(self):
|
||||
"""Return a still image reponse from the camera."""
|
||||
# Send the request to snap a picture and return raw jpg data
|
||||
response = self._data.camera.snapshot(channel=self._resolution)
|
||||
response = self._camera.snapshot(channel=self._resolution)
|
||||
return response.data
|
||||
|
||||
@asyncio.coroutine
|
||||
@ -123,10 +119,8 @@ class AmcrestCam(Camera):
|
||||
|
||||
# Otherwise, stream an MJPEG image stream directly from the camera
|
||||
websession = async_get_clientsession(self.hass)
|
||||
streaming_url = '%s/mjpg/video.cgi?channel=0&subtype=%d' % (
|
||||
self._base_url,
|
||||
self._resolution
|
||||
)
|
||||
streaming_url = '{0}mjpg/video.cgi?channel=0&subtype={1}'.format(
|
||||
self._base_url, self._resolution)
|
||||
|
||||
stream_coro = websession.get(
|
||||
streaming_url, auth=self._token, timeout=TIMEOUT)
|
||||
|
@ -20,7 +20,7 @@ import homeassistant.loader as loader
|
||||
|
||||
from requests.exceptions import HTTPError, ConnectTimeout
|
||||
|
||||
REQUIREMENTS = ['amcrest==1.1.0']
|
||||
REQUIREMENTS = ['amcrest==1.1.3']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -55,13 +55,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up a sensor for an Amcrest IP Camera."""
|
||||
from amcrest import AmcrestCamera
|
||||
|
||||
data = AmcrestCamera(
|
||||
camera = AmcrestCamera(
|
||||
config.get(CONF_HOST), config.get(CONF_PORT),
|
||||
config.get(CONF_USERNAME), config.get(CONF_PASSWORD))
|
||||
config.get(CONF_USERNAME), config.get(CONF_PASSWORD)).camera
|
||||
|
||||
persistent_notification = loader.get_component('persistent_notification')
|
||||
try:
|
||||
data.camera.current_time
|
||||
camera.current_time
|
||||
except (ConnectTimeout, HTTPError) as ex:
|
||||
_LOGGER.error("Unable to connect to Amcrest camera: %s", str(ex))
|
||||
persistent_notification.create(
|
||||
@ -74,7 +74,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
|
||||
sensors = []
|
||||
for sensor_type in config.get(CONF_MONITORED_CONDITIONS):
|
||||
sensors.append(AmcrestSensor(config, data, sensor_type))
|
||||
sensors.append(AmcrestSensor(config, camera, sensor_type))
|
||||
|
||||
add_devices(sensors, True)
|
||||
|
||||
@ -84,11 +84,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
class AmcrestSensor(Entity):
|
||||
"""A sensor implementation for Amcrest IP camera."""
|
||||
|
||||
def __init__(self, device_info, data, sensor_type):
|
||||
def __init__(self, device_info, camera, sensor_type):
|
||||
"""Initialize a sensor for Amcrest camera."""
|
||||
super(AmcrestSensor, self).__init__()
|
||||
self._attrs = {}
|
||||
self._data = data
|
||||
self._camera = camera
|
||||
self._sensor_type = sensor_type
|
||||
self._name = '{0}_{1}'.format(device_info.get(CONF_NAME),
|
||||
SENSOR_TYPES.get(self._sensor_type)[0])
|
||||
@ -122,21 +122,21 @@ class AmcrestSensor(Entity):
|
||||
|
||||
def update(self):
|
||||
"""Get the latest data and updates the state."""
|
||||
version, build_date = self._data.camera.software_information
|
||||
version, build_date = self._camera.software_information
|
||||
self._attrs['Build Date'] = build_date.split('=')[-1]
|
||||
self._attrs['Serial Number'] = self._data.camera.serial_number
|
||||
self._attrs['Serial Number'] = self._camera.serial_number
|
||||
self._attrs['Version'] = version.split('=')[-1]
|
||||
|
||||
if self._sensor_type == 'motion_detector':
|
||||
self._state = self._data.camera.is_motion_detected
|
||||
self._attrs['Record Mode'] = self._data.camera.record_mode
|
||||
self._state = self._camera.is_motion_detected
|
||||
self._attrs['Record Mode'] = self._camera.record_mode
|
||||
|
||||
elif self._sensor_type == 'ptz_preset':
|
||||
self._state = self._data.camera.ptz_presets_count
|
||||
self._state = self._camera.ptz_presets_count
|
||||
|
||||
elif self._sensor_type == 'sdcard':
|
||||
sd_used = self._data.camera.storage_used
|
||||
sd_total = self._data.camera.storage_total
|
||||
sd_used = self._camera.storage_used
|
||||
sd_total = self._camera.storage_total
|
||||
self._attrs['Total'] = '{0} {1}'.format(*sd_total)
|
||||
self._attrs['Used'] = '{0} {1}'.format(*sd_used)
|
||||
self._state = self._data.camera.percent(sd_used[0], sd_total[0])
|
||||
self._state = self._camera.percent(sd_used[0], sd_total[0])
|
||||
|
@ -38,7 +38,7 @@ aiohttp_cors==0.5.0
|
||||
|
||||
# homeassistant.components.camera.amcrest
|
||||
# homeassistant.components.sensor.amcrest
|
||||
amcrest==1.1.0
|
||||
amcrest==1.1.3
|
||||
|
||||
# homeassistant.components.media_player.anthemav
|
||||
anthemav==1.1.7
|
||||
|
Loading…
x
Reference in New Issue
Block a user