From a8cbb949fa83410f3bda959a8bbba846a6d8a037 Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Thu, 9 Sep 2021 07:17:02 +0200 Subject: [PATCH] Rfxtrx drop yaml configuration (#54173) --- homeassistant/components/rfxtrx/__init__.py | 81 +---------- .../components/rfxtrx/binary_sensor.py | 20 ++- .../components/rfxtrx/config_flow.py | 24 --- homeassistant/components/rfxtrx/const.py | 1 - homeassistant/components/rfxtrx/cover.py | 4 +- homeassistant/components/rfxtrx/light.py | 11 +- homeassistant/components/rfxtrx/switch.py | 11 +- tests/components/rfxtrx/test_config_flow.py | 137 ------------------ tests/components/rfxtrx/test_init.py | 47 ------ 9 files changed, 26 insertions(+), 310 deletions(-) diff --git a/homeassistant/components/rfxtrx/__init__.py b/homeassistant/components/rfxtrx/__init__.py index 34b7c01600a..970aed38335 100644 --- a/homeassistant/components/rfxtrx/__init__.py +++ b/homeassistant/components/rfxtrx/__init__.py @@ -10,13 +10,9 @@ import async_timeout import voluptuous as vol from homeassistant import config_entries -from homeassistant.components.binary_sensor import DEVICE_CLASSES_SCHEMA from homeassistant.const import ( ATTR_DEVICE_ID, - CONF_COMMAND_OFF, - CONF_COMMAND_ON, CONF_DEVICE, - CONF_DEVICE_CLASS, CONF_DEVICE_ID, CONF_DEVICES, CONF_HOST, @@ -33,11 +29,8 @@ from .const import ( COMMAND_GROUP_LIST, CONF_AUTOMATIC_ADD, CONF_DATA_BITS, - CONF_DEBUG, CONF_FIRE_EVENT, - CONF_OFF_DELAY, CONF_REMOVE_DEVICE, - CONF_SIGNAL_REPETITIONS, DATA_CLEANUP_CALLBACKS, DATA_LISTENER, DATA_RFXOBJECT, @@ -65,83 +58,11 @@ def _bytearray_string(data): ) from err -def _ensure_device(value): - if value is None: - return DEVICE_DATA_SCHEMA({}) - return DEVICE_DATA_SCHEMA(value) - - SERVICE_SEND_SCHEMA = vol.Schema({ATTR_EVENT: _bytearray_string}) -DEVICE_DATA_SCHEMA = vol.Schema( - { - vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA, - vol.Optional(CONF_FIRE_EVENT, default=False): cv.boolean, - vol.Optional(CONF_OFF_DELAY): vol.All( - cv.time_period, cv.positive_timedelta, lambda value: value.total_seconds() - ), - vol.Optional(CONF_DATA_BITS): cv.positive_int, - vol.Optional(CONF_COMMAND_ON): cv.byte, - vol.Optional(CONF_COMMAND_OFF): cv.byte, - vol.Optional(CONF_SIGNAL_REPETITIONS, default=1): cv.positive_int, - } -) - -BASE_SCHEMA = vol.Schema( - { - vol.Optional(CONF_DEBUG): cv.boolean, - vol.Optional(CONF_AUTOMATIC_ADD, default=False): cv.boolean, - vol.Optional(CONF_DEVICES, default={}): {cv.string: _ensure_device}, - }, -) - -DEVICE_SCHEMA = BASE_SCHEMA.extend({vol.Required(CONF_DEVICE): cv.string}) - -PORT_SCHEMA = BASE_SCHEMA.extend( - {vol.Required(CONF_PORT): cv.port, vol.Optional(CONF_HOST): cv.string} -) - -CONFIG_SCHEMA = vol.Schema( - {DOMAIN: vol.All(cv.deprecated(CONF_DEBUG), vol.Any(DEVICE_SCHEMA, PORT_SCHEMA))}, - extra=vol.ALLOW_EXTRA, -) - PLATFORMS = ["switch", "sensor", "light", "binary_sensor", "cover"] -async def async_setup(hass, config): - """Set up the RFXtrx component.""" - if DOMAIN not in config: - return True - - data = { - CONF_HOST: config[DOMAIN].get(CONF_HOST), - CONF_PORT: config[DOMAIN].get(CONF_PORT), - CONF_DEVICE: config[DOMAIN].get(CONF_DEVICE), - CONF_AUTOMATIC_ADD: config[DOMAIN].get(CONF_AUTOMATIC_ADD), - CONF_DEVICES: config[DOMAIN][CONF_DEVICES], - } - - # Read device_id from the event code add to the data that will end up in the ConfigEntry - for event_code, event_config in data[CONF_DEVICES].items(): - event = get_rfx_object(event_code) - if event is None: - continue - device_id = get_device_id( - event.device, data_bits=event_config.get(CONF_DATA_BITS) - ) - event_config[CONF_DEVICE_ID] = device_id - - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data=data, - ) - ) - return True - - async def async_setup_entry(hass, entry: config_entries.ConfigEntry): """Set up the RFXtrx component.""" hass.data.setdefault(DOMAIN, {}) @@ -272,7 +193,7 @@ async def async_setup_internal(hass, entry: config_entries.ConfigEntry): @callback def _add_device(event, device_id): """Add a device to config entry.""" - config = DEVICE_DATA_SCHEMA({}) + config = {} config[CONF_DEVICE_ID] = device_id data = entry.data.copy() diff --git a/homeassistant/components/rfxtrx/binary_sensor.py b/homeassistant/components/rfxtrx/binary_sensor.py index f6751d760b2..788c5dec436 100644 --- a/homeassistant/components/rfxtrx/binary_sensor.py +++ b/homeassistant/components/rfxtrx/binary_sensor.py @@ -1,7 +1,6 @@ """Support for RFXtrx binary sensors.""" from __future__ import annotations -from dataclasses import replace import logging import RFXtrx as rfxtrxmod @@ -15,7 +14,6 @@ from homeassistant.components.binary_sensor import ( from homeassistant.const import ( CONF_COMMAND_OFF, CONF_COMMAND_ON, - CONF_DEVICE_CLASS, CONF_DEVICES, STATE_ON, ) @@ -23,8 +21,6 @@ from homeassistant.core import callback from homeassistant.helpers import event as evt from . import ( - CONF_DATA_BITS, - CONF_OFF_DELAY, RfxtrxEntity, connect_auto_add, find_possible_pt2262_device, @@ -32,7 +28,13 @@ from . import ( get_pt2262_cmd, get_rfx_object, ) -from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST, DEVICE_PACKET_TYPE_LIGHTING4 +from .const import ( + COMMAND_OFF_LIST, + COMMAND_ON_LIST, + CONF_DATA_BITS, + CONF_OFF_DELAY, + DEVICE_PACKET_TYPE_LIGHTING4, +) _LOGGER = logging.getLogger(__name__) @@ -106,12 +108,10 @@ async def async_setup_entry( discovery_info = config_entry.data - def get_sensor_description(type_string: str, device_class: str | None = None): + def get_sensor_description(type_string: str): description = SENSOR_TYPES_DICT.get(type_string) if description is None: description = BinarySensorEntityDescription(key=type_string) - if device_class: - description = replace(description, device_class=device_class) return description for packet_id, entity_info in discovery_info[CONF_DEVICES].items(): @@ -136,9 +136,7 @@ async def async_setup_entry( device = RfxtrxBinarySensor( event.device, device_id, - get_sensor_description( - event.device.type_string, entity_info.get(CONF_DEVICE_CLASS) - ), + get_sensor_description(event.device.type_string), entity_info.get(CONF_OFF_DELAY), entity_info.get(CONF_DATA_BITS), entity_info.get(CONF_COMMAND_ON), diff --git a/homeassistant/components/rfxtrx/config_flow.py b/homeassistant/components/rfxtrx/config_flow.py index 91afd9da999..01bcc6ea035 100644 --- a/homeassistant/components/rfxtrx/config_flow.py +++ b/homeassistant/components/rfxtrx/config_flow.py @@ -547,30 +547,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): errors=errors, ) - async def async_step_import(self, import_config=None): - """Handle the initial step.""" - entry = await self.async_set_unique_id(DOMAIN) - if entry: - if CONF_DEVICES not in entry.data: - # In version 0.113, devices key was not written to config entry. Update the entry with import data - self._abort_if_unique_id_configured(import_config) - else: - self._abort_if_unique_id_configured() - - host = import_config[CONF_HOST] - port = import_config[CONF_PORT] - device = import_config[CONF_DEVICE] - - try: - if host is not None: - await self.async_validate_rfx(host=host, port=port) - else: - await self.async_validate_rfx(device=device) - except CannotConnect: - return self.async_abort(reason="cannot_connect") - - return self.async_create_entry(title="RFXTRX", data=import_config) - async def async_validate_rfx(self, host=None, port=None, device=None): """Create data for rfxtrx entry.""" success = await self.hass.async_add_executor_job( diff --git a/homeassistant/components/rfxtrx/const.py b/homeassistant/components/rfxtrx/const.py index d457435f85c..17f54ef24c9 100644 --- a/homeassistant/components/rfxtrx/const.py +++ b/homeassistant/components/rfxtrx/const.py @@ -4,7 +4,6 @@ CONF_FIRE_EVENT = "fire_event" CONF_DATA_BITS = "data_bits" CONF_AUTOMATIC_ADD = "automatic_add" CONF_SIGNAL_REPETITIONS = "signal_repetitions" -CONF_DEBUG = "debug" CONF_OFF_DELAY = "off_delay" CONF_VENETIAN_BLIND_MODE = "venetian_blind_mode" diff --git a/homeassistant/components/rfxtrx/cover.py b/homeassistant/components/rfxtrx/cover.py index a5f5edd0e42..26a938141a2 100644 --- a/homeassistant/components/rfxtrx/cover.py +++ b/homeassistant/components/rfxtrx/cover.py @@ -14,8 +14,6 @@ from homeassistant.const import CONF_DEVICES, STATE_OPEN from homeassistant.core import callback from . import ( - CONF_DATA_BITS, - CONF_SIGNAL_REPETITIONS, DEFAULT_SIGNAL_REPETITIONS, RfxtrxCommandEntity, connect_auto_add, @@ -25,6 +23,8 @@ from . import ( from .const import ( COMMAND_OFF_LIST, COMMAND_ON_LIST, + CONF_DATA_BITS, + CONF_SIGNAL_REPETITIONS, CONF_VENETIAN_BLIND_MODE, CONST_VENETIAN_BLIND_MODE_EU, CONST_VENETIAN_BLIND_MODE_US, diff --git a/homeassistant/components/rfxtrx/light.py b/homeassistant/components/rfxtrx/light.py index fd790581eda..ea197b5ebc4 100644 --- a/homeassistant/components/rfxtrx/light.py +++ b/homeassistant/components/rfxtrx/light.py @@ -12,15 +12,18 @@ from homeassistant.const import CONF_DEVICES, STATE_ON from homeassistant.core import callback from . import ( - CONF_DATA_BITS, - CONF_SIGNAL_REPETITIONS, DEFAULT_SIGNAL_REPETITIONS, RfxtrxCommandEntity, connect_auto_add, get_device_id, get_rfx_object, ) -from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST +from .const import ( + COMMAND_OFF_LIST, + COMMAND_ON_LIST, + CONF_DATA_BITS, + CONF_SIGNAL_REPETITIONS, +) _LOGGER = logging.getLogger(__name__) @@ -62,7 +65,7 @@ async def async_setup_entry( device_ids.add(device_id) entity = RfxtrxLight( - event.device, device_id, entity_info[CONF_SIGNAL_REPETITIONS] + event.device, device_id, entity_info.get(CONF_SIGNAL_REPETITIONS, 1) ) entities.append(entity) diff --git a/homeassistant/components/rfxtrx/switch.py b/homeassistant/components/rfxtrx/switch.py index 60ddb9a4d16..2a09d027345 100644 --- a/homeassistant/components/rfxtrx/switch.py +++ b/homeassistant/components/rfxtrx/switch.py @@ -8,8 +8,6 @@ from homeassistant.const import CONF_DEVICES, STATE_ON from homeassistant.core import callback from . import ( - CONF_DATA_BITS, - CONF_SIGNAL_REPETITIONS, DEFAULT_SIGNAL_REPETITIONS, DOMAIN, RfxtrxCommandEntity, @@ -17,7 +15,12 @@ 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, + CONF_DATA_BITS, + CONF_SIGNAL_REPETITIONS, +) DATA_SWITCH = f"{DOMAIN}_switch" @@ -61,7 +64,7 @@ async def async_setup_entry( device_ids.add(device_id) entity = RfxtrxSwitch( - event.device, device_id, entity_info[CONF_SIGNAL_REPETITIONS] + event.device, device_id, entity_info.get(CONF_SIGNAL_REPETITIONS, 1) ) entities.append(entity) diff --git a/tests/components/rfxtrx/test_config_flow.py b/tests/components/rfxtrx/test_config_flow.py index 1c16507f960..2b55db0b889 100644 --- a/tests/components/rfxtrx/test_config_flow.py +++ b/tests/components/rfxtrx/test_config_flow.py @@ -277,143 +277,6 @@ async def test_setup_serial_manual_fail(com_mock, hass): assert result["errors"] == {"base": "cannot_connect"} -@patch( - "homeassistant.components.rfxtrx.rfxtrxmod.PySerialTransport.connect", - serial_connect, -) -@patch( - "homeassistant.components.rfxtrx.rfxtrxmod.PySerialTransport.close", - return_value=None, -) -async def test_import_serial(connect_mock, hass): - """Test we can import.""" - await setup.async_setup_component(hass, "persistent_notification", {}) - - with patch("homeassistant.components.rfxtrx.async_setup_entry", return_value=True): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={"host": None, "port": None, "device": "/dev/tty123", "debug": False}, - ) - - assert result["type"] == "create_entry" - assert result["title"] == "RFXTRX" - assert result["data"] == { - "host": None, - "port": None, - "device": "/dev/tty123", - "debug": False, - } - - -@patch( - "homeassistant.components.rfxtrx.rfxtrxmod.PyNetworkTransport.connect", - return_value=None, -) -async def test_import_network(connect_mock, hass): - """Test we can import.""" - await setup.async_setup_component(hass, "persistent_notification", {}) - - with patch("homeassistant.components.rfxtrx.async_setup_entry", return_value=True): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={"host": "localhost", "port": 1234, "device": None, "debug": False}, - ) - - assert result["type"] == "create_entry" - assert result["title"] == "RFXTRX" - assert result["data"] == { - "host": "localhost", - "port": 1234, - "device": None, - "debug": False, - } - - -@patch( - "homeassistant.components.rfxtrx.rfxtrxmod.PyNetworkTransport.connect", - side_effect=OSError, -) -async def test_import_network_connection_fail(connect_mock, hass): - """Test we can import.""" - await setup.async_setup_component(hass, "persistent_notification", {}) - - with patch("homeassistant.components.rfxtrx.async_setup_entry", return_value=True): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={"host": "localhost", "port": 1234, "device": None, "debug": False}, - ) - - assert result["type"] == "abort" - assert result["reason"] == "cannot_connect" - - -async def test_import_update(hass): - """Test we can import.""" - await setup.async_setup_component(hass, "persistent_notification", {}) - - entry = MockConfigEntry( - domain=DOMAIN, - data={ - "host": None, - "port": None, - "device": "/dev/tty123", - "debug": False, - "devices": {}, - }, - unique_id=DOMAIN, - ) - entry.add_to_hass(hass) - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={ - "host": None, - "port": None, - "device": "/dev/tty123", - "debug": True, - "devices": {}, - }, - ) - - assert result["type"] == "abort" - assert result["reason"] == "already_configured" - - -async def test_import_migrate(hass): - """Test we can import.""" - await setup.async_setup_component(hass, "persistent_notification", {}) - - entry = MockConfigEntry( - domain=DOMAIN, - data={"host": None, "port": None, "device": "/dev/tty123", "debug": False}, - unique_id=DOMAIN, - ) - entry.add_to_hass(hass) - - with patch("homeassistant.components.rfxtrx.async_setup_entry", return_value=True): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={ - "host": None, - "port": None, - "device": "/dev/tty123", - "debug": True, - "automatic_add": True, - "devices": {}, - }, - ) - - assert result["type"] == "abort" - assert result["reason"] == "already_configured" - - assert entry.data["devices"] == {} - - async def test_options_global(hass): """Test if we can set global options.""" await setup.async_setup_component(hass, "persistent_notification", {}) diff --git a/tests/components/rfxtrx/test_init.py b/tests/components/rfxtrx/test_init.py index 3625c23ebb8..0c904896090 100644 --- a/tests/components/rfxtrx/test_init.py +++ b/tests/components/rfxtrx/test_init.py @@ -6,58 +6,11 @@ from homeassistant.components.rfxtrx import DOMAIN from homeassistant.components.rfxtrx.const import EVENT_RFXTRX_EVENT from homeassistant.core import callback from homeassistant.helpers import device_registry as dr -from homeassistant.setup import async_setup_component from tests.common import MockConfigEntry from tests.components.rfxtrx.conftest import create_rfx_test_cfg -async def test_valid_config(hass): - """Test configuration.""" - assert await async_setup_component( - hass, - "rfxtrx", - { - "rfxtrx": { - "device": "/dev/serial/by-id/usb" - + "-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0", - } - }, - ) - - -async def test_valid_config2(hass): - """Test configuration.""" - assert await async_setup_component( - hass, - "rfxtrx", - { - "rfxtrx": { - "device": "/dev/serial/by-id/usb" - + "-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0", - "debug": True, - } - }, - ) - - -async def test_invalid_config(hass): - """Test configuration.""" - assert not await async_setup_component(hass, "rfxtrx", {"rfxtrx": {}}) - - assert not await async_setup_component( - hass, - "rfxtrx", - { - "rfxtrx": { - "device": "/dev/serial/by-id/usb" - + "-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0", - "invalid_key": True, - } - }, - ) - - async def test_fire_event(hass, rfxtrx): """Test fire event.""" entry_data = create_rfx_test_cfg(