From 42e691c1945e18183cd872127a6294aefed4e3fb Mon Sep 17 00:00:00 2001 From: Mattias Welponer Date: Tue, 26 Feb 2019 23:48:19 +0100 Subject: [PATCH] Add HomematicIP HmIP-OC8 module (#21401) * Add support for HmIP-OC8 module * Fix line lenght --- .../components/homematicip_cloud/switch.py | 50 +++++++++++++++---- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/homematicip_cloud/switch.py b/homeassistant/components/homematicip_cloud/switch.py index 057673d8f9b..f129febb5e7 100644 --- a/homeassistant/components/homematicip_cloud/switch.py +++ b/homeassistant/components/homematicip_cloud/switch.py @@ -22,11 +22,12 @@ async def async_setup_platform( async def async_setup_entry(hass, config_entry, async_add_entities): """Set up the HomematicIP switch from a config entry.""" - from homematicip.device import ( - PlugableSwitch, - PlugableSwitchMeasuring, - BrandSwitchMeasuring, - FullFlushSwitchMeasuring, + from homematicip.aio.device import ( + AsyncPlugableSwitch, + AsyncPlugableSwitchMeasuring, + AsyncBrandSwitchMeasuring, + AsyncFullFlushSwitchMeasuring, + AsyncOpenCollector8Module, ) from homematicip.group import SwitchingGroup @@ -34,16 +35,19 @@ async def async_setup_entry(hass, config_entry, async_add_entities): home = hass.data[HMIPC_DOMAIN][config_entry.data[HMIPC_HAPID]].home devices = [] for device in home.devices: - if isinstance(device, BrandSwitchMeasuring): + if isinstance(device, AsyncBrandSwitchMeasuring): # BrandSwitchMeasuring inherits PlugableSwitchMeasuring # This device is implemented in the light platform and will # not be added in the switch platform pass - elif isinstance(device, (PlugableSwitchMeasuring, - FullFlushSwitchMeasuring)): + elif isinstance(device, (AsyncPlugableSwitchMeasuring, + AsyncFullFlushSwitchMeasuring)): devices.append(HomematicipSwitchMeasuring(home, device)) - elif isinstance(device, PlugableSwitch): + elif isinstance(device, AsyncPlugableSwitch): devices.append(HomematicipSwitch(home, device)) + elif isinstance(device, AsyncOpenCollector8Module): + for channel in range(1, 9): + devices.append(HomematicipMultiSwitch(home, device, channel)) for group in home.groups: if isinstance(group, SwitchingGroup): @@ -111,3 +115,31 @@ class HomematicipSwitchMeasuring(HomematicipSwitch): if self._device.energyCounter is None: return 0 return round(self._device.energyCounter) + + +class HomematicipMultiSwitch(HomematicipGenericDevice, SwitchDevice): + """Representation of a HomematicIP Cloud multi switch device.""" + + def __init__(self, home, device, channel): + """Initialize the multi switch device.""" + self.channel = channel + super().__init__(home, device, 'Channel{}'.format(channel)) + + @property + def unique_id(self): + """Return a unique ID.""" + return "{}_{}_{}".format(self.__class__.__name__, + self.post, self._device.id) + + @property + def is_on(self): + """Return true if device is on.""" + return self._device.functionalChannels[self.channel].on + + async def async_turn_on(self, **kwargs): + """Turn the device on.""" + await self._device.turn_on(self.channel) + + async def async_turn_off(self, **kwargs): + """Turn the device off.""" + await self._device.turn_off(self.channel)