Merge pull request #665 from badele/rfxtrx_event

Add should_fire_event in rfxtrx component
This commit is contained in:
Paulus Schoutsen 2015-11-25 23:40:39 -08:00
commit c4d4dcccb7
3 changed files with 102 additions and 30 deletions

View File

@ -12,6 +12,11 @@ import homeassistant.components.rfxtrx as rfxtrx
from homeassistant.components.light import Light from homeassistant.components.light import Light
from homeassistant.util import slugify from homeassistant.util import slugify
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.components.rfxtrx import ATTR_STATE, ATTR_FIREEVENT, ATTR_PACKETID, \
ATTR_NAME, EVENT_BUTTON_PRESSED
DEPENDENCIES = ['rfxtrx'] DEPENDENCIES = ['rfxtrx']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -23,12 +28,20 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
lights = [] lights = []
devices = config.get('devices', None) devices = config.get('devices', None)
if devices: if devices:
for entity_id, entity_info in devices.items(): for entity_id, entity_info in devices.items():
if entity_id not in rfxtrx.RFX_DEVICES: if entity_id not in rfxtrx.RFX_DEVICES:
_LOGGER.info("Add %s rfxtrx.light", entity_info['name']) _LOGGER.info("Add %s rfxtrx.light", entity_info[ATTR_NAME])
rfxobject = rfxtrx.get_rfx_object(entity_info['packetid'])
new_light = RfxtrxLight(entity_info['name'], rfxobject, False) # Check if i must fire event
fire_event = entity_info.get(ATTR_FIREEVENT, False)
datas = {ATTR_STATE: False, ATTR_FIREEVENT: fire_event}
rfxobject = rfxtrx.get_rfx_object(entity_info[ATTR_PACKETID])
new_light = RfxtrxLight(
entity_info[ATTR_NAME], rfxobject, datas
)
rfxtrx.RFX_DEVICES[entity_id] = new_light rfxtrx.RFX_DEVICES[entity_id] = new_light
lights.append(new_light) lights.append(new_light)
@ -54,12 +67,14 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
) )
pkt_id = "".join("{0:02x}".format(x) for x in event.data) pkt_id = "".join("{0:02x}".format(x) for x in event.data)
entity_name = "%s : %s" % (entity_id, pkt_id) entity_name = "%s : %s" % (entity_id, pkt_id)
new_light = RfxtrxLight(entity_name, event, False) datas = {ATTR_STATE: False, ATTR_FIREEVENT: False}
new_light = RfxtrxLight(entity_name, event, datas)
rfxtrx.RFX_DEVICES[entity_id] = new_light rfxtrx.RFX_DEVICES[entity_id] = new_light
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,
@ -67,10 +82,22 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
) )
if event.values['Command'] == 'On'\ if event.values['Command'] == 'On'\
or event.values['Command'] == 'Off': or event.values['Command'] == 'Off':
if event.values['Command'] == 'On':
rfxtrx.RFX_DEVICES[entity_id].turn_on() # Update the rfxtrx device state
else: is_on = event.values['Command'] == 'On'
rfxtrx.RFX_DEVICES[entity_id].turn_off() # pylint: disable=protected-access
rfxtrx.RFX_DEVICES[entity_id]._state = is_on
rfxtrx.RFX_DEVICES[entity_id].update_ha_state()
# 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:
@ -79,10 +106,11 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
class RfxtrxLight(Light): class RfxtrxLight(Light):
""" Provides a RFXtrx light. """ """ Provides a RFXtrx light. """
def __init__(self, name, event, state): def __init__(self, name, event, datas):
self._name = name self._name = name
self._event = event self._event = event
self._state = state self._state = datas[ATTR_STATE]
self._should_fire_event = datas[ATTR_FIREEVENT]
@property @property
def should_poll(self): def should_poll(self):
@ -94,6 +122,11 @@ class RfxtrxLight(Light):
""" Returns the name of the light if any. """ """ Returns the name of the light if any. """
return self._name return self._name
@property
def should_fire_event(self):
""" Returns is the device must fire event"""
return self._should_fire_event
@property @property
def is_on(self): def is_on(self):
""" True if light is on. """ """ True if light is on. """

View File

@ -14,8 +14,16 @@ REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/0.2.zip' +
'#RFXtrx==0.2'] '#RFXtrx==0.2']
DOMAIN = "rfxtrx" DOMAIN = "rfxtrx"
CONF_DEVICE = 'device'
CONF_DEBUG = 'debug' ATTR_DEVICE = 'device'
ATTR_DEBUG = 'debug'
ATTR_STATE = 'state'
ATTR_NAME = 'name'
ATTR_PACKETID = 'packetid'
ATTR_FIREEVENT = 'fire_event'
EVENT_BUTTON_PRESSED = 'button_pressed'
RECEIVED_EVT_SUBSCRIBERS = [] RECEIVED_EVT_SUBSCRIBERS = []
RFX_DEVICES = {} RFX_DEVICES = {}
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -50,15 +58,15 @@ def setup(hass, config):
# Init the rfxtrx module # Init the rfxtrx module
global RFXOBJECT global RFXOBJECT
if CONF_DEVICE not in config[DOMAIN]: if ATTR_DEVICE not in config[DOMAIN]:
_LOGGER.exception( _LOGGER.exception(
"can found device parameter in %s YAML configuration section", "can found device parameter in %s YAML configuration section",
DOMAIN DOMAIN
) )
return False return False
device = config[DOMAIN][CONF_DEVICE] device = config[DOMAIN][ATTR_DEVICE]
debug = config[DOMAIN].get(CONF_DEBUG, False) debug = config[DOMAIN].get(ATTR_DEBUG, False)
RFXOBJECT = rfxtrxmod.Core(device, handle_receive, debug=debug) RFXOBJECT = rfxtrxmod.Core(device, handle_receive, debug=debug)

