Skip to content

Commit

Permalink
AlarmDecoder config flow fixes (#40037)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajschmidt8 authored Sep 15, 2020
1 parent 000d204 commit 66bb6a6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 40 deletions.
13 changes: 6 additions & 7 deletions homeassistant/components/alarmdecoder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ def stop_alarmdecoder(event):
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
Expand Down Expand Up @@ -100,8 +100,7 @@ def handle_rel_message(sender, message):
"""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]
Expand Down Expand Up @@ -129,7 +128,7 @@ def handle_rel_message(sender, message):
DATA_RESTART: False,
}

open_connection()
await open_connection()

for component in PLATFORMS:
hass.async_create_task(
Expand All @@ -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)
Expand Down
23 changes: 6 additions & 17 deletions homeassistant/components/alarmdecoder/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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,
Expand All @@ -40,21 +39,9 @@
_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(
Expand All @@ -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."""
Expand Down
9 changes: 4 additions & 5 deletions homeassistant/components/alarmdecoder/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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):
Expand Down
10 changes: 7 additions & 3 deletions homeassistant/components/alarmdecoder/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ async def async_step_protocol(self, user_input=None):
):
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}"
Expand All @@ -100,9 +100,13 @@ async def async_step_protocol(self, user_input=None):
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}
)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/alarmdecoder/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/alarmdecoder/translations/en.json
Original file line number Diff line number Diff line change
@@ -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."
Expand Down Expand Up @@ -71,4 +71,4 @@
}
}
}
}
}
16 changes: 11 additions & 5 deletions tests/components/alarmdecoder/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@


@pytest.mark.parametrize(
"protocol,connection,baud,title",
"protocol,connection,title",
[
(
PROTOCOL_SOCKET,
{
CONF_HOST: "alarmdecoder123",
CONF_PORT: 10001,
},
None,
"alarmdecoder123:10001",
),
(
Expand All @@ -51,12 +50,11 @@
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(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 66bb6a6

Please sign in to comment.