-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Support for multiple Fibaro gateways #19705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
4868a77
4dc95eb
ca565fc
d07cff5
8e4e157
48cff00
43adef1
4172234
d541d17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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" | ||
|
|
@@ -65,7 +66,7 @@ | |
|
|
||
| FIBARO_ID_LIST_SCHEMA = vol.Schema([cv.string]) | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema({ | ||
| GATEWAY_CONFIG = vol.Schema({ | ||
| DOMAIN: vol.Schema({ | ||
| vol.Required(CONF_PASSWORD): cv.string, | ||
| vol.Required(CONF_USERNAME): cv.string, | ||
|
|
@@ -77,18 +78,28 @@ | |
| }) | ||
| }, extra=vol.ALLOW_EXTRA) | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema({ | ||
| DOMAIN: vol.Schema({ | ||
| vol.Optional(CONF_GATEWAYS, default={}): | ||
|
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), | ||
|
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) | ||
|
pbalogh77 marked this conversation as resolved.
Outdated
|
||
| self._device_config = config.get(CONF_DEVICE_CONFIG, {}) | ||
|
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 | ||
|
|
@@ -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: | ||
|
|
@@ -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: | ||
|
|
@@ -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) | ||
|
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] = {} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't that be a less efficient algorithm, tho?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I'll keep in mind.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this line.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| 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 | ||
|
|
||
|
|
@@ -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 | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.