Moved trx/except, added debug messages, minor fixes

This commit is contained in:
Daniel Perna 2016-06-25 18:19:05 +02:00
parent 5ca26fc13f
commit 43faeff42a

View File

@ -15,11 +15,11 @@ homematic:
""" """
import time import time
import logging import logging
from homeassistant.const import EVENT_HOMEASSISTANT_STOP,\ from homeassistant.const import EVENT_HOMEASSISTANT_STOP, \
EVENT_PLATFORM_DISCOVERED,\ EVENT_PLATFORM_DISCOVERED, \
ATTR_SERVICE,\ ATTR_SERVICE, \
ATTR_DISCOVERED,\ ATTR_DISCOVERED, \
STATE_UNKNOWN STATE_UNKNOWN
from homeassistant.loader import get_component from homeassistant.loader import get_component
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
import homeassistant.bootstrap import homeassistant.bootstrap
@ -141,13 +141,8 @@ def system_callback_handler(src, *args):
('sensor', DISCOVER_SENSORS), ('sensor', DISCOVER_SENSORS),
('thermostat', DISCOVER_THERMOSTATS)): ('thermostat', DISCOVER_THERMOSTATS)):
# Get all devices of a specific type # Get all devices of a specific type
try: found_devices = _get_devices(discovery_type,
found_devices = _get_devices(discovery_type, devices_not_created)
devices_not_created)
# pylint: disable=broad-except
except Exception as err:
_LOGGER.error("Failed generate opt %s with error '%s'",
component_name, str(err))
# When devices of this type are found # When devices of this type are found
# they are setup in HA and an event is fired # they are setup in HA and an event is fired
@ -157,21 +152,21 @@ def system_callback_handler(src, *args):
# Ensure component is loaded # Ensure component is loaded
homeassistant.bootstrap.setup_component( homeassistant.bootstrap.setup_component(
_HM_DISCOVER_HASS, _HM_DISCOVER_HASS,
component.DOMAIN, component.DOMAIN,
config) config)
# Fire discovery event # Fire discovery event
_HM_DISCOVER_HASS.bus.fire( _HM_DISCOVER_HASS.bus.fire(
EVENT_PLATFORM_DISCOVERED, { EVENT_PLATFORM_DISCOVERED, {
ATTR_SERVICE: discovery_type, ATTR_SERVICE: discovery_type,
ATTR_DISCOVERED: { ATTR_DISCOVERED: {
ATTR_DISCOVER_DEVICES: ATTR_DISCOVER_DEVICES:
found_devices, found_devices,
ATTR_DISCOVER_CONFIG: '' ATTR_DISCOVER_CONFIG: ''
} }
} }
) )
for dev in devices_not_created: for dev in devices_not_created:
if dev in HOMEMATIC_DEVICES: if dev in HOMEMATIC_DEVICES:
@ -192,13 +187,12 @@ def _get_devices(device_type, keys):
device = HOMEMATIC.devices[key] device = HOMEMATIC.devices[key]
if device.__class__.__name__ not in HM_DEVICE_TYPES[device_type]: if device.__class__.__name__ not in HM_DEVICE_TYPES[device_type]:
continue continue
elements = device.ELEMENT + 1
metadata = {} metadata = {}
# Load metadata if needed to generate a param list # Load metadata if needed to generate a param list
if device_type is DISCOVER_SENSORS: if device_type == DISCOVER_SENSORS:
metadata.update(device.SENSORNODE) metadata.update(device.SENSORNODE)
elif device_type is DISCOVER_BINARY_SENSORS: elif device_type == DISCOVER_BINARY_SENSORS:
metadata.update(device.BINARYNODE) metadata.update(device.BINARYNODE)
# Also add supported events as binary type # Also add supported events as binary type
@ -207,45 +201,57 @@ def _get_devices(device_type, keys):
metadata.update({event: channel}) metadata.update({event: channel})
params = _create_params_list(device, metadata) params = _create_params_list(device, metadata)
if params:
# Generate options for 1...n elements with 1...n params
for channel in range(1, device.ELEMENT + 1):
_LOGGER.debug("Handling %s:%i", key, channel)
if channel in params:
for param in params[channel]:
name = _create_ha_name(name=device.NAME,
channel=channel,
param=param)
device_dict = dict(platform="homematic",
address=key,
name=name,
button=channel)
if param is not None:
device_dict["param"] = param
# Generate options for 1...n elements with 1...n params # Add new device
for channel in range(1, elements): device_arr.append(device_dict)
for param in params[channel]: else:
name = _create_ha_name(name=device.NAME, _LOGGER.debug("Channel %i not in params", channel)
channel=channel, else:
param=param) _LOGGER.debug("Got no params for %s", key)
device_dict = dict(platform="homematic", address=key, _LOGGER.debug("%s autodiscovery: %s",
name=name, button=channel) device_type, str(device_arr))
if param is not None:
device_dict["param"] = param
# Add new device
device_arr.append(device_dict)
_LOGGER.debug("%s autodiscovery: %s", device_type, str(device_arr))
return device_arr return device_arr
def _create_params_list(hmdevice, metadata): def _create_params_list(hmdevice, metadata):
"""Create a list from HMDevice with all possible parameters in config.""" """Create a list from HMDevice with all possible parameters in config."""
params = {} params = {}
elements = hmdevice.ELEMENT + 1
# Search in sensor and binary metadata per elements # Search in sensor and binary metadata per elements
for channel in range(1, elements): for channel in range(1, hmdevice.ELEMENT + 1):
param_chan = [] param_chan = []
for node, meta_chan in metadata.items(): try:
# Is this attribute ignored? for node, meta_chan in metadata.items():
if node in HM_IGNORE_DISCOVERY_NODE: # Is this attribute ignored?
continue if node in HM_IGNORE_DISCOVERY_NODE:
if meta_chan == 'c' or meta_chan is None: continue
# Only channel linked data if meta_chan == 'c' or meta_chan is None:
param_chan.append(node) # Only channel linked data
elif channel == 1: param_chan.append(node)
# First channel can have other data channel elif channel == 1:
param_chan.append(node) # First channel can have other data channel
param_chan.append(node)
# pylint: disable=broad-except
except Exception as err:
_LOGGER.error("Exception generating %s (%s): %s",
hmdevice.ADDRESS, str(metadata), str(err))
# Default parameter # Default parameter
if len(param_chan) == 0: if not param_chan:
param_chan.append(None) param_chan.append(None)
# Add to channel # Add to channel
params.update({channel: param_chan}) params.update({channel: param_chan})