From 4e107a2bcf0ed202f4fcb2d6e020564cd2a0d52a Mon Sep 17 00:00:00 2001 From: tetienne Date: Thu, 28 Nov 2019 10:42:17 +0100 Subject: [PATCH] Add support for Somfy Camera Shutter (#29057) --- homeassistant/components/somfy/__init__.py | 2 +- homeassistant/components/somfy/cover.py | 16 +------ homeassistant/components/somfy/manifest.json | 2 +- homeassistant/components/somfy/switch.py | 49 ++++++++++++++++++++ requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 6 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 homeassistant/components/somfy/switch.py diff --git a/homeassistant/components/somfy/__init__.py b/homeassistant/components/somfy/__init__.py index b767ea83431..184e32f1e6d 100644 --- a/homeassistant/components/somfy/__init__.py +++ b/homeassistant/components/somfy/__init__.py @@ -48,7 +48,7 @@ CONFIG_SCHEMA = vol.Schema( extra=vol.ALLOW_EXTRA, ) -SOMFY_COMPONENTS = ["cover"] +SOMFY_COMPONENTS = ["cover", "switch"] async def async_setup(hass, config): diff --git a/homeassistant/components/somfy/cover.py b/homeassistant/components/somfy/cover.py index d54e7c99001..3a8557ea484 100644 --- a/homeassistant/components/somfy/cover.py +++ b/homeassistant/components/somfy/cover.py @@ -1,9 +1,4 @@ -""" -Support for Somfy Covers. - -For more details about this platform, please refer to the documentation at -https://home-assistant.io/components/cover.somfy/ -""" +"""Support for Somfy Covers.""" from pymfy.api.devices.category import Category from pymfy.api.devices.blind import Blind @@ -37,15 +32,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities): async_add_entities(await hass.async_add_executor_job(get_covers), True) -async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): - """Old way of setting up platform. - - Can only be called when a user accidentally mentions the platform in their - config. But even in that case it would have been ignored. - """ - pass - - class SomfyCover(SomfyEntity, CoverDevice): """Representation of a Somfy cover device.""" diff --git a/homeassistant/components/somfy/manifest.json b/homeassistant/components/somfy/manifest.json index f5a17275bcb..82e62e7dd08 100644 --- a/homeassistant/components/somfy/manifest.json +++ b/homeassistant/components/somfy/manifest.json @@ -5,5 +5,5 @@ "documentation": "https://www.home-assistant.io/integrations/somfy", "dependencies": ["http"], "codeowners": ["@tetienne"], - "requirements": ["pymfy==0.6.1"] + "requirements": ["pymfy==0.7.1"] } diff --git a/homeassistant/components/somfy/switch.py b/homeassistant/components/somfy/switch.py new file mode 100644 index 00000000000..d3294954851 --- /dev/null +++ b/homeassistant/components/somfy/switch.py @@ -0,0 +1,49 @@ +"""Support for Somfy Camera Shutter.""" +from pymfy.api.devices.camera_protect import CameraProtect +from pymfy.api.devices.category import Category + +from homeassistant.components.somfy import DOMAIN, SomfyEntity, DEVICES, API +from homeassistant.helpers.entity import ToggleEntity + + +async def async_setup_entry(hass, config_entry, async_add_entities): + """Set up the Somfy switch platform.""" + + def get_shutters(): + """Retrieve switches.""" + devices = hass.data[DOMAIN][DEVICES] + + return [ + SomfyCameraShutter(device, hass.data[DOMAIN][API]) + for device in devices + if Category.CAMERA.value in device.categories + ] + + async_add_entities(await hass.async_add_executor_job(get_shutters), True) + + +class SomfyCameraShutter(SomfyEntity, ToggleEntity): + """Representation of a Somfy Camera Shutter device.""" + + def __init__(self, device, api): + """Initialize the Somfy device.""" + super().__init__(device, api) + self.shutter = CameraProtect(self.device, self.api) + + async def async_update(self): + """Update the device with the latest data.""" + await super().async_update() + self.shutter = CameraProtect(self.device, self.api) + + def turn_on(self, **kwargs) -> None: + """Turn the entity on.""" + self.shutter.open_shutter() + + def turn_off(self, **kwargs): + """Turn the entity off.""" + self.shutter.close_shutter() + + @property + def is_on(self) -> bool: + """Return True if entity is on.""" + return self.shutter.get_shutter_position() == "opened" diff --git a/requirements_all.txt b/requirements_all.txt index 33693732c23..7d0c08e6a28 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1335,7 +1335,7 @@ pymailgunner==1.4 pymediaroom==0.6.4 # homeassistant.components.somfy -pymfy==0.6.1 +pymfy==0.7.1 # homeassistant.components.xiaomi_tv pymitv==1.4.3 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 0f22fe8c232..d8d13ab8908 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -449,7 +449,7 @@ pylitejet==0.1 pymailgunner==1.4 # homeassistant.components.somfy -pymfy==0.6.1 +pymfy==0.7.1 # homeassistant.components.mochad pymochad==0.2.0