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

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