From d996a4a9a9f3eab0f9ecaaf6e5e8040f9d0ed61f Mon Sep 17 00:00:00 2001 From: Rocik Date: Mon, 24 Feb 2020 15:34:53 +0100 Subject: [PATCH] Add Supla gate (#31643) * Add support for Supla gate with sensor * Fix Supla switch module description and state access * Add docs to methods of Supla gate * Add missing comma * Remove unused import * Sort imports of Supla cover * Add returning availability for every Supla device * Use direct access to dict * Remove deprecated property "hidden" * Remove unused constant * Revert using get function on dict --- homeassistant/components/supla/__init__.py | 20 ++++++++ homeassistant/components/supla/cover.py | 55 ++++++++++++++++++++-- homeassistant/components/supla/switch.py | 2 +- 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/supla/__init__.py b/homeassistant/components/supla/__init__.py index fd60254cd0a..6c9bfb8d16e 100644 --- a/homeassistant/components/supla/__init__.py +++ b/homeassistant/components/supla/__init__.py @@ -18,8 +18,10 @@ CONF_SERVERS = "servers" SUPLA_FUNCTION_HA_CMP_MAP = { "CONTROLLINGTHEROLLERSHUTTER": "cover", + "CONTROLLINGTHEGATE": "cover", "LIGHTSWITCH": "switch", } +SUPLA_FUNCTION_NONE = "NONE" SUPLA_CHANNELS = "supla_channels" SUPLA_SERVERS = "supla_servers" @@ -86,6 +88,14 @@ def discover_devices(hass, hass_config): for channel in server.get_channels(include=["iodevice"]): channel_function = channel["function"]["name"] + if channel_function == SUPLA_FUNCTION_NONE: + _LOGGER.debug( + "Ignored function: %s, channel id: %s", + channel_function, + channel["id"], + ) + continue + component_name = SUPLA_FUNCTION_HA_CMP_MAP.get(channel_function) if component_name is None: @@ -130,6 +140,16 @@ class SuplaChannel(Entity): """Return the name of the device.""" return self.channel_data["caption"] + @property + def available(self) -> bool: + """Return True if entity is available.""" + if self.channel_data is None: + return False + state = self.channel_data.get("state") + if state is None: + return False + return state.get("connected") + def action(self, action, **add_pars): """ Run server action. diff --git a/homeassistant/components/supla/cover.py b/homeassistant/components/supla/cover.py index 3182aa8c136..659b78cc41a 100644 --- a/homeassistant/components/supla/cover.py +++ b/homeassistant/components/supla/cover.py @@ -1,12 +1,19 @@ -"""Support for Supla cover - curtains, rollershutters etc.""" +"""Support for Supla cover - curtains, rollershutters, entry gate etc.""" import logging from pprint import pformat -from homeassistant.components.cover import ATTR_POSITION, CoverDevice +from homeassistant.components.cover import ( + ATTR_POSITION, + DEVICE_CLASS_GARAGE, + CoverDevice, +) from homeassistant.components.supla import SuplaChannel _LOGGER = logging.getLogger(__name__) +SUPLA_SHUTTER = "CONTROLLINGTHEROLLERSHUTTER" +SUPLA_GATE = "CONTROLLINGTHEGATE" + def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Supla covers.""" @@ -15,7 +22,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None): _LOGGER.debug("Discovery: %s", pformat(discovery_info)) - add_entities([SuplaCover(device) for device in discovery_info]) + entities = [] + for device in discovery_info: + device_name = device["function"]["name"] + if device_name == SUPLA_SHUTTER: + entities.append(SuplaCover(device)) + elif device_name == SUPLA_GATE: + entities.append(SuplaGateDoor(device)) + add_entities(entities) class SuplaCover(SuplaChannel, CoverDevice): @@ -51,3 +65,38 @@ class SuplaCover(SuplaChannel, CoverDevice): def stop_cover(self, **kwargs): """Stop the cover.""" self.action("STOP") + + +class SuplaGateDoor(SuplaChannel, CoverDevice): + """Representation of a Supla gate door.""" + + @property + def is_closed(self): + """Return if the gate is closed or not.""" + state = self.channel_data.get("state") + if state and "hi" in state: + return state.get("hi") + return None + + def open_cover(self, **kwargs) -> None: + """Open the gate.""" + if self.is_closed: + self.action("OPEN_CLOSE") + + def close_cover(self, **kwargs) -> None: + """Close the gate.""" + if not self.is_closed: + self.action("OPEN_CLOSE") + + def stop_cover(self, **kwargs) -> None: + """Stop the gate.""" + self.action("OPEN_CLOSE") + + def toggle(self, **kwargs) -> None: + """Toggle the gate.""" + self.action("OPEN_CLOSE") + + @property + def device_class(self): + """Return the class of this device, from component DEVICE_CLASSES.""" + return DEVICE_CLASS_GARAGE diff --git a/homeassistant/components/supla/switch.py b/homeassistant/components/supla/switch.py index 725771e21e8..556c1b69a53 100644 --- a/homeassistant/components/supla/switch.py +++ b/homeassistant/components/supla/switch.py @@ -1,4 +1,4 @@ -"""Support for Supla cover - curtains, rollershutters etc.""" +"""Support for Supla switch.""" import logging from pprint import pformat