From 5a09628c5cad83fc40d079ad130f2144b2f02a73 Mon Sep 17 00:00:00 2001 From: nhorvath Date: Fri, 14 Dec 2018 18:12:34 -0500 Subject: [PATCH 1/2] add camera selection config to skybell camera --- homeassistant/components/camera/skybell.py | 44 +++++++++++++++++++--- homeassistant/components/skybell.py | 2 +- requirements_all.txt | 2 +- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/camera/skybell.py b/homeassistant/components/camera/skybell.py index 9a7d7a069447bd..61aa88c81ebfbc 100644 --- a/homeassistant/components/camera/skybell.py +++ b/homeassistant/components/camera/skybell.py @@ -8,6 +8,11 @@ import logging import requests +import voluptuous as vol + +from homeassistant.components.sensor import PLATFORM_SCHEMA +from homeassistant.const import CONF_MONITORED_CONDITIONS +import homeassistant.helpers.config_validation as cv from homeassistant.components.camera import Camera from homeassistant.components.skybell import ( @@ -19,14 +24,32 @@ SCAN_INTERVAL = timedelta(seconds=90) +IMAGE_AVATAR = 'avatar' +IMAGE_ACTIVITY = 'activity' + +CONF_ACTIVITY_NAME = 'activity_name' +CONF_AVATAR_NAME = 'avatar_name' + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Optional(CONF_MONITORED_CONDITIONS, default=[IMAGE_AVATAR]): + vol.All(cv.ensure_list, [vol.In([IMAGE_AVATAR, IMAGE_ACTIVITY])]), + vol.Optional(CONF_ACTIVITY_NAME, default=""): cv.string, + vol.Optional(CONF_AVATAR_NAME, default=""): cv.string, +}) + def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the platform for a Skybell device.""" + cond = config[CONF_MONITORED_CONDITIONS] + names = {} + names[IMAGE_ACTIVITY] = config[CONF_ACTIVITY_NAME] + names[IMAGE_AVATAR] = config[CONF_AVATAR_NAME] skybell = hass.data.get(SKYBELL_DOMAIN) sensors = [] for device in skybell.get_devices(): - sensors.append(SkybellCamera(device)) + for camera_type in cond: + sensors.append(SkybellCamera(device, type, names[camera_type])) add_entities(sensors, True) @@ -34,11 +57,15 @@ def setup_platform(hass, config, add_entities, discovery_info=None): class SkybellCamera(SkybellDevice, Camera): """A camera implementation for Skybell devices.""" - def __init__(self, device): + def __init__(self, device, camera_type, name=""): """Initialize a camera for a Skybell device.""" + self._type = camera_type SkybellDevice.__init__(self, device) Camera.__init__(self) - self._name = self._device.name + if name != "": + self._name = self._device.name + " " + name + else: + self._name = self._device.name self._url = None self._response = None @@ -47,12 +74,19 @@ def name(self): """Return the name of the sensor.""" return self._name + @property + def image_url(self): + """Get the camera image url based on type.""" + if self._type == IMAGE_ACTIVITY: + return self._device.activity_image + return self._device.image + def camera_image(self): """Get the latest camera image.""" super().update() - if self._url != self._device.image: - self._url = self._device.image + if self._url != self.image_url: + self._url = self.image_url try: self._response = requests.get( diff --git a/homeassistant/components/skybell.py b/homeassistant/components/skybell.py index b3c3b63bd84a7e..ee384fd7094972 100644 --- a/homeassistant/components/skybell.py +++ b/homeassistant/components/skybell.py @@ -14,7 +14,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity -REQUIREMENTS = ['skybellpy==0.2.0'] +REQUIREMENTS = ['skybellpy==0.3.0'] _LOGGER = logging.getLogger(__name__) diff --git a/requirements_all.txt b/requirements_all.txt index 41a8025fee104c..f451f32df597c4 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1440,7 +1440,7 @@ simplisafe-python==3.1.14 sisyphus-control==2.1 # homeassistant.components.skybell -skybellpy==0.2.0 +skybellpy==0.3.0 # homeassistant.components.notify.slack slacker==0.12.0 From fa4e1099371b7d7ef856bc7789947977b07a49db Mon Sep 17 00:00:00 2001 From: nhorvath Date: Mon, 17 Dec 2018 12:49:32 -0500 Subject: [PATCH 2/2] code review changes. --- homeassistant/components/camera/skybell.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/camera/skybell.py b/homeassistant/components/camera/skybell.py index 61aa88c81ebfbc..3ad95e40d62f9b 100644 --- a/homeassistant/components/camera/skybell.py +++ b/homeassistant/components/camera/skybell.py @@ -10,7 +10,7 @@ import requests import voluptuous as vol -from homeassistant.components.sensor import PLATFORM_SCHEMA +from homeassistant.components.camera import PLATFORM_SCHEMA from homeassistant.const import CONF_MONITORED_CONDITIONS import homeassistant.helpers.config_validation as cv @@ -33,8 +33,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Optional(CONF_MONITORED_CONDITIONS, default=[IMAGE_AVATAR]): vol.All(cv.ensure_list, [vol.In([IMAGE_AVATAR, IMAGE_ACTIVITY])]), - vol.Optional(CONF_ACTIVITY_NAME, default=""): cv.string, - vol.Optional(CONF_AVATAR_NAME, default=""): cv.string, + vol.Optional(CONF_ACTIVITY_NAME): cv.string, + vol.Optional(CONF_AVATAR_NAME): cv.string, }) @@ -42,14 +42,15 @@ def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the platform for a Skybell device.""" cond = config[CONF_MONITORED_CONDITIONS] names = {} - names[IMAGE_ACTIVITY] = config[CONF_ACTIVITY_NAME] - names[IMAGE_AVATAR] = config[CONF_AVATAR_NAME] + names[IMAGE_ACTIVITY] = config.get(CONF_ACTIVITY_NAME) + names[IMAGE_AVATAR] = config.get(CONF_AVATAR_NAME) skybell = hass.data.get(SKYBELL_DOMAIN) sensors = [] for device in skybell.get_devices(): for camera_type in cond: - sensors.append(SkybellCamera(device, type, names[camera_type])) + sensors.append(SkybellCamera(device, camera_type, + names.get(camera_type))) add_entities(sensors, True) @@ -57,13 +58,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None): class SkybellCamera(SkybellDevice, Camera): """A camera implementation for Skybell devices.""" - def __init__(self, device, camera_type, name=""): + def __init__(self, device, camera_type, name=None): """Initialize a camera for a Skybell device.""" self._type = camera_type SkybellDevice.__init__(self, device) Camera.__init__(self) - if name != "": - self._name = self._device.name + " " + name + if name is not None: + self._name = "{} {}".format(self._device.name, name) else: self._name = self._device.name self._url = None