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
This commit is contained in:
Marcelo Moreira de Mello 2017-11-23 19:38:53 -05:00 committed by Paulus Schoutsen
parent 3ef9c99003
commit 3dd49b2b95
3 changed files with 38 additions and 35 deletions

View file

@ -89,6 +89,7 @@ def setup(hass, config):
"""Set up the Amcrest IP Camera component.""" """Set up the Amcrest IP Camera component."""
from amcrest import AmcrestCamera from amcrest import AmcrestCamera
hass.data[DATA_AMCREST] = {}
amcrest_cams = config[DOMAIN] amcrest_cams = config[DOMAIN]
for device in amcrest_cams: for device in amcrest_cams:
@ -126,22 +127,34 @@ def setup(hass, config):
else: else:
authentication = None authentication = None
hass.data[DATA_AMCREST][name] = AmcrestDevice(
camera, name, authentication, ffmpeg_arguments, stream_source,
resolution)
discovery.load_platform( discovery.load_platform(
hass, 'camera', DOMAIN, { hass, 'camera', DOMAIN, {
'device': camera,
CONF_AUTHENTICATION: authentication,
CONF_FFMPEG_ARGUMENTS: ffmpeg_arguments,
CONF_NAME: name, CONF_NAME: name,
CONF_RESOLUTION: resolution,
CONF_STREAM_SOURCE: stream_source,
}, config) }, config)
if sensors: if sensors:
discovery.load_platform( discovery.load_platform(
hass, 'sensor', DOMAIN, { hass, 'sensor', DOMAIN, {
'device': camera,
CONF_NAME: name, CONF_NAME: name,
CONF_SENSORS: sensors, CONF_SENSORS: sensors,
}, config) }, config)
return True 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

View file

@ -8,9 +8,10 @@ import asyncio
import logging import logging
from homeassistant.components.amcrest import ( from homeassistant.components.amcrest import (
STREAM_SOURCE_LIST, TIMEOUT) DATA_AMCREST, STREAM_SOURCE_LIST, TIMEOUT)
from homeassistant.components.camera import Camera from homeassistant.components.camera import Camera
from homeassistant.components.ffmpeg import DATA_FFMPEG from homeassistant.components.ffmpeg import DATA_FFMPEG
from homeassistant.const import CONF_NAME
from homeassistant.helpers.aiohttp_client import ( from homeassistant.helpers.aiohttp_client import (
async_get_clientsession, async_aiohttp_proxy_web, async_get_clientsession, async_aiohttp_proxy_web,
async_aiohttp_proxy_stream) 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: if discovery_info is None:
return return
device = discovery_info['device'] device_name = discovery_info[CONF_NAME]
authentication = discovery_info['authentication'] amcrest = hass.data[DATA_AMCREST][device_name]
ffmpeg_arguments = discovery_info['ffmpeg_arguments']
name = discovery_info['name']
resolution = discovery_info['resolution']
stream_source = discovery_info['stream_source']
async_add_devices([ async_add_devices([AmcrestCam(hass, amcrest)], True)
AmcrestCam(hass,
name,
device,
authentication,
ffmpeg_arguments,
stream_source,
resolution)], True)
return True return True
@ -48,18 +38,17 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
class AmcrestCam(Camera): class AmcrestCam(Camera):
"""An implementation of an Amcrest IP camera.""" """An implementation of an Amcrest IP camera."""
def __init__(self, hass, name, camera, authentication, def __init__(self, hass, amcrest):
ffmpeg_arguments, stream_source, resolution):
"""Initialize an Amcrest camera.""" """Initialize an Amcrest camera."""
super(AmcrestCam, self).__init__() super(AmcrestCam, self).__init__()
self._name = name self._name = amcrest.name
self._camera = camera self._camera = amcrest.device
self._base_url = self._camera.get_base_url() self._base_url = self._camera.get_base_url()
self._ffmpeg = hass.data[DATA_FFMPEG] self._ffmpeg = hass.data[DATA_FFMPEG]
self._ffmpeg_arguments = ffmpeg_arguments self._ffmpeg_arguments = amcrest.ffmpeg_arguments
self._stream_source = stream_source self._stream_source = amcrest.stream_source
self._resolution = resolution self._resolution = amcrest.resolution
self._token = self._auth = authentication self._token = self._auth = amcrest.authentication
def camera_image(self): def camera_image(self):
"""Return a still image response from the camera.""" """Return a still image response from the camera."""

View file

@ -8,9 +8,9 @@ import asyncio
from datetime import timedelta from datetime import timedelta
import logging import logging
from homeassistant.components.amcrest import SENSORS from homeassistant.components.amcrest import DATA_AMCREST, SENSORS
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.const import STATE_UNKNOWN from homeassistant.const import CONF_NAME, CONF_SENSORS, STATE_UNKNOWN
DEPENDENCIES = ['amcrest'] DEPENDENCIES = ['amcrest']
@ -25,13 +25,14 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
if discovery_info is None: if discovery_info is None:
return return
device = discovery_info['device'] device_name = discovery_info[CONF_NAME]
name = discovery_info['name'] sensors = discovery_info[CONF_SENSORS]
sensors = discovery_info['sensors'] amcrest = hass.data[DATA_AMCREST][device_name]
amcrest_sensors = [] amcrest_sensors = []
for sensor_type in 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) async_add_devices(amcrest_sensors, True)
return True return True