Skip to content

Commit

Permalink
Fix timing bug while linking HM device to HA object
Browse files Browse the repository at this point in the history
  • Loading branch information
pvizeli committed Jun 26, 2016
1 parent fb3e388 commit 3afc566
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions homeassistant/components/homematic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
REQUIREMENTS = ['pyhomematic==0.1.6']

HOMEMATIC = None
HOMEMATIC_LINK_DELAY = 0.5
HOMEMATIC_DEVICES = {}

DISCOVER_SWITCHES = "homematic.switch"
Expand Down Expand Up @@ -71,7 +72,7 @@
# pylint: disable=unused-argument
def setup(hass, config):
"""Setup the Homematic component."""
global HOMEMATIC
global HOMEMATIC, HOMEMATIC_LINK_DELAY

from pyhomematic import HMConnection

Expand All @@ -80,6 +81,7 @@ def setup(hass, config):
remote_ip = config[DOMAIN].get("remote_ip", None)
remote_port = config[DOMAIN].get("remote_port", 2001)
resolvenames = config[DOMAIN].get("resolvenames", False)
HOMEMATIC_LINK_DELAY = config[DOMAIN].get("delay", 0.5)

if remote_ip is None or local_ip is None:
_LOGGER.error("Missing remote CCU/Homegear or local address")
Expand Down Expand Up @@ -108,27 +110,30 @@ def setup(hass, config):
# pylint: disable=too-many-branches
def system_callback_handler(hass, config, src, *args):
"""Callback handler."""
delay = config[DOMAIN].get("delay", 0.5)
if src == 'newDevices':
_LOGGER.debug("newDevices with: %s", str(args))
# pylint: disable=unused-variable
(interface_id, dev_descriptions) = args
key_dict = {}
# Get list of all keys of the devices (ignoring channels)
for dev in dev_descriptions:
key_dict[dev['ADDRESS'].split(':')[0]] = True

# Connect devices already created in HA to pyhomematic and
# add remaining devices to list
devices_not_created = []
for dev in key_dict:
if dev in HOMEMATIC_DEVICES:
for hm_element in HOMEMATIC_DEVICES[dev]:
hm_element.link_homematic(delay=delay)
hm_element.link_homematic()
else:
devices_not_created.append(dev)

# If configuration allows autodetection of devices,
# all devices not configured are added.
autodetect = config[DOMAIN].get("autodetect", False)
_LOGGER.debug("Autodetect is %s / unknown device: %s", str(autodetect),
str(devices_not_created))
if autodetect and devices_not_created:
for component_name, discovery_type in (
('switch', DISCOVER_SWITCHES),
Expand All @@ -149,11 +154,6 @@ def system_callback_handler(hass, config, src, *args):
ATTR_DISCOVER_DEVICES: found_devices
}, config)

for dev in devices_not_created:
if dev in HOMEMATIC_DEVICES:
for hm_element in HOMEMATIC_DEVICES[dev]:
hm_element.link_homematic(delay=delay)


def _get_devices(device_type, keys):
"""Get devices."""
Expand Down Expand Up @@ -269,6 +269,7 @@ def setup_hmdevice_discovery_helper(hmdevicetype, discovery_info,
add_callback_devices)
if not ret:
_LOGGER.error("Setup discovery error with config %s", str(config))

return True


Expand All @@ -284,6 +285,8 @@ def setup_hmdevice_entity_helper(hmdevicetype, config, add_callback_devices):
"'address' missing in configuration.", address)
return False

_LOGGER.debug("Add device %s from config: %s",
str(hmdevicetype), str(config))
# Create a new HA homematic object
new_device = hmdevicetype(config)
if address not in HOMEMATIC_DEVICES:
Expand All @@ -292,6 +295,10 @@ def setup_hmdevice_entity_helper(hmdevicetype, config, add_callback_devices):

# Add to HA
add_callback_devices([new_device])

# HM is connected
if address in HOMEMATIC.devices:
return new_device.link_homematic()
return True


Expand Down Expand Up @@ -360,8 +367,12 @@ def device_state_attributes(self):

return attr

def link_homematic(self, delay=0.5):
def link_homematic(self):
"""Connect to homematic."""
# device is already linked
if self._connected:
return True

# Does a HMDevice from pyhomematic exist?
if self._address in HOMEMATIC.devices:
# Init
Expand All @@ -374,10 +385,10 @@ def link_homematic(self, delay=0.5):
try:
# Init datapoints of this object
self._init_data_struct()
if delay:
if HOMEMATIC_LINK_DELAY:
# We delay / pause loading of data to avoid overloading
# of CCU / Homegear when doing auto detection
time.sleep(delay)
time.sleep(HOMEMATIC_LINK_DELAY)
self._load_init_data_from_hm()
_LOGGER.debug("%s datastruct: %s",
self._name, str(self._data))
Expand All @@ -388,23 +399,21 @@ def link_homematic(self, delay=0.5):
# pylint: disable=broad-except
except Exception as err:
self._connected = False
self._available = False
_LOGGER.error("Exception while linking %s: %s",
self._address, str(err))
else:
_LOGGER.critical("Delink %s object from HM!", self._name)
self._connected = False
self._available = False

# Update HA
_LOGGER.debug("%s linking down, send update_ha_state", self._name)
_LOGGER.debug("%s linking done, send update_ha_state", self._name)
self.update_ha_state()
else:
_LOGGER.debug("%s not found in HOMEMATIC.devices", self._address)

def _hm_event_callback(self, device, caller, attribute, value):
"""Handle all pyhomematic device events."""
_LOGGER.debug("%s receive event '%s' value: %s", self._name,
_LOGGER.debug("%s received event '%s' value: %s", self._name,
attribute, value)
have_change = False

Expand Down

0 comments on commit 3afc566

Please sign in to comment.