From 149eddaf4615e131e7c40d69ce89757b86b7b057 Mon Sep 17 00:00:00 2001 From: pbalogh77 Date: Mon, 3 Dec 2018 14:57:55 +0100 Subject: [PATCH] Initial scene support for Fibaro hubs (#18779) * Initial scene support Added initial support for fibaro scenes * removed comments * cleanup based on code review * Removed unused functions * grrr, my mistake. My local pylint and flake8 are playing tricks with me * Update homeassistant/components/scene/fibaro.py * fixes based on code review ABC ordered the list of platforms changed setup platform to async removed overloaded name property as the FibaroDevice parent class already provides this Changed to new style string formatting * Update homeassistant/components/scene/fibaro.py Co-Authored-By: pbalogh77 --- homeassistant/components/fibaro.py | 21 +++++++++++++- homeassistant/components/scene/fibaro.py | 35 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/scene/fibaro.py diff --git a/homeassistant/components/fibaro.py b/homeassistant/components/fibaro.py index 51d7dd2ef7e..55f6f528622 100644 --- a/homeassistant/components/fibaro.py +++ b/homeassistant/components/fibaro.py @@ -27,7 +27,8 @@ ATTR_CURRENT_POWER_W = "current_power_w" ATTR_CURRENT_ENERGY_KWH = "current_energy_kwh" CONF_PLUGINS = "plugins" -FIBARO_COMPONENTS = ['binary_sensor', 'cover', 'light', 'sensor', 'switch'] +FIBARO_COMPONENTS = ['binary_sensor', 'cover', 'light', + 'scene', 'sensor', 'switch'] FIBARO_TYPEMAP = { 'com.fibaro.multilevelSensor': "sensor", @@ -72,6 +73,7 @@ class FibaroController(): """Initialize the Fibaro controller.""" from fiblary3.client.v4.client import Client as FibaroClient self._client = FibaroClient(url, username, password) + self._scene_map = None def connect(self): """Start the communication with the Fibaro controller.""" @@ -88,6 +90,7 @@ class FibaroController(): self._room_map = {room.id: room for room in self._client.rooms.list()} self._read_devices() + self._read_scenes() return True def enable_state_handler(self): @@ -167,6 +170,22 @@ class FibaroController(): device_type = 'light' return device_type + def _read_scenes(self): + scenes = self._client.scenes.list() + self._scene_map = {} + for device in scenes: + if not device.visible: + continue + if device.roomID == 0: + room_name = 'Unknown' + else: + room_name = self._room_map[device.roomID].name + device.friendly_name = '{} {}'.format(room_name, device.name) + device.ha_id = '{}_{}_{}'.format( + slugify(room_name), slugify(device.name), device.id) + self._scene_map[device.id] = device + self.fibaro_devices['scene'].append(device) + def _read_devices(self): """Read and process the device list.""" devices = self._client.devices.list() diff --git a/homeassistant/components/scene/fibaro.py b/homeassistant/components/scene/fibaro.py new file mode 100644 index 00000000000..7a36900f884 --- /dev/null +++ b/homeassistant/components/scene/fibaro.py @@ -0,0 +1,35 @@ +""" +Support for Fibaro scenes. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/scene.fibaro/ +""" +import logging + +from homeassistant.components.scene import ( + Scene) +from homeassistant.components.fibaro import ( + FIBARO_CONTROLLER, FIBARO_DEVICES, FibaroDevice) + +DEPENDENCIES = ['fibaro'] + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_platform(hass, config, async_add_entities, + discovery_info=None): + """Perform the setup for Fibaro scenes.""" + if discovery_info is None: + return + + async_add_entities( + [FibaroScene(scene, hass.data[FIBARO_CONTROLLER]) + for scene in hass.data[FIBARO_DEVICES]['scene']], True) + + +class FibaroScene(FibaroDevice, Scene): + """Representation of a Fibaro scene entity.""" + + def activate(self): + """Activate the scene.""" + self.fibaro_device.start()