Improve the robustness of the rfxtrx module, and solve issue #1116

This commit is contained in:
Daniel 2016-02-05 13:46:54 +01:00
parent a5db23afa4
commit cdf0e80773
4 changed files with 36 additions and 18 deletions

View File

@ -40,6 +40,10 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
datas = {ATTR_STATE: False, ATTR_FIREEVENT: fire_event} datas = {ATTR_STATE: False, ATTR_FIREEVENT: fire_event}
rfxobject = rfxtrx.get_rfx_object(entity_info[ATTR_PACKETID]) rfxobject = rfxtrx.get_rfx_object(entity_info[ATTR_PACKETID])
if not isinstance(rfxobject.device, rfxtrxmod.LightDevice):
_LOGGER.exception("%s is not a light",
entity_info[ATTR_NAME])
return
new_light = RfxtrxLight( new_light = RfxtrxLight(
entity_info[ATTR_NAME], rfxobject, datas entity_info[ATTR_NAME], rfxobject, datas
) )
@ -50,7 +54,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
def light_update(event): def light_update(event):
""" Callback for light updates from the RFXtrx gateway. """ """ Callback for light updates from the RFXtrx gateway. """
if not isinstance(event.device, rfxtrxmod.LightingDevice): if not isinstance(event.device, rfxtrxmod.LightDevice):
return return
# Add entity if not exist and the automatic_add is True # Add entity if not exist and the automatic_add is True
@ -74,13 +78,13 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
add_devices_callback([new_light]) add_devices_callback([new_light])
# Check if entity exists or previously added automatically # Check if entity exists or previously added automatically
if entity_id in rfxtrx.RFX_DEVICES \ if entity_id in rfxtrx.RFX_DEVICES:
and isinstance(rfxtrx.RFX_DEVICES[entity_id], RfxtrxLight):
_LOGGER.debug( _LOGGER.debug(
"EntityID: %s light_update. Command: %s", "EntityID: %s light_update. Command: %s",
entity_id, entity_id,
event.values['Command'] event.values['Command']
) )
if event.values['Command'] == 'On'\ if event.values['Command'] == 'On'\
or event.values['Command'] == 'Off': or event.values['Command'] == 'Off':
@ -90,15 +94,27 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
rfxtrx.RFX_DEVICES[entity_id]._state = is_on rfxtrx.RFX_DEVICES[entity_id]._state = is_on
rfxtrx.RFX_DEVICES[entity_id].update_ha_state() rfxtrx.RFX_DEVICES[entity_id].update_ha_state()
# Fire event elif event.values['Command'] == 'Set level':
if rfxtrx.RFX_DEVICES[entity_id].should_fire_event: # pylint: disable=protected-access
rfxtrx.RFX_DEVICES[entity_id].hass.bus.fire( rfxtrx.RFX_DEVICES[entity_id]._brightness = \
EVENT_BUTTON_PRESSED, { (event.values['Dim level'] * 255 // 100)
ATTR_ENTITY_ID:
rfxtrx.RFX_DEVICES[entity_id].entity_id, # Update the rfxtrx device state
ATTR_STATE: event.values['Command'].lower() is_on = rfxtrx.RFX_DEVICES[entity_id]._brightness > 0
} rfxtrx.RFX_DEVICES[entity_id]._state = is_on
) rfxtrx.RFX_DEVICES[entity_id].update_ha_state()
else:
return
# Fire event
if rfxtrx.RFX_DEVICES[entity_id].should_fire_event:
rfxtrx.RFX_DEVICES[entity_id].hass.bus.fire(
EVENT_BUTTON_PRESSED, {
ATTR_ENTITY_ID:
rfxtrx.RFX_DEVICES[entity_id].entity_id,
ATTR_STATE: event.values['Command'].lower()
}
)
# Subscribe to main rfxtrx events # Subscribe to main rfxtrx events
if light_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS: if light_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:

View File

@ -9,8 +9,8 @@ https://home-assistant.io/components/rfxtrx/
import logging import logging
from homeassistant.util import slugify from homeassistant.util import slugify
REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/0.2.zip' + REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/0.3.zip' +
'#RFXtrx==0.2'] '#RFXtrx==0.3']
DOMAIN = "rfxtrx" DOMAIN = "rfxtrx"
@ -37,6 +37,8 @@ def setup(hass, config):
""" Callback all subscribers for RFXtrx gateway. """ """ Callback all subscribers for RFXtrx gateway. """
# Log RFXCOM event # Log RFXCOM event
if not event.device.id_string:
return
entity_id = slugify(event.device.id_string.lower()) entity_id = slugify(event.device.id_string.lower())
packet_id = "".join("{0:02x}".format(x) for x in event.data) packet_id = "".join("{0:02x}".format(x) for x in event.data)
entity_name = "%s : %s" % (entity_id, packet_id) entity_name = "%s : %s" % (entity_id, packet_id)

View File

@ -49,7 +49,8 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
def switch_update(event): def switch_update(event):
""" Callback for sensor updates from the RFXtrx gateway. """ """ Callback for sensor updates from the RFXtrx gateway. """
if not isinstance(event.device, rfxtrxmod.LightingDevice): if not isinstance(event.device, rfxtrxmod.SwitchDevice) or \
isinstance(event.device, rfxtrxmod.LightDevice):
return return
# Add entity if not exist and the automatic_add is True # Add entity if not exist and the automatic_add is True
@ -73,8 +74,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
add_devices_callback([new_switch]) add_devices_callback([new_switch])
# Check if entity exists or previously added automatically # Check if entity exists or previously added automatically
if entity_id in rfxtrx.RFX_DEVICES \ if entity_id in rfxtrx.RFX_DEVICES:
and isinstance(rfxtrx.RFX_DEVICES[entity_id], RfxtrxSwitch):
_LOGGER.debug( _LOGGER.debug(
"EntityID: %s switch_update. Command: %s", "EntityID: %s switch_update. Command: %s",
entity_id, entity_id,

View File

@ -61,7 +61,7 @@ hikvision==0.4
# http://github.com/mala-zaba/Adafruit_Python_DHT/archive/4101340de8d2457dd194bca1e8d11cbfc237e919.zip#Adafruit_DHT==1.1.0 # http://github.com/mala-zaba/Adafruit_Python_DHT/archive/4101340de8d2457dd194bca1e8d11cbfc237e919.zip#Adafruit_DHT==1.1.0
# homeassistant.components.rfxtrx # homeassistant.components.rfxtrx
https://github.com/Danielhiversen/pyRFXtrx/archive/0.2.zip#RFXtrx==0.2 https://github.com/Danielhiversen/pyRFXtrx/archive/0.3.zip#RFXtrx==0.3
# homeassistant.components.sensor.netatmo # homeassistant.components.sensor.netatmo
https://github.com/HydrelioxGitHub/netatmo-api-python/archive/43ff238a0122b0939a0dc4e8836b6782913fb6e2.zip#lnetatmo==0.4.0 https://github.com/HydrelioxGitHub/netatmo-api-python/archive/43ff238a0122b0939a0dc4e8836b6782913fb6e2.zip#lnetatmo==0.4.0