From d8d85f5a4ba40aa666f82b96789be52d68c143b9 Mon Sep 17 00:00:00 2001 From: Marcelo Moreira de Mello Date: Tue, 14 Nov 2017 03:14:47 -0500 Subject: [PATCH 1/5] Creates a AmcresHub object to protect some private attributes on the logs --- homeassistant/components/amcrest.py | 41 +++++++++++++++++----- homeassistant/components/camera/amcrest.py | 19 ++++------ homeassistant/components/sensor/amcrest.py | 6 ++-- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/amcrest.py b/homeassistant/components/amcrest.py index 157b9574a0647..1ccc960ec4e25 100644 --- a/homeassistant/components/amcrest.py +++ b/homeassistant/components/amcrest.py @@ -126,22 +126,47 @@ def setup(hass, config): else: authentication = None + amcrest_hub = AmcrestHub(camera, + name, + authentication, + ffmpeg_arguments, + stream_source, + resolution) + discovery.load_platform( hass, 'camera', DOMAIN, { - 'device': camera, - CONF_AUTHENTICATION: authentication, - CONF_FFMPEG_ARGUMENTS: ffmpeg_arguments, - CONF_NAME: name, - CONF_RESOLUTION: resolution, - CONF_STREAM_SOURCE: stream_source, + 'amcrest_hub': amcrest_hub, }, config) if sensors: discovery.load_platform( hass, 'sensor', DOMAIN, { - 'device': camera, - CONF_NAME: name, + 'amcrest_hub': amcrest_hub, CONF_SENSORS: sensors, }, config) return True + + +class AmcrestHub(object): + """Representation of a base AmcrestHub discovery device.""" + + def __init__(self, camera, name, authentication, ffmpeg_arguments, + stream_source, resolution): + """Initialize the entity.""" + self.device = camera + self.name = name + self.authentication = authentication + self.ffmpeg_arguments = ffmpeg_arguments + self.stream_source = stream_source + self.resolution = resolution + + def as_dict(self): + """Convert entry to a dict to be used within JSON.""" + return { + 'device': self.device, + 'name': self.name, + 'ffmpeg_arguments': self.ffmpeg_arguments, + 'stream_source': self.stream_source, + 'resolution': self.resolution, + } diff --git a/homeassistant/components/camera/amcrest.py b/homeassistant/components/camera/amcrest.py index aba1bb08c9369..581c287cf184c 100644 --- a/homeassistant/components/camera/amcrest.py +++ b/homeassistant/components/camera/amcrest.py @@ -26,21 +26,16 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): if discovery_info is None: return - device = discovery_info['device'] - authentication = discovery_info['authentication'] - ffmpeg_arguments = discovery_info['ffmpeg_arguments'] - name = discovery_info['name'] - resolution = discovery_info['resolution'] - stream_source = discovery_info['stream_source'] + amcrest = discovery_info['amcrest_hub'] async_add_devices([ AmcrestCam(hass, - name, - device, - authentication, - ffmpeg_arguments, - stream_source, - resolution)], True) + amcrest.name, + amcrest.device, + amcrest.authentication, + amcrest.ffmpeg_arguments, + amcrest.stream_source, + amcrest.resolution)], True) return True diff --git a/homeassistant/components/sensor/amcrest.py b/homeassistant/components/sensor/amcrest.py index e7bf309c33a66..4848706b5feb9 100644 --- a/homeassistant/components/sensor/amcrest.py +++ b/homeassistant/components/sensor/amcrest.py @@ -25,13 +25,13 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): if discovery_info is None: return - device = discovery_info['device'] - name = discovery_info['name'] + amcrest = discovery_info['amcrest_hub'] sensors = discovery_info['sensors'] amcrest_sensors = [] for sensor_type in sensors: - amcrest_sensors.append(AmcrestSensor(name, device, sensor_type)) + amcrest_sensors.append( + AmcrestSensor(amcrest.name, amcrest.device, sensor_type)) async_add_devices(amcrest_sensors, True) return True From 1505316e94098e04302b9c1d2c4c936fdafad13f Mon Sep 17 00:00:00 2001 From: Marcelo Moreira de Mello Date: Tue, 14 Nov 2017 20:35:31 -0500 Subject: [PATCH 2/5] Uses hass.data to pass AmcrestHub to components --- homeassistant/components/amcrest.py | 7 +++++-- homeassistant/components/camera/amcrest.py | 5 +++-- homeassistant/components/sensor/amcrest.py | 5 +++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/amcrest.py b/homeassistant/components/amcrest.py index 1ccc960ec4e25..d0db6edc1de8b 100644 --- a/homeassistant/components/amcrest.py +++ b/homeassistant/components/amcrest.py @@ -89,6 +89,7 @@ def setup(hass, config): """Set up the Amcrest IP Camera component.""" from amcrest import AmcrestCamera + hass.data[DATA_AMCREST] = {} amcrest_cams = config[DOMAIN] for device in amcrest_cams: @@ -133,15 +134,17 @@ def setup(hass, config): stream_source, resolution) + hass.data[DATA_AMCREST][name] = amcrest_hub + discovery.load_platform( hass, 'camera', DOMAIN, { - 'amcrest_hub': amcrest_hub, + CONF_NAME: name, }, config) if sensors: discovery.load_platform( hass, 'sensor', DOMAIN, { - 'amcrest_hub': amcrest_hub, + CONF_NAME: name, CONF_SENSORS: sensors, }, config) diff --git a/homeassistant/components/camera/amcrest.py b/homeassistant/components/camera/amcrest.py index 581c287cf184c..18dfdff64e70c 100644 --- a/homeassistant/components/camera/amcrest.py +++ b/homeassistant/components/camera/amcrest.py @@ -8,7 +8,7 @@ import logging from homeassistant.components.amcrest import ( - STREAM_SOURCE_LIST, TIMEOUT) + DATA_AMCREST, STREAM_SOURCE_LIST, TIMEOUT) from homeassistant.components.camera import Camera from homeassistant.components.ffmpeg import DATA_FFMPEG from homeassistant.helpers.aiohttp_client import ( @@ -26,7 +26,8 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): if discovery_info is None: return - amcrest = discovery_info['amcrest_hub'] + device_name = discovery_info['name'] + amcrest = hass.data[DATA_AMCREST][device_name] async_add_devices([ AmcrestCam(hass, diff --git a/homeassistant/components/sensor/amcrest.py b/homeassistant/components/sensor/amcrest.py index 4848706b5feb9..43d752d9d77ec 100644 --- a/homeassistant/components/sensor/amcrest.py +++ b/homeassistant/components/sensor/amcrest.py @@ -8,7 +8,7 @@ from datetime import timedelta import logging -from homeassistant.components.amcrest import SENSORS +from homeassistant.components.amcrest import DATA_AMCREST, SENSORS from homeassistant.helpers.entity import Entity from homeassistant.const import STATE_UNKNOWN @@ -25,8 +25,9 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): if discovery_info is None: return - amcrest = discovery_info['amcrest_hub'] + device_name = discovery_info['name'] sensors = discovery_info['sensors'] + amcrest = hass.data[DATA_AMCREST][device_name] amcrest_sensors = [] for sensor_type in sensors: From 80334c3031d89fd58622d2c01e3a35abbe65f9ee Mon Sep 17 00:00:00 2001 From: Marcelo Moreira de Mello Date: Tue, 14 Nov 2017 20:54:59 -0500 Subject: [PATCH 3/5] Prefer constants --- homeassistant/components/camera/amcrest.py | 3 ++- homeassistant/components/sensor/amcrest.py | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/camera/amcrest.py b/homeassistant/components/camera/amcrest.py index 18dfdff64e70c..e89b13fecaaa4 100644 --- a/homeassistant/components/camera/amcrest.py +++ b/homeassistant/components/camera/amcrest.py @@ -11,6 +11,7 @@ DATA_AMCREST, STREAM_SOURCE_LIST, TIMEOUT) from homeassistant.components.camera import Camera from homeassistant.components.ffmpeg import DATA_FFMPEG +from homeassistant.const import CONF_NAME from homeassistant.helpers.aiohttp_client import ( async_get_clientsession, async_aiohttp_proxy_web, async_aiohttp_proxy_stream) @@ -26,7 +27,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): if discovery_info is None: return - device_name = discovery_info['name'] + device_name = discovery_info[CONF_NAME] amcrest = hass.data[DATA_AMCREST][device_name] async_add_devices([ diff --git a/homeassistant/components/sensor/amcrest.py b/homeassistant/components/sensor/amcrest.py index 43d752d9d77ec..99a4371f6a21a 100644 --- a/homeassistant/components/sensor/amcrest.py +++ b/homeassistant/components/sensor/amcrest.py @@ -10,7 +10,7 @@ from homeassistant.components.amcrest import DATA_AMCREST, SENSORS from homeassistant.helpers.entity import Entity -from homeassistant.const import STATE_UNKNOWN +from homeassistant.const import CONF_NAME, CONF_SENSORS, STATE_UNKNOWN DEPENDENCIES = ['amcrest'] @@ -25,8 +25,8 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): if discovery_info is None: return - device_name = discovery_info['name'] - sensors = discovery_info['sensors'] + device_name = discovery_info[CONF_NAME] + sensors = discovery_info[CONF_SENSORS] amcrest = hass.data[DATA_AMCREST][device_name] amcrest_sensors = [] From 09bca04c3ccf8b65c3652114c915dbb3e797f66f Mon Sep 17 00:00:00 2001 From: Marcelo Moreira de Mello Date: Wed, 22 Nov 2017 01:11:46 -0500 Subject: [PATCH 4/5] Removed serializer since it's using hass.data and simplified camera entity constructor --- homeassistant/components/amcrest.py | 10 --------- homeassistant/components/camera/amcrest.py | 24 ++++++++-------------- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/homeassistant/components/amcrest.py b/homeassistant/components/amcrest.py index d0db6edc1de8b..cac18b521546a 100644 --- a/homeassistant/components/amcrest.py +++ b/homeassistant/components/amcrest.py @@ -163,13 +163,3 @@ def __init__(self, camera, name, authentication, ffmpeg_arguments, self.ffmpeg_arguments = ffmpeg_arguments self.stream_source = stream_source self.resolution = resolution - - def as_dict(self): - """Convert entry to a dict to be used within JSON.""" - return { - 'device': self.device, - 'name': self.name, - 'ffmpeg_arguments': self.ffmpeg_arguments, - 'stream_source': self.stream_source, - 'resolution': self.resolution, - } diff --git a/homeassistant/components/camera/amcrest.py b/homeassistant/components/camera/amcrest.py index e89b13fecaaa4..3c63e56b3191f 100644 --- a/homeassistant/components/camera/amcrest.py +++ b/homeassistant/components/camera/amcrest.py @@ -30,14 +30,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): device_name = discovery_info[CONF_NAME] amcrest = hass.data[DATA_AMCREST][device_name] - async_add_devices([ - AmcrestCam(hass, - amcrest.name, - amcrest.device, - amcrest.authentication, - amcrest.ffmpeg_arguments, - amcrest.stream_source, - amcrest.resolution)], True) + async_add_devices([AmcrestCam(hass, amcrest)], True) return True @@ -45,18 +38,17 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): class AmcrestCam(Camera): """An implementation of an Amcrest IP camera.""" - def __init__(self, hass, name, camera, authentication, - ffmpeg_arguments, stream_source, resolution): + def __init__(self, hass, amcrest): """Initialize an Amcrest camera.""" super(AmcrestCam, self).__init__() - self._name = name - self._camera = camera + self._name = amcrest.name + self._camera = amcrest.device self._base_url = self._camera.get_base_url() self._ffmpeg = hass.data[DATA_FFMPEG] - self._ffmpeg_arguments = ffmpeg_arguments - self._stream_source = stream_source - self._resolution = resolution - self._token = self._auth = authentication + self._ffmpeg_arguments = amcrest.ffmpeg_arguments + self._stream_source = amcrest.stream_source + self._resolution = amcrest.resolution + self._token = self._auth = amcrest.authentication def camera_image(self): """Return a still image response from the camera.""" From dd2818c4ef65a766f9ad7e6644fde22b7b6c4d95 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Wed, 22 Nov 2017 11:57:52 +0100 Subject: [PATCH 5/5] small cleanup --- homeassistant/components/amcrest.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/amcrest.py b/homeassistant/components/amcrest.py index cac18b521546a..9205846462ffc 100644 --- a/homeassistant/components/amcrest.py +++ b/homeassistant/components/amcrest.py @@ -127,14 +127,9 @@ def setup(hass, config): else: authentication = None - amcrest_hub = AmcrestHub(camera, - name, - authentication, - ffmpeg_arguments, - stream_source, - resolution) - - hass.data[DATA_AMCREST][name] = amcrest_hub + hass.data[DATA_AMCREST][name] = AmcrestDevice( + camera, name, authentication, ffmpeg_arguments, stream_source, + resolution) discovery.load_platform( hass, 'camera', DOMAIN, { @@ -151,8 +146,8 @@ def setup(hass, config): return True -class AmcrestHub(object): - """Representation of a base AmcrestHub discovery device.""" +class AmcrestDevice(object): + """Representation of a base Amcrest discovery device.""" def __init__(self, camera, name, authentication, ffmpeg_arguments, stream_source, resolution):