From 3dd49b2b95807453ee9a2f1baa81a061199cda80 Mon Sep 17 00:00:00 2001 From: Marcelo Moreira de Mello Date: Thu, 23 Nov 2017 19:38:53 -0500 Subject: [PATCH] Protect sensitive information for Amcrest cameras (#10569) * Creates a AmcresHub object to protect some private attributes on the logs * Uses hass.data to pass AmcrestHub to components * Prefer constants * Removed serializer since it's using hass.data and simplified camera entity constructor * small cleanup --- homeassistant/components/amcrest.py | 25 ++++++++++++---- homeassistant/components/camera/amcrest.py | 35 ++++++++-------------- homeassistant/components/sensor/amcrest.py | 13 ++++---- 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/homeassistant/components/amcrest.py b/homeassistant/components/amcrest.py index 157b9574a06..9205846462f 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: @@ -126,22 +127,34 @@ def setup(hass, config): else: authentication = None + hass.data[DATA_AMCREST][name] = AmcrestDevice( + 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, }, config) if sensors: discovery.load_platform( hass, 'sensor', DOMAIN, { - 'device': camera, CONF_NAME: name, CONF_SENSORS: sensors, }, config) return True + + +class AmcrestDevice(object): + """Representation of a base Amcrest 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 diff --git a/homeassistant/components/camera/amcrest.py b/homeassistant/components/camera/amcrest.py index aba1bb08c93..3c63e56b319 100644 --- a/homeassistant/components/camera/amcrest.py +++ b/homeassistant/components/camera/amcrest.py @@ -8,9 +8,10 @@ import asyncio 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.const import CONF_NAME from homeassistant.helpers.aiohttp_client import ( async_get_clientsession, async_aiohttp_proxy_web, async_aiohttp_proxy_stream) @@ -26,21 +27,10 @@ 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'] + device_name = discovery_info[CONF_NAME] + amcrest = hass.data[DATA_AMCREST][device_name] - async_add_devices([ - AmcrestCam(hass, - name, - device, - authentication, - ffmpeg_arguments, - stream_source, - resolution)], True) + async_add_devices([AmcrestCam(hass, amcrest)], True) return True @@ -48,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.""" diff --git a/homeassistant/components/sensor/amcrest.py b/homeassistant/components/sensor/amcrest.py index e7bf309c33a..99a4371f6a2 100644 --- a/homeassistant/components/sensor/amcrest.py +++ b/homeassistant/components/sensor/amcrest.py @@ -8,9 +8,9 @@ import asyncio 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 +from homeassistant.const import CONF_NAME, CONF_SENSORS, STATE_UNKNOWN DEPENDENCIES = ['amcrest'] @@ -25,13 +25,14 @@ 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'] - sensors = discovery_info['sensors'] + device_name = discovery_info[CONF_NAME] + sensors = discovery_info[CONF_SENSORS] + amcrest = hass.data[DATA_AMCREST][device_name] 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