diff --git a/homeassistant/components/alarmdecoder/__init__.py b/homeassistant/components/alarmdecoder/__init__.py index b69b60b82c4..8dd704f1333 100644 --- a/homeassistant/components/alarmdecoder/__init__.py +++ b/homeassistant/components/alarmdecoder/__init__.py @@ -59,13 +59,13 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool hass.data[DOMAIN][entry.entry_id][DATA_RESTART] = False controller.close() - def open_connection(now=None): + async def open_connection(now=None): """Open a connection to AlarmDecoder.""" try: - controller.open(baud) + await hass.async_add_executor_job(controller.open, baud) except NoDeviceError: _LOGGER.debug("Failed to connect. Retrying in 5 seconds") - hass.helpers.event.track_point_in_time( + hass.helpers.event.async_track_point_in_time( open_connection, dt_util.utcnow() + timedelta(seconds=5) ) return @@ -100,8 +100,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool """Handle relay or zone expander message from AlarmDecoder.""" hass.helpers.dispatcher.dispatcher_send(SIGNAL_REL_MESSAGE, message) - controller = False - baud = ad_connection[CONF_DEVICE_BAUD] + baud = ad_connection.get(CONF_DEVICE_BAUD) if protocol == PROTOCOL_SOCKET: host = ad_connection[CONF_HOST] port = ad_connection[CONF_PORT] @@ -129,7 +128,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool DATA_RESTART: False, } - open_connection() + await open_connection() for component in PLATFORMS: hass.async_create_task( @@ -156,7 +155,7 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry): hass.data[DOMAIN][entry.entry_id][DATA_REMOVE_UPDATE_LISTENER]() hass.data[DOMAIN][entry.entry_id][DATA_REMOVE_STOP_LISTENER]() - hass.data[DOMAIN][entry.entry_id][DATA_AD].close() + await hass.async_add_executor_job(hass.data[DOMAIN][entry.entry_id][DATA_AD].close) if hass.data[DOMAIN][entry.entry_id]: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/homeassistant/components/alarmdecoder/alarm_control_panel.py b/homeassistant/components/alarmdecoder/alarm_control_panel.py index 83288c93c67..bc2d74a5042 100644 --- a/homeassistant/components/alarmdecoder/alarm_control_panel.py +++ b/homeassistant/components/alarmdecoder/alarm_control_panel.py @@ -15,7 +15,6 @@ from homeassistant.components.alarm_control_panel.const import ( from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( ATTR_CODE, - ATTR_ENTITY_ID, STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_NIGHT, @@ -40,21 +39,9 @@ from .const import ( _LOGGER = logging.getLogger(__name__) SERVICE_ALARM_TOGGLE_CHIME = "alarm_toggle_chime" -ALARM_TOGGLE_CHIME_SCHEMA = vol.Schema( - { - vol.Required(ATTR_ENTITY_ID, default=[]): cv.entity_ids, - vol.Required(ATTR_CODE): cv.string, - } -) SERVICE_ALARM_KEYPRESS = "alarm_keypress" ATTR_KEYPRESS = "keypress" -ALARM_KEYPRESS_SCHEMA = vol.Schema( - { - vol.Required(ATTR_ENTITY_ID, default=[]): cv.entity_ids, - vol.Required(ATTR_KEYPRESS): cv.string, - } -) async def async_setup_entry( @@ -77,18 +64,20 @@ async def async_setup_entry( platform.async_register_entity_service( SERVICE_ALARM_TOGGLE_CHIME, - ALARM_TOGGLE_CHIME_SCHEMA, + { + vol.Required(ATTR_CODE): cv.string, + }, "alarm_toggle_chime", ) platform.async_register_entity_service( SERVICE_ALARM_KEYPRESS, - ALARM_KEYPRESS_SCHEMA, + { + vol.Required(ATTR_KEYPRESS): cv.string, + }, "alarm_keypress", ) - return True - class AlarmDecoderAlarmPanel(AlarmControlPanelEntity): """Representation of an AlarmDecoder-based alarm panel.""" diff --git a/homeassistant/components/alarmdecoder/binary_sensor.py b/homeassistant/components/alarmdecoder/binary_sensor.py index 417dfd6f96a..89b7d00b3a4 100644 --- a/homeassistant/components/alarmdecoder/binary_sensor.py +++ b/homeassistant/components/alarmdecoder/binary_sensor.py @@ -40,7 +40,7 @@ async def async_setup_entry( zones = entry.options.get(OPTIONS_ZONES, DEFAULT_ZONE_OPTIONS) - devices = [] + entities = [] for zone_num in zones: zone_info = zones[zone_num] zone_type = zone_info[CONF_ZONE_TYPE] @@ -49,13 +49,12 @@ async def async_setup_entry( zone_loop = zone_info.get(CONF_ZONE_LOOP) relay_addr = zone_info.get(CONF_RELAY_ADDR) relay_chan = zone_info.get(CONF_RELAY_CHAN) - device = AlarmDecoderBinarySensor( + entity = AlarmDecoderBinarySensor( zone_num, zone_name, zone_type, zone_rfid, zone_loop, relay_addr, relay_chan ) - devices.append(device) + entities.append(entity) - async_add_entities(devices) - return True + async_add_entities(entities) class AlarmDecoderBinarySensor(BinarySensorEntity): diff --git a/homeassistant/components/alarmdecoder/config_flow.py b/homeassistant/components/alarmdecoder/config_flow.py index 1f6f049fcb3..74b23f049a7 100644 --- a/homeassistant/components/alarmdecoder/config_flow.py +++ b/homeassistant/components/alarmdecoder/config_flow.py @@ -87,8 +87,8 @@ class AlarmDecoderFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): ): return self.async_abort(reason="already_configured") connection = {} + baud = None if self.protocol == PROTOCOL_SOCKET: - baud = connection[CONF_DEVICE_BAUD] = None host = connection[CONF_HOST] = user_input[CONF_HOST] port = connection[CONF_PORT] = user_input[CONF_PORT] title = f"{host}:{port}" @@ -100,9 +100,13 @@ class AlarmDecoderFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): device = SerialDevice(interface=path) controller = AdExt(device) + + def test_connection(): + controller.open(baud) + controller.close() + try: - with controller: - controller.open(baudrate=baud) + await self.hass.async_add_executor_job(test_connection) return self.async_create_entry( title=title, data={CONF_PROTOCOL: self.protocol, **connection} ) diff --git a/homeassistant/components/alarmdecoder/strings.json b/homeassistant/components/alarmdecoder/strings.json index 73e4cc760f2..ed250b92b98 100644 --- a/homeassistant/components/alarmdecoder/strings.json +++ b/homeassistant/components/alarmdecoder/strings.json @@ -22,7 +22,7 @@ }, "create_entry": { "default": "Successfully connected to AlarmDecoder." }, "abort": { - "already_configured": "AlarmDecoder device is already configured." + "already_configured": "[%key:common::config_flow::abort::already_configured_device%]" } }, "options": { diff --git a/homeassistant/components/alarmdecoder/translations/en.json b/homeassistant/components/alarmdecoder/translations/en.json index 756e1f1479e..55a4ad99d1c 100644 --- a/homeassistant/components/alarmdecoder/translations/en.json +++ b/homeassistant/components/alarmdecoder/translations/en.json @@ -1,7 +1,7 @@ { "config": { "abort": { - "already_configured": "AlarmDecoder device is already configured." + "already_configured": "Device is already configured" }, "create_entry": { "default": "Successfully connected to AlarmDecoder." @@ -71,4 +71,4 @@ } } } -} \ No newline at end of file +} diff --git a/tests/components/alarmdecoder/test_config_flow.py b/tests/components/alarmdecoder/test_config_flow.py index dd7091fd0ef..64f4a604ff3 100644 --- a/tests/components/alarmdecoder/test_config_flow.py +++ b/tests/components/alarmdecoder/test_config_flow.py @@ -34,7 +34,7 @@ from tests.common import MockConfigEntry @pytest.mark.parametrize( - "protocol,connection,baud,title", + "protocol,connection,title", [ ( PROTOCOL_SOCKET, @@ -42,7 +42,6 @@ from tests.common import MockConfigEntry CONF_HOST: "alarmdecoder123", CONF_PORT: 10001, }, - None, "alarmdecoder123:10001", ), ( @@ -51,12 +50,11 @@ from tests.common import MockConfigEntry CONF_DEVICE_PATH: "/dev/ttyUSB123", CONF_DEVICE_BAUD: 115000, }, - 115000, "/dev/ttyUSB123", ), ], ) -async def test_setups(hass: HomeAssistant, protocol, connection, baud, title): +async def test_setups(hass: HomeAssistant, protocol, connection, title): """Test flow for setting up the available AlarmDecoder protocols.""" result = await hass.config_entries.flow.async_init( @@ -90,7 +88,6 @@ async def test_setups(hass: HomeAssistant, protocol, connection, baud, title): assert result["data"] == { **connection, CONF_PROTOCOL: protocol, - CONF_DEVICE_BAUD: baud, } await hass.async_block_till_done() @@ -142,6 +139,9 @@ async def test_options_arm_flow(hass: HomeAssistant): entry = MockConfigEntry(domain=DOMAIN) entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + result = await hass.config_entries.options.async_init(entry.entry_id) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM @@ -177,6 +177,9 @@ async def test_options_zone_flow(hass: HomeAssistant): entry = MockConfigEntry(domain=DOMAIN) entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + result = await hass.config_entries.options.async_init(entry.entry_id) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM @@ -250,6 +253,9 @@ async def test_options_zone_flow_validation(hass: HomeAssistant): entry = MockConfigEntry(domain=DOMAIN) entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + result = await hass.config_entries.options.async_init(entry.entry_id) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM