From 1a1f95ea29ef17ce9e52906f5eafb678f3836cab Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Sun, 25 Sep 2016 11:56:18 +0200 Subject: [PATCH] Added support for MySensors cover device --- homeassistant/components/cover/mysensors.py | 99 +++++++++++++++++++++ homeassistant/components/mysensors.py | 2 +- 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/cover/mysensors.py diff --git a/homeassistant/components/cover/mysensors.py b/homeassistant/components/cover/mysensors.py new file mode 100644 index 00000000000..5442a5bd06c --- /dev/null +++ b/homeassistant/components/cover/mysensors.py @@ -0,0 +1,99 @@ +""" +Support for MySensors covers. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/cover.mysensors/ +""" +import logging + +from homeassistant.components import mysensors +from homeassistant.components.cover import CoverDevice, ATTR_POSITION + +_LOGGER = logging.getLogger(__name__) +DEPENDENCIES = [] + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup the mysensors platform for covers.""" + if discovery_info is None: + return + for gateway in mysensors.GATEWAYS.values(): + if float(gateway.protocol_version) < 1.5: + continue + pres = gateway.const.Presentation + set_req = gateway.const.SetReq + map_sv_types = { + pres.S_COVER: [set_req.V_UP], + } + devices = {} + gateway.platform_callbacks.append(mysensors.pf_callback_factory( + map_sv_types, devices, add_devices, MySensorsCover)) + + +class MySensorsCover(mysensors.MySensorsDeviceEntity, CoverDevice): + """Representation of the value of a MySensors Cover child node.""" + + @property + def assumed_state(self): + """Return True if unable to access real state of entity.""" + return self.gateway.optimistic + + def update(self): + """Update the controller with the latest value from a sensor.""" + set_req = self.gateway.const.SetReq + node = self.gateway.sensors[self.node_id] + child = node.children[self.child_id] + for value_type, value in child.values.items(): + _LOGGER.debug( + '%s: value_type %s, value = %s', self._name, value_type, value) + self._values[value_type] = value + + @property + def is_closed(self): + """Return True if cover is closed.""" + return self._values.get(set_req.V_PERCENTAGE) == 0 + + @property + def current_cover_position(self): + """Return current position of cover. + + None is unknown, 0 is closed, 100 is fully open. + """ + return self._values.get(set_req.V_PERCENTAGE) + + def open_cover(self, **kwargs): + """Move the cover up.""" + set_req = self.gateway.const.SetReq + self.gateway.set_child_value( + self.node_id, self.child_id, set_req.V_UP, 1) + if self.gateway.optimistic: + # Optimistically assume that cover has changed state. + self._values[set_req.V_PERCENTAGE] = 100 + self.update_ha_state() + + def close_cover(self, **kwargs): + """Move the cover down.""" + set_req = self.gateway.const.SetReq + self.gateway.set_child_value( + self.node_id, self.child_id, set_req.V_DOWN, 1) + if self.gateway.optimistic: + # Optimistically assume that cover has changed state. + self._values[set_req.V_PERCENTAGE] = 0 + self.update_ha_state() + + def set_cover_position(self, **kwargs): + """Move the cover to a specific position.""" + position = kwargs.get(ATTR_POSITION) + set_req = self.gateway.const.SetReq + self.gateway.set_child_value( + self.node_id, self.child_id, set_req.V_PERCENTAGE, position) + if self.gateway.optimistic: + # Optimistically assume that cover has changed state. + self._values[set_req.V_PERCENTAGE] = position + self.update_ha_state() + + def stop_cover(self, **kwargs): + """Stop the device.""" + set_req = self.gateway.const.SetReq + self.gateway.set_child_value( + self.node_id, self.child_id, set_req.V_STOP, 1) diff --git a/homeassistant/components/mysensors.py b/homeassistant/components/mysensors.py index 84d1150a6d8..6db6b2e99fa 100644 --- a/homeassistant/components/mysensors.py +++ b/homeassistant/components/mysensors.py @@ -158,7 +158,7 @@ def setup(hass, config): # pylint: disable=too-many-locals 'No devices could be setup as gateways, check your configuration') return False - for component in 'sensor', 'switch', 'light', 'binary_sensor', 'climate': + for component in 'sensor', 'switch', 'light', 'binary_sensor', 'climate', 'cover': discovery.load_platform(hass, component, DOMAIN, {}, config) return True