Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ omit =
homeassistant/components/eufy.py
homeassistant/components/*/eufy.py

homeassistant/components/fibaro.py
homeassistant/components/fibaro/__init__.py
homeassistant/components/*/fibaro.py

homeassistant/components/gc100.py
Expand Down
8 changes: 4 additions & 4 deletions homeassistant/components/binary_sensor/fibaro.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from homeassistant.components.binary_sensor import (
BinarySensorDevice, ENTITY_ID_FORMAT)
from homeassistant.components.fibaro import (
FIBARO_CONTROLLER, FIBARO_DEVICES, FibaroDevice)
FIBARO_DEVICES, FibaroDevice)
from homeassistant.const import (CONF_DEVICE_CLASS, CONF_ICON)

DEPENDENCIES = ['fibaro']
Expand All @@ -33,17 +33,17 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
return

add_entities(
[FibaroBinarySensor(device, hass.data[FIBARO_CONTROLLER])
[FibaroBinarySensor(device)
for device in hass.data[FIBARO_DEVICES]['binary_sensor']], True)


class FibaroBinarySensor(FibaroDevice, BinarySensorDevice):
"""Representation of a Fibaro Binary Sensor."""

def __init__(self, fibaro_device, controller):
def __init__(self, fibaro_device):
"""Initialize the binary_sensor."""
self._state = None
super().__init__(fibaro_device, controller)
super().__init__(fibaro_device)
self.entity_id = ENTITY_ID_FORMAT.format(self.ha_id)
stype = None
devconf = fibaro_device.device_config
Expand Down
8 changes: 4 additions & 4 deletions homeassistant/components/cover/fibaro.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from homeassistant.components.cover import (
CoverDevice, ENTITY_ID_FORMAT, ATTR_POSITION, ATTR_TILT_POSITION)
from homeassistant.components.fibaro import (
FIBARO_CONTROLLER, FIBARO_DEVICES, FibaroDevice)
FIBARO_DEVICES, FibaroDevice)

DEPENDENCIES = ['fibaro']

Expand All @@ -22,16 +22,16 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
return

add_entities(
[FibaroCover(device, hass.data[FIBARO_CONTROLLER]) for
[FibaroCover(device) for
device in hass.data[FIBARO_DEVICES]['cover']], True)


class FibaroCover(FibaroDevice, CoverDevice):
"""Representation a Fibaro Cover."""

def __init__(self, fibaro_device, controller):
def __init__(self, fibaro_device):
"""Initialize the Vera device."""
super().__init__(fibaro_device, controller)
super().__init__(fibaro_device)
self.entity_id = ENTITY_ID_FORMAT.format(self.ha_id)

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import voluptuous as vol

from homeassistant.const import (
ATTR_ARMED, ATTR_BATTERY_LEVEL, CONF_DEVICE_CLASS,
CONF_EXCLUDE, CONF_ICON, CONF_PASSWORD, CONF_URL, CONF_USERNAME,
ATTR_ARMED, ATTR_BATTERY_LEVEL, CONF_DEVICE_CLASS, CONF_EXCLUDE,
CONF_ICON, CONF_PASSWORD, CONF_URL, CONF_USERNAME,
CONF_WHITE_VALUE, EVENT_HOMEASSISTANT_STOP)
from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv
Expand All @@ -24,10 +24,11 @@
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'fibaro'
FIBARO_DEVICES = 'fibaro_devices'
FIBARO_CONTROLLER = 'fibaro_controller'
FIBARO_CONTROLLERS = 'fibaro_controllers'
ATTR_CURRENT_POWER_W = "current_power_w"
ATTR_CURRENT_ENERGY_KWH = "current_energy_kwh"
CONF_PLUGINS = "plugins"
CONF_GATEWAYS = 'gateways'
CONF_DIMMING = "dimming"
CONF_COLOR = "color"
CONF_RESET_COLOR = "reset_color"
Expand Down Expand Up @@ -65,7 +66,7 @@

FIBARO_ID_LIST_SCHEMA = vol.Schema([cv.string])

CONFIG_SCHEMA = vol.Schema({
GATEWAY_CONFIG = vol.Schema({
DOMAIN: vol.Schema({
Comment thread
pbalogh77 marked this conversation as resolved.
Outdated
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_USERNAME): cv.string,
Expand All @@ -77,18 +78,28 @@
})
}, extra=vol.ALLOW_EXTRA)

CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Optional(CONF_GATEWAYS, default={}):
Comment thread
pbalogh77 marked this conversation as resolved.
Outdated
vol.All(cv.ensure_list, [GATEWAY_CONFIG])
})
}, extra=vol.ALLOW_EXTRA)


class FibaroController():
"""Initiate Fibaro Controller Class."""

