Switch rfxtrx to config entries (#37794)

* Switch to config flow setup

* Add minimal test for config flow

* Add myself as codeowner and address some review concerns

* Address some further review comments
This commit is contained in:
Joakim Plate 2020-07-13 23:24:28 +02:00 committed by GitHub
parent ed3f25489e
commit 1a64108eea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 228 additions and 71 deletions

View file

@ -5,6 +5,7 @@ import RFXtrx as rfxtrxmod
from homeassistant.components.switch import SwitchEntity
from homeassistant.const import CONF_DEVICES, STATE_ON
from homeassistant.core import callback
from homeassistant.helpers.restore_state import RestoreEntity
from . import (
@ -17,18 +18,18 @@ from . import (
get_device_id,
get_rfx_object,
)
from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST
from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST, DATA_RFXTRX_CONFIG
DATA_SWITCH = f"{DOMAIN}_switch"
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities_callback, discovery_info=None):
"""Set up the RFXtrx platform."""
if discovery_info is None:
return
async def async_setup_entry(
hass, config_entry, async_add_entities,
):
"""Set up config entry."""
discovery_info = hass.data[DATA_RFXTRX_CONFIG]
device_ids = set()
def supported(event):
@ -58,8 +59,9 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None):
)
entities.append(entity)
add_entities_callback(entities)
async_add_entities(entities)
@callback
def switch_update(event, device_id):
"""Handle sensor updates from the RFXtrx gateway."""
if not supported(event):
@ -80,11 +82,11 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None):
entity = RfxtrxSwitch(
event.device, device_id, DEFAULT_SIGNAL_REPETITIONS, event=event
)
add_entities_callback([entity])
async_add_entities([entity])
# Subscribe to main RFXtrx events
if discovery_info[CONF_AUTOMATIC_ADD]:
hass.helpers.dispatcher.dispatcher_connect(SIGNAL_EVENT, switch_update)
hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_EVENT, switch_update)
class RfxtrxSwitch(RfxtrxDevice, SwitchEntity, RestoreEntity):
@ -111,6 +113,7 @@ class RfxtrxSwitch(RfxtrxDevice, SwitchEntity, RestoreEntity):
elif event.values["Command"] in COMMAND_OFF_LIST:
self._state = False
@callback
def _handle_event(self, event, device_id):
"""Check if event applies to me and update."""
if device_id != self._device_id:
@ -118,7 +121,7 @@ class RfxtrxSwitch(RfxtrxDevice, SwitchEntity, RestoreEntity):
self._apply_event(event)
self.schedule_update_ha_state()
self.async_write_ha_state()
def turn_on(self, **kwargs):
"""Turn the device on."""