View File

@ -12,6 +12,11 @@ import homeassistant.components.rfxtrx as rfxtrx
from homeassistant.components.switch import SwitchDevice from homeassistant.components.switch import SwitchDevice
from homeassistant.util import slugify from homeassistant.util import slugify
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.components.rfxtrx import ATTR_STATE, ATTR_FIREEVENT, ATTR_PACKETID, \
ATTR_NAME, EVENT_BUTTON_PRESSED
DEPENDENCIES = ['rfxtrx'] DEPENDENCIES = ['rfxtrx']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -19,7 +24,7 @@ _LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices_callback, discovery_info=None): def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Setup the RFXtrx platform. """ """ Setup the RFXtrx platform. """
from RFXtrx import LightingDevice import RFXtrx as rfxtrxmod
# Add switch from config file # Add switch from config file
switchs = [] switchs = []
@ -27,9 +32,15 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
if devices: if devices:
for entity_id, entity_info in devices.items(): for entity_id, entity_info in devices.items():
if entity_id not in rfxtrx.RFX_DEVICES: if entity_id not in rfxtrx.RFX_DEVICES:
_LOGGER.info("Add %s rfxtrx.switch", entity_info['name']) _LOGGER.info("Add %s rfxtrx.switch", entity_info[ATTR_NAME])
rfxobject = rfxtrx.get_rfx_object(entity_info['packetid'])
newswitch = RfxtrxSwitch(entity_info['name'], rfxobject, False) # Check if i must fire event
fire_event = entity_info.get(ATTR_FIREEVENT, False)
datas = {ATTR_STATE: False, ATTR_FIREEVENT: fire_event}
rfxobject = rfxtrx.get_rfx_object(entity_info[ATTR_PACKETID])
newswitch = RfxtrxSwitch(
entity_info[ATTR_NAME], rfxobject, datas)
rfxtrx.RFX_DEVICES[entity_id] = newswitch rfxtrx.RFX_DEVICES[entity_id] = newswitch
switchs.append(newswitch) switchs.append(newswitch)
@ -37,7 +48,7 @@ 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 isinstance(event.device, LightingDevice): if not isinstance(event.device, rfxtrxmod.LightingDevice):
return return
# Add entity if not exist and the automatic_add is True # Add entity if not exist and the automatic_add is True
@ -55,12 +66,14 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
) )
pkt_id = "".join("{0:02x}".format(x) for x in event.data) pkt_id = "".join("{0:02x}".format(x) for x in event.data)
entity_name = "%s : %s" % (entity_id, pkt_id) entity_name = "%s : %s" % (entity_id, pkt_id)
new_switch = RfxtrxSwitch(entity_name, event, False) datas = {ATTR_STATE: False, ATTR_FIREEVENT: False}
new_switch = RfxtrxSwitch(entity_name, event, datas)
rfxtrx.RFX_DEVICES[entity_id] = new_switch rfxtrx.RFX_DEVICES[entity_id] = new_switch
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,
@ -68,10 +81,22 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
) )
if event.values['Command'] == 'On'\ if event.values['Command'] == 'On'\
or event.values['Command'] == 'Off': or event.values['Command'] == 'Off':
if event.values['Command'] == 'On':
rfxtrx.RFX_DEVICES[entity_id].turn_on() # Update the rfxtrx device state
else: is_on = event.values['Command'] == 'On'
rfxtrx.RFX_DEVICES[entity_id].turn_off() # pylint: disable=protected-access
rfxtrx.RFX_DEVICES[entity_id]._state = is_on
rfxtrx.RFX_DEVICES[entity_id].update_ha_state()
# 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 switch_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS: if switch_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
@ -80,10 +105,11 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
class RfxtrxSwitch(SwitchDevice): class RfxtrxSwitch(SwitchDevice):
""" Provides a RFXtrx switch. """ """ Provides a RFXtrx switch. """
def __init__(self, name, event, state): def __init__(self, name, event, datas):
self._name = name self._name = name
self._event = event self._event = event
self._state = state self._state = datas[ATTR_STATE]
self._should_fire_event = datas[ATTR_FIREEVENT]
@property @property
def should_poll(self): def should_poll(self):
@ -95,9 +121,14 @@ class RfxtrxSwitch(SwitchDevice):
""" Returns the name of the device if any. """ """ Returns the name of the device if any. """
return self._name return self._name
@property
def should_fire_event(self):
""" Returns is the device must fire event"""
return self._should_fire_event
@property @property
def is_on(self): def is_on(self):
""" True if device is on. """ """ True if light is on. """
return self._state return self._state
def turn_on(self, **kwargs): def turn_on(self, **kwargs):