AlarmDecoder config flow fixes (#40037)

This commit is contained in:
AJ Schmidt 2020-09-15 18:11:29 -04:00 committed by GitHub
parent 000d2047fb
commit 66bb6a6ffa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 37 additions and 40 deletions

View File

@ -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)

View File

@ -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."""

View File

@ -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):

View File

@ -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}
)

View File

@ -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": {

View File

@ -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 @@
}
}
}
}
}

View File

@ -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