Add should_fire_event in rfxtrx component

This commit is contained in:
badele 2015-11-26 07:52:37 +01:00
parent 3e60c4801c
commit 4bd0db30c9
3 changed files with 107 additions and 36 deletions

View File

@ -8,10 +8,16 @@ https://home-assistant.io/components/light.rfxtrx/
"""
import logging
import homeassistant.components.rfxtrx as rfxtrx
import RFXtrx as rfxtrxmod
from homeassistant.components.light import Light
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']
_LOGGER = logging.getLogger(__name__)
@ -19,16 +25,22 @@ _LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Setup the RFXtrx platform. """
import RFXtrx as rfxtrxmod
lights = []
devices = config.get('devices', None)
if devices:
for entity_id, entity_info in devices.items():
if entity_id not in rfxtrx.RFX_DEVICES:
_LOGGER.info("Add %s rfxtrx.light", entity_info['name'])
rfxobject = rfxtrx.get_rfx_object(entity_info['packetid'])
new_light = RfxtrxLight(entity_info['name'], rfxobject, False)
_LOGGER.info("Add %s rfxtrx.light", entity_info[ATTR_NAME])
# 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
lights.append(new_light)
@ -54,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)
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
add_devices_callback([new_light])
# 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(
"EntityID: %s light_update. Command: %s",
entity_id,
@ -67,10 +81,22 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
)
if event.values['Command'] == 'On'\
or event.values['Command'] == 'Off':
if event.values['Command'] == 'On':
rfxtrx.RFX_DEVICES[entity_id].turn_on()
else:
rfxtrx.RFX_DEVICES[entity_id].turn_off()
# Update the rfxtrx device state
is_on = event.values['Command'] == 'On'
# 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
if light_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
@ -79,10 +105,11 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
class RfxtrxLight(Light):
""" Provides a RFXtrx light. """
def __init__(self, name, event, state):
def __init__(self, name, event, datas):
self._name = name
self._event = event
self._state = state
self._state = datas[ATTR_STATE]
self._should_fire_event = datas[ATTR_FIREEVENT]
@property
def should_poll(self):
@ -94,6 +121,11 @@ class RfxtrxLight(Light):
""" Returns the name of the light if any. """
return self._name
@property
def should_fire_event(self):
""" Returns is the device must fire event"""
return self._should_fire_event
@property
def is_on(self):
""" True if light is on. """
@ -115,4 +147,4 @@ class RfxtrxLight(Light):
self._event.device.send_off(rfxtrx.RFXOBJECT.transport)
self._state = False
self.update_ha_state()
self.update_ha_state()

View File

@ -4,7 +4,7 @@ homeassistant.components.rfxtrx
Provides support for RFXtrx components.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/rfxtrx/
https://home-assistant.io/components/rfxtrx.html
"""
import logging
from homeassistant.util import slugify
@ -14,8 +14,16 @@ REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/0.2.zip' +
'#RFXtrx==0.2']
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 = []
RFX_DEVICES = {}
_LOGGER = logging.getLogger(__name__)
@ -50,15 +58,15 @@ def setup(hass, config):
# Init the rfxtrx module
global RFXOBJECT
if CONF_DEVICE not in config[DOMAIN]:
if ATTR_DEVICE not in config[DOMAIN]:
_LOGGER.exception(
"can found device parameter in %s YAML configuration section",
DOMAIN
)
return False
device = config[DOMAIN][CONF_DEVICE]
debug = config[DOMAIN].get(CONF_DEBUG, False)
device = config[DOMAIN][ATTR_DEVICE]
debug = config[DOMAIN].get(ATTR_DEBUG, False)
RFXOBJECT = rfxtrxmod.Core(device, handle_receive, debug=debug)
@ -86,4 +94,4 @@ def get_rfx_object(packetid):
return obj
return None
return None

View File

@ -8,10 +8,16 @@ https://home-assistant.io/components/switch.rfxtrx/
"""
import logging
import homeassistant.components.rfxtrx as rfxtrx
import RFXtrx as rfxtrxmod
from homeassistant.components.switch import SwitchDevice
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']
_LOGGER = logging.getLogger(__name__)
@ -19,7 +25,6 @@ _LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Setup the RFXtrx platform. """
from RFXtrx import LightingDevice
# Add switch from config file
switchs = []
@ -27,9 +32,15 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
if devices:
for entity_id, entity_info in devices.items():
if entity_id not in rfxtrx.RFX_DEVICES:
_LOGGER.info("Add %s rfxtrx.switch", entity_info['name'])
rfxobject = rfxtrx.get_rfx_object(entity_info['packetid'])
newswitch = RfxtrxSwitch(entity_info['name'], rfxobject, False)
_LOGGER.info("Add %s rfxtrx.switch", entity_info[ATTR_NAME])
# 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
switchs.append(newswitch)
@ -37,7 +48,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
def switch_update(event):
""" Callback for sensor updates from the RFXtrx gateway. """
if isinstance(event.device, LightingDevice):
if not isinstance(event.device, rfxtrxmod.LightingDevice):
return
# 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)
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
add_devices_callback([new_switch])
# 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(
"EntityID: %s switch_update. Command: %s",
entity_id,
@ -68,10 +81,22 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
)
if event.values['Command'] == 'On'\
or event.values['Command'] == 'Off':
if event.values['Command'] == 'On':
rfxtrx.RFX_DEVICES[entity_id].turn_on()
else:
rfxtrx.RFX_DEVICES[entity_id].turn_off()
# Update the rfxtrx device state
is_on = event.values['Command'] == 'On'
# 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
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):
""" Provides a RFXtrx switch. """
def __init__(self, name, event, state):
def __init__(self, name, event, datas):
self._name = name
self._event = event
self._state = state
self._state = datas[ATTR_STATE]
self._should_fire_event = datas[ATTR_FIREEVENT]
@property
def should_poll(self):
@ -95,9 +121,14 @@ class RfxtrxSwitch(SwitchDevice):
""" Returns the name of the device if any. """
return self._name
@property
def should_fire_event(self):
""" Returns is the device must fire event"""
return self._should_fire_event
@property
def is_on(self):
""" True if device is on. """
""" True if light is on. """
return self._state
def turn_on(self, **kwargs):
@ -114,4 +145,4 @@ class RfxtrxSwitch(SwitchDevice):
self._event.device.send_off(rfxtrx.RFXOBJECT.transport)
self._state = False
self.update_ha_state()
self.update_ha_state()