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
13 changed files with 228 additions and 71 deletions

View File

@@ -10,15 +10,18 @@ from homeassistant.components.sensor import (
DEVICE_CLASS_TEMPERATURE,
)
from homeassistant.const import CONF_DEVICES
from homeassistant.core import callback
from homeassistant.helpers.entity import Entity
from . import (
CONF_AUTOMATIC_ADD,
DATA_TYPES,
DOMAIN,
SIGNAL_EVENT,
get_device_id,
get_rfx_object,
)
from .const import DATA_RFXTRX_CONFIG
_LOGGER = logging.getLogger(__name__)
@@ -51,11 +54,11 @@ CONVERT_FUNCTIONS = {
}
def setup_platform(hass, config, add_entities, 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 platform."""
discovery_info = hass.data[DATA_RFXTRX_CONFIG]
data_ids = set()
def supported(event):
@@ -80,8 +83,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
entity = RfxtrxSensor(event.device, device_id, data_type)
entities.append(entity)
add_entities(entities)
async_add_entities(entities)
@callback
def sensor_update(event, device_id):
"""Handle sensor updates from the RFXtrx gateway."""
if not supported(event):
@@ -102,11 +106,11 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
)
entity = RfxtrxSensor(event.device, device_id, data_type, event=event)
add_entities([entity])
async_add_entities([entity])
# Subscribe to main RFXtrx events
if discovery_info[CONF_AUTOMATIC_ADD]:
hass.helpers.dispatcher.dispatcher_connect(SIGNAL_EVENT, sensor_update)
hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_EVENT, sensor_update)
class RfxtrxSensor(Entity):
@@ -177,10 +181,20 @@ class RfxtrxSensor(Entity):
"""Return unique identifier of remote device."""
return self._unique_id
@property
def device_info(self):
"""Return the device info."""
return {
"identifiers": {(DOMAIN, *self._device_id)},
"name": f"{self._device.type_string} {self._device.id_string}",
"model": self._device.type_string,
}
def _apply_event(self, event):
"""Apply command from rfxtrx."""
self.event = event
@callback
def _handle_event(self, event, device_id):
"""Check if event applies to me and update."""
if not isinstance(event, SensorEvent):
@@ -201,4 +215,4 @@ class RfxtrxSensor(Entity):
self._apply_event(event)
self.schedule_update_ha_state()
self.async_write_ha_state()