Refactor rfxtrx (#9117)

* rfxtrx refactor

* rfxtrx refactor

* rfxtrx refactor

* rfxtrx refactor

* rfxtrx refactor

* rfxtrx refactor

* rfxtrx refactor

* rfxtrx refactor
This commit is contained in:
Daniel Høyer Iversen 2017-08-29 16:22:28 +02:00 committed by Pascal Vizeli
parent aa8dd8fbdd
commit ee28b439b3
5 changed files with 33 additions and 28 deletions

View File

@ -16,7 +16,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Set up the RFXtrx cover.""" """Set up the RFXtrx cover."""
import RFXtrx as rfxtrxmod import RFXtrx as rfxtrxmod
covers = rfxtrx.get_devices_from_config(config, RfxtrxCover, hass) covers = rfxtrx.get_devices_from_config(config, RfxtrxCover)
add_devices_callback(covers) add_devices_callback(covers)
def cover_update(event): def cover_update(event):
@ -26,7 +26,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
not event.device.known_to_be_rollershutter: not event.device.known_to_be_rollershutter:
return return
new_device = rfxtrx.get_new_device(event, config, RfxtrxCover, hass) new_device = rfxtrx.get_new_device(event, config, RfxtrxCover)
if new_device: if new_device:
add_devices_callback([new_device]) add_devices_callback([new_device])

View File

@ -23,7 +23,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the RFXtrx platform.""" """Set up the RFXtrx platform."""
import RFXtrx as rfxtrxmod import RFXtrx as rfxtrxmod
lights = rfxtrx.get_devices_from_config(config, RfxtrxLight, hass) lights = rfxtrx.get_devices_from_config(config, RfxtrxLight)
add_devices(lights) add_devices(lights)
def light_update(event): def light_update(event):
@ -32,7 +32,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
not event.device.known_to_be_dimmable: not event.device.known_to_be_dimmable:
return return
new_device = rfxtrx.get_new_device(event, config, RfxtrxLight, hass) new_device = rfxtrx.get_new_device(event, config, RfxtrxLight)
if new_device: if new_device:
add_devices([new_device]) add_devices([new_device])

View File

@ -4,6 +4,7 @@ Support for RFXtrx components.
For more details about this component, please refer to the documentation at For more details about this component, please refer to the documentation at
https://home-assistant.io/components/rfxtrx/ https://home-assistant.io/components/rfxtrx/
""" """
import logging import logging
from collections import OrderedDict from collections import OrderedDict
import voluptuous as vol import voluptuous as vol
@ -11,13 +12,14 @@ import voluptuous as vol
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.util import slugify from homeassistant.util import slugify
from homeassistant.const import ( from homeassistant.const import (
EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_STOP,
ATTR_ENTITY_ID, TEMP_CELSIUS, ATTR_ENTITY_ID, TEMP_CELSIUS,
CONF_DEVICE_CLASS, CONF_COMMAND_ON, CONF_COMMAND_OFF CONF_DEVICE_CLASS, CONF_COMMAND_ON, CONF_COMMAND_OFF
) )
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
REQUIREMENTS = ['pyRFXtrx==0.19.0'] REQUIREMENTS = ['pyRFXtrx==0.20.0']
DOMAIN = 'rfxtrx' DOMAIN = 'rfxtrx'
@ -54,7 +56,7 @@ DATA_TYPES = OrderedDict([
RECEIVED_EVT_SUBSCRIBERS = [] RECEIVED_EVT_SUBSCRIBERS = []
RFX_DEVICES = {} RFX_DEVICES = {}
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
RFXOBJECT = None RFXOBJECT = 'rfxobject'
def _valid_device(value, device_type): def _valid_device(value, device_type):
@ -167,24 +169,24 @@ def setup(hass, config):
# Try to load the RFXtrx module. # Try to load the RFXtrx module.
import RFXtrx as rfxtrxmod import RFXtrx as rfxtrxmod
# Init the rfxtrx module.
global RFXOBJECT
device = config[DOMAIN][ATTR_DEVICE] device = config[DOMAIN][ATTR_DEVICE]
debug = config[DOMAIN][ATTR_DEBUG] debug = config[DOMAIN][ATTR_DEBUG]
dummy_connection = config[DOMAIN][ATTR_DUMMY] dummy_connection = config[DOMAIN][ATTR_DUMMY]
if dummy_connection: if dummy_connection:
RFXOBJECT =\ hass.data[RFXOBJECT] =\
rfxtrxmod.Connect(device, handle_receive, debug=debug, rfxtrxmod.Connect(device, None, debug=debug,
transport_protocol=rfxtrxmod.DummyTransport2) transport_protocol=rfxtrxmod.DummyTransport2)
else: else:
RFXOBJECT = rfxtrxmod.Connect(device, handle_receive, debug=debug) hass.data[RFXOBJECT] = rfxtrxmod.Connect(device, None, debug=debug)
def _start_rfxtrx(event):
hass.data[RFXOBJECT].event_callback = handle_receive
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, _start_rfxtrx)
def _shutdown_rfxtrx(event): def _shutdown_rfxtrx(event):
"""Close connection with RFXtrx.""" """Close connection with RFXtrx."""
RFXOBJECT.close_connection() hass.data[RFXOBJECT].close_connection()
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown_rfxtrx) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown_rfxtrx)
return True return True
@ -281,7 +283,7 @@ def find_possible_pt2262_device(device_id):
return None return None
def get_devices_from_config(config, device, hass): def get_devices_from_config(config, device):
"""Read rfxtrx configuration.""" """Read rfxtrx configuration."""
signal_repetitions = config[CONF_SIGNAL_REPETITIONS] signal_repetitions = config[CONF_SIGNAL_REPETITIONS]
@ -302,13 +304,12 @@ def get_devices_from_config(config, device, hass):
new_device = device(entity_info[ATTR_NAME], event, datas, new_device = device(entity_info[ATTR_NAME], event, datas,
signal_repetitions) signal_repetitions)
new_device.hass = hass
RFX_DEVICES[device_id] = new_device RFX_DEVICES[device_id] = new_device
devices.append(new_device) devices.append(new_device)
return devices return devices
def get_new_device(event, config, device, hass): def get_new_device(event, config, device):
"""Add entity if not exist and the automatic_add is True.""" """Add entity if not exist and the automatic_add is True."""
device_id = slugify(event.device.id_string.lower()) device_id = slugify(event.device.id_string.lower())
if device_id in RFX_DEVICES: if device_id in RFX_DEVICES:
@ -329,7 +330,6 @@ def get_new_device(event, config, device, hass):
signal_repetitions = config[CONF_SIGNAL_REPETITIONS] signal_repetitions = config[CONF_SIGNAL_REPETITIONS]
new_device = device(pkt_id, event, datas, new_device = device(pkt_id, event, datas,
signal_repetitions) signal_repetitions)
new_device.hass = hass
RFX_DEVICES[device_id] = new_device RFX_DEVICES[device_id] = new_device
return new_device return new_device
@ -437,31 +437,36 @@ class RfxtrxDevice(Entity):
if command == "turn_on": if command == "turn_on":
for _ in range(self.signal_repetitions): for _ in range(self.signal_repetitions):
self._event.device.send_on(RFXOBJECT.transport) self._event.device.send_on(self.hass.data[RFXOBJECT]
.transport)
self._state = True self._state = True
elif command == "dim": elif command == "dim":
for _ in range(self.signal_repetitions): for _ in range(self.signal_repetitions):
self._event.device.send_dim(RFXOBJECT.transport, self._event.device.send_dim(self.hass.data[RFXOBJECT]
brightness) .transport, brightness)
self._state = True self._state = True
elif command == 'turn_off': elif command == 'turn_off':
for _ in range(self.signal_repetitions): for _ in range(self.signal_repetitions):
self._event.device.send_off(RFXOBJECT.transport) self._event.device.send_off(self.hass.data[RFXOBJECT]
.transport)
self._state = False self._state = False
self._brightness = 0 self._brightness = 0
elif command == "roll_up": elif command == "roll_up":
for _ in range(self.signal_repetitions): for _ in range(self.signal_repetitions):
self._event.device.send_open(RFXOBJECT.transport) self._event.device.send_open(self.hass.data[RFXOBJECT]
.transport)
elif command == "roll_down": elif command == "roll_down":
for _ in range(self.signal_repetitions): for _ in range(self.signal_repetitions):
self._event.device.send_close(RFXOBJECT.transport) self._event.device.send_close(self.hass.data[RFXOBJECT]
.transport)
elif command == "stop_roll": elif command == "stop_roll":
for _ in range(self.signal_repetitions): for _ in range(self.signal_repetitions):
self._event.device.send_stop(RFXOBJECT.transport) self._event.device.send_stop(self.hass.data[RFXOBJECT]
.transport)
self.schedule_update_ha_state() self.schedule_update_ha_state()

View File

@ -21,7 +21,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
import RFXtrx as rfxtrxmod import RFXtrx as rfxtrxmod
# Add switch from config file # Add switch from config file
switches = rfxtrx.get_devices_from_config(config, RfxtrxSwitch, hass) switches = rfxtrx.get_devices_from_config(config, RfxtrxSwitch)
add_devices_callback(switches) add_devices_callback(switches)
def switch_update(event): def switch_update(event):
@ -31,7 +31,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
event.device.known_to_be_rollershutter: event.device.known_to_be_rollershutter:
return return
new_device = rfxtrx.get_new_device(event, config, RfxtrxSwitch, hass) new_device = rfxtrx.get_new_device(event, config, RfxtrxSwitch)
if new_device: if new_device:
add_devices_callback([new_device]) add_devices_callback([new_device])

View File

@ -533,7 +533,7 @@ pyCEC==0.4.13
pyHS100==0.2.4.2 pyHS100==0.2.4.2
# homeassistant.components.rfxtrx # homeassistant.components.rfxtrx
pyRFXtrx==0.19.0 pyRFXtrx==0.20.0
# homeassistant.components.switch.dlink # homeassistant.components.switch.dlink
pyW215==0.5.1 pyW215==0.5.1