def __init__(self, username, password, url, import_plugins, config):
def __init__(self, config):
"""Initialize the Fibaro controller."""
from fiblary3.client.v4.client import Client as FibaroClient
self._client = FibaroClient(url, username, password)

self._client = FibaroClient(config.get(CONF_URL),
Comment thread
pbalogh77 marked this conversation as resolved.
Outdated
config.get(CONF_USERNAME),
config.get(CONF_PASSWORD))
self._scene_map = None
# Whether to import devices from plugins
self._import_plugins = import_plugins
self._device_config = config[CONF_DEVICE_CONFIG]
self._import_plugins = config.get(CONF_PLUGINS, False)
Comment thread
pbalogh77 marked this conversation as resolved.
Outdated
self._device_config = config.get(CONF_DEVICE_CONFIG, {})
Comment thread
pbalogh77 marked this conversation as resolved.
Outdated
self._room_map = None # Mapping roomId to room object
self._device_map = None # Mapping deviceId to device object
self.fibaro_devices = None # List of devices by type
Expand Down Expand Up @@ -200,6 +211,7 @@ def _read_scenes(self):
for device in scenes:
if not device.visible:
continue
device.fibaro_controller = self
if device.roomID == 0:
room_name = 'Unknown'
else:
Expand All @@ -220,6 +232,7 @@ def _read_devices(self):
self.fibaro_devices = defaultdict(list)
for device in devices:
try:
device.fibaro_controller = self
if device.roomID == 0:
room_name = 'Unknown'
else:
Expand Down Expand Up @@ -250,25 +263,36 @@ def _read_devices(self):
pass


def setup(hass, config):
def setup(hass, base_config):
"""Set up the Fibaro Component."""
hass.data[FIBARO_CONTROLLER] = controller = \
FibaroController(config[DOMAIN][CONF_USERNAME],
config[DOMAIN][CONF_PASSWORD],
config[DOMAIN][CONF_URL],
config[DOMAIN][CONF_PLUGINS],
config[DOMAIN])
config = base_config.get(DOMAIN)
gateways = config.get(CONF_GATEWAYS)
Comment thread
pbalogh77 marked this conversation as resolved.
Outdated
hass.data[FIBARO_CONTROLLERS] = {}

def stop_fibaro(event):
"""Stop Fibaro Thread."""
_LOGGER.info("Shutting down Fibaro connection")
hass.data[FIBARO_CONTROLLER].disable_state_handler()

if controller.connect():
hass.data[FIBARO_DEVICES] = controller.fibaro_devices
for controller in hass.data[FIBARO_CONTROLLERS].values():
controller.disable_state_handler()
hass.data[FIBARO_CONTROLLERS] = {}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't need this key anymore we should pop it or delete it from the dict.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that be a less efficient algorithm, tho?
I know it's highly theoretical either way, since it's usually a single element list and it's probably equally readable either way, but this seems like a more appropriate solution on an embedded device. I could be wrong, I'm new here. Of course if you insist, I'll modify it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should worry about speed here, but clean up remaining references.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now though we don't really need to clean up anything, since this is only done when home assistant shuts down. So we can remove this line completely. Clean up will be relevant if moving this component to use config entries.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll keep in mind.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I removed it. Just for my education, what was wrong with that line? I'm new to python but as far as I understand it did remove the reference to all items in the map and by that freeing up the memory. I understand your suggestion that you'd prefer enumerate + pop, but coming from decades of embedded c programming and knowing how enumerate and pop works, that's really a very wasteful way of solving the problem. Afaik, enumerate iterates through the list an extra time, creates a new index, and pop unlinks items one-by-one from the list. I could be totally wrong on my assumptions, but in general this construct feels like something to discourage on an embedded platform. I could be totally not pythonic in thinking this.
Anyhow, I'd love to learn. Thanks in advance.


hass.data[FIBARO_DEVICES] = {}
for component in FIBARO_COMPONENTS:
hass.data[FIBARO_DEVICES][component] = []

for gateway in gateways:
controller = FibaroController(gateway)
if controller.connect():
hass.data[FIBARO_CONTROLLERS][controller.hub_serial] = controller
for component in FIBARO_COMPONENTS:
hass.data[FIBARO_DEVICES][component].extend(
controller.fibaro_devices[component])

if hass.data[FIBARO_CONTROLLERS]:
for component in FIBARO_COMPONENTS:
discovery.load_platform(hass, component, DOMAIN, {}, config)
controller.enable_state_handler()
for controller in hass.data[FIBARO_CONTROLLERS].values():
controller.enable_state_handler()
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_fibaro)
return True

Expand All @@ -278,10 +302,10 @@ def stop_fibaro(event):
class FibaroDevice(Entity):
"""Representation of a Fibaro device entity."""

def __init__(self, fibaro_device, controller):
def __init__(self, fibaro_device):
"""Initialize the device."""
self.fibaro_device = fibaro_device
self.controller = controller
self.controller = fibaro_device.fibaro_controller
self._name = fibaro_device.friendly_name
self.ha_id = fibaro_device.ha_id

Expand Down
8 changes: 4 additions & 4 deletions homeassistant/components/light/fibaro.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from homeassistant.const import (
CONF_WHITE_VALUE)
from homeassistant.components.fibaro import (
FIBARO_CONTROLLER, FIBARO_DEVICES, FibaroDevice,
FIBARO_DEVICES, FibaroDevice,
CONF_DIMMING, CONF_COLOR, CONF_RESET_COLOR)
from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_HS_COLOR, ATTR_WHITE_VALUE, ENTITY_ID_FORMAT,
Expand Down Expand Up @@ -50,14 +50,14 @@ async def async_setup_platform(hass,
return

async_add_entities(
[FibaroLight(device, hass.data[FIBARO_CONTROLLER])
[FibaroLight(device)
for device in hass.data[FIBARO_DEVICES]['light']], True)


class FibaroLight(FibaroDevice, Light):
"""Representation of a Fibaro Light, including dimmable."""

def __init__(self, fibaro_device, controller):
def __init__(self, fibaro_device):
"""Initialize the light."""
self._brightness = None
self._color = (0, 0)
Expand All @@ -81,7 +81,7 @@ def __init__(self, fibaro_device, controller):
if devconf.get(CONF_WHITE_VALUE, supports_white_v):
self._supported_flags |= SUPPORT_WHITE_VALUE

super().__init__(fibaro_device, controller)
super().__init__(fibaro_device)
self.entity_id = ENTITY_ID_FORMAT.format(self.ha_id)

@property
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/scene/fibaro.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from homeassistant.components.scene import (
Scene)
from homeassistant.components.fibaro import (
FIBARO_CONTROLLER, FIBARO_DEVICES, FibaroDevice)
FIBARO_DEVICES, FibaroDevice)

DEPENDENCIES = ['fibaro']

Expand All @@ -23,7 +23,7 @@ async def async_setup_platform(hass, config, async_add_entities,
return

async_add_entities(
[FibaroScene(scene, hass.data[FIBARO_CONTROLLER])
[FibaroScene(scene)
for scene in hass.data[FIBARO_DEVICES]['scene']], True)


Expand Down
8 changes: 4 additions & 4 deletions homeassistant/components/sensor/fibaro.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from homeassistant.helpers.entity import Entity
from homeassistant.components.sensor import ENTITY_ID_FORMAT
from homeassistant.components.fibaro import (
FIBARO_CONTROLLER, FIBARO_DEVICES, FibaroDevice)
FIBARO_DEVICES, FibaroDevice)

SENSOR_TYPES = {
'com.fibaro.temperatureSensor':
Expand All @@ -37,18 +37,18 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
return

add_entities(
[FibaroSensor(device, hass.data[FIBARO_CONTROLLER])
[FibaroSensor(device)
for device in hass.data[FIBARO_DEVICES]['sensor']], True)


class FibaroSensor(FibaroDevice, Entity):
"""Representation of a Fibaro Sensor."""

def __init__(self, fibaro_device, controller):
def __init__(self, fibaro_device):
"""Initialize the sensor."""
self.current_value = None
self.last_changed_time = None
super().__init__(fibaro_device, controller)
super().__init__(fibaro_device)
self.entity_id = ENTITY_ID_FORMAT.format(self.ha_id)
if fibaro_device.type in SENSOR_TYPES:
self._unit = SENSOR_TYPES[fibaro_device.type][1]
Expand Down
8 changes: 4 additions & 4 deletions homeassistant/components/switch/fibaro.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from homeassistant.util import convert
from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchDevice
from homeassistant.components.fibaro import (
FIBARO_CONTROLLER, FIBARO_DEVICES, FibaroDevice)
FIBARO_DEVICES, FibaroDevice)

DEPENDENCIES = ['fibaro']
_LOGGER = logging.getLogger(__name__)
Expand All @@ -21,17 +21,17 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
return

add_entities(
[FibaroSwitch(device, hass.data[FIBARO_CONTROLLER]) for
[FibaroSwitch(device) for
device in hass.data[FIBARO_DEVICES]['switch']], True)


class FibaroSwitch(FibaroDevice, SwitchDevice):
"""Representation of a Fibaro Switch."""

def __init__(self, fibaro_device, controller):
def __init__(self, fibaro_device):
"""Initialize the Fibaro device."""
self._state = False
super().__init__(fibaro_device, controller)
super().__init__(fibaro_device)
self.entity_id = ENTITY_ID_FORMAT.format(self.ha_id)

def turn_on(self, **kwargs):
Expand Down