Hotfix for crash with virtual devices (#18808)

* Quickfix for crash with virtual devices

Added try/except to critical loops of processing
Reinforced read_devices, map_device_to_type and update processing

* oops
pull/18811/head
pbalogh77 2018-11-29 22:57:05 +01:00 committed by Paulus Schoutsen
parent 31d7221c90
commit 0467d0563a
1 changed files with 50 additions and 40 deletions

View File

@ -103,29 +103,31 @@ class FibaroController():
"""Handle change report received from the HomeCenter.""" """Handle change report received from the HomeCenter."""
callback_set = set() callback_set = set()
for change in state.get('changes', []): for change in state.get('changes', []):
dev_id = change.pop('id') try:
for property_name, value in change.items(): dev_id = change.pop('id')
if property_name == 'log': if dev_id not in self._device_map.keys():
if value and value != "transfer OK":
_LOGGER.debug("LOG %s: %s",
self._device_map[dev_id].friendly_name,
value)
continue continue
if property_name == 'logTemp': device = self._device_map[dev_id]
continue for property_name, value in change.items():
if property_name in self._device_map[dev_id].properties: if property_name == 'log':
self._device_map[dev_id].properties[property_name] = \ if value and value != "transfer OK":
value _LOGGER.debug("LOG %s: %s",
_LOGGER.debug("<- %s.%s = %s", device.friendly_name, value)
self._device_map[dev_id].ha_id, continue
property_name, if property_name == 'logTemp':
str(value)) continue
else: if property_name in device.properties:
_LOGGER.warning("Error updating %s data of %s, not found", device.properties[property_name] = \
property_name, value
self._device_map[dev_id].ha_id) _LOGGER.debug("<- %s.%s = %s", device.ha_id,
if dev_id in self._callbacks: property_name, str(value))
callback_set.add(dev_id) else:
_LOGGER.warning("%s.%s not found", device.ha_id,
property_name)
if dev_id in self._callbacks:
callback_set.add(dev_id)
except (ValueError, KeyError):
pass
for item in callback_set: for item in callback_set:
self._callbacks[item]() self._callbacks[item]()
@ -137,8 +139,12 @@ class FibaroController():
def _map_device_to_type(device): def _map_device_to_type(device):
"""Map device to HA device type.""" """Map device to HA device type."""
# Use our lookup table to identify device type # Use our lookup table to identify device type
device_type = FIBARO_TYPEMAP.get( if 'type' in device:
device.type, FIBARO_TYPEMAP.get(device.baseType)) device_type = FIBARO_TYPEMAP.get(device.type)
elif 'baseType' in device:
device_type = FIBARO_TYPEMAP.get(device.baseType)
else:
device_type = None
# We can also identify device type by its capabilities # We can also identify device type by its capabilities
if device_type is None: if device_type is None:
@ -156,8 +162,7 @@ class FibaroController():
# Switches that control lights should show up as lights # Switches that control lights should show up as lights
if device_type == 'switch' and \ if device_type == 'switch' and \
'isLight' in device.properties and \ device.properties.get('isLight', 'false') == 'true':
device.properties.isLight == 'true':
device_type = 'light' device_type = 'light'
return device_type return device_type
@ -165,26 +170,31 @@ class FibaroController():
"""Read and process the device list.""" """Read and process the device list."""
devices = self._client.devices.list() devices = self._client.devices.list()
self._device_map = {} self._device_map = {}
for device in devices:
if device.roomID == 0:
room_name = 'Unknown'
else:
room_name = self._room_map[device.roomID].name
device.friendly_name = room_name + ' ' + device.name
device.ha_id = '{}_{}_{}'.format(
slugify(room_name), slugify(device.name), device.id)
self._device_map[device.id] = device
self.fibaro_devices = defaultdict(list) self.fibaro_devices = defaultdict(list)
for device in self._device_map.values(): for device in devices:
if device.enabled and \ try:
(not device.isPlugin or self._import_plugins): if device.roomID == 0:
device.mapped_type = self._map_device_to_type(device) room_name = 'Unknown'
else:
room_name = self._room_map[device.roomID].name
device.friendly_name = room_name + ' ' + device.name
device.ha_id = '{}_{}_{}'.format(
slugify(room_name), slugify(device.name), device.id)
if device.enabled and \
('isPlugin' not in device or
(not device.isPlugin or self._import_plugins)):
device.mapped_type = self._map_device_to_type(device)
else:
device.mapped_type = None
if device.mapped_type: if device.mapped_type:
self._device_map[device.id] = device
self.fibaro_devices[device.mapped_type].append(device) self.fibaro_devices[device.mapped_type].append(device)
else: else:
_LOGGER.debug("%s (%s, %s) not mapped", _LOGGER.debug("%s (%s, %s) not used",
device.ha_id, device.type, device.ha_id, device.type,
device.baseType) device.baseType)
except (KeyError, ValueError):
pass
def setup(hass, config): def setup(hass, config):