From 1400422f84a187613fb259d23d07ee0132b95f58 Mon Sep 17 00:00:00 2001 From: lawtancool <26829131+lawtancool@users.noreply.github.com> Date: Sat, 15 Jan 2022 20:13:08 -0800 Subject: [PATCH] Add drop down for alarm config flow --- README.md | 2 +- custom_components/control4/__init__.py | 9 ++++++ .../control4/alarm_control_panel.py | 28 ++++++++++++++----- custom_components/control4/config_flow.py | 17 +++++++---- custom_components/control4/const.py | 11 ++++---- custom_components/control4/manifest.json | 2 +- 6 files changed, 49 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index fad39bc..18bc2c5 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Add this repository as a custom repository in HACS to install the integration. O If you are using an alarm control panel, you must go to Home Assistant -> Configuration -> Devices and Services -> Integrations and click "Configure" on the Control4 entry. -In the dialog that appears, type the English names of the arming modes that your alarm system is capable of into the corresponding fields. For example, a DSC alarm system uses "Stay" as the "Alarm arm home mode name", and "Away" as the "Alarm arm away mode name". If your alarm system does not use one of the mode names, just leave the field blank. Once you click submit on the dialog, Home Assistant will be able to arm your alarm control panel and detect its state. +In the dialog that appears, choose the Control4 alarm arming modes that you want to correspond to each Home Assistant arming mode. For example, a DSC alarm system uses "Stay" as the "Alarm arm home mode name", and "Away" as the "Alarm arm away mode name". If your alarm system does not use one of the mode names, select `(not set)`. Once you click submit on the dialog, Home Assistant will be able to arm your alarm control panel and detect its state. ## Disclaimer diff --git a/custom_components/control4/__init__.py b/custom_components/control4/__init__.py index ccd0295..3531601 100644 --- a/custom_components/control4/__init__.py +++ b/custom_components/control4/__init__.py @@ -26,6 +26,7 @@ from .const import ( CONF_ACCOUNT, + CONF_ALARM_ARM_STATES, CONF_ALARM_AWAY_MODE, CONF_ALARM_CUSTOM_BYPASS_MODE, CONF_ALARM_HOME_MODE, @@ -105,6 +106,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: CONF_ALARM_VACATION_MODE, DEFAULT_ALARM_VACATION_MODE ) + entry_data[CONF_ALARM_ARM_STATES] = { + DEFAULT_ALARM_AWAY_MODE, + DEFAULT_ALARM_HOME_MODE, + DEFAULT_ALARM_NIGHT_MODE, + DEFAULT_ALARM_CUSTOM_BYPASS_MODE, + DEFAULT_ALARM_VACATION_MODE, + } + entry_data[CONF_CONFIG_LISTENER] = entry.add_update_listener(update_listener) hass.config_entries.async_setup_platforms(entry, PLATFORMS) diff --git a/custom_components/control4/alarm_control_panel.py b/custom_components/control4/alarm_control_panel.py index aa8f22e..0cb64e4 100644 --- a/custom_components/control4/alarm_control_panel.py +++ b/custom_components/control4/alarm_control_panel.py @@ -38,6 +38,7 @@ from . import Control4Entity, get_items_of_category from .const import ( + CONF_ALARM_ARM_STATES, CONF_ALARM_AWAY_MODE, CONF_ALARM_CUSTOM_BYPASS_MODE, CONF_ALARM_HOME_MODE, @@ -45,6 +46,11 @@ CONF_ALARM_VACATION_MODE, CONF_DIRECTOR, CONTROL4_ENTITY_TYPE, + DEFAULT_ALARM_AWAY_MODE, + DEFAULT_ALARM_CUSTOM_BYPASS_MODE, + DEFAULT_ALARM_HOME_MODE, + DEFAULT_ALARM_NIGHT_MODE, + DEFAULT_ALARM_VACATION_MODE, DOMAIN, ) from .director_utils import director_get_entry_variables @@ -203,6 +209,10 @@ async def async_setup_entry( for item in items_of_category: try: if item["type"] == CONTROL4_ENTITY_TYPE and item["id"]: + if "capabilities" in item and "arm_states" in item["capabilities"]: + entry_data[CONF_ALARM_ARM_STATES].update( + item["capabilities"]["arm_states"].split(",") + ) item_name = str(item["name"]) item_id = item["id"] item_area = item["roomName"] @@ -223,9 +233,10 @@ async def async_setup_entry( item_model = parent_item["model"] else: continue - except KeyError: + except KeyError as exception: _LOGGER.warning( - "Unknown device properties received from Control4: %s", + "Unknown device properties received from Control4: %s %s", + exception, item, ) continue @@ -340,15 +351,18 @@ def code_format(self): def supported_features(self) -> int: """Flag supported features.""" flags = 0 - if not self.entry_data[CONF_ALARM_AWAY_MODE] == "": + if not self.entry_data[CONF_ALARM_AWAY_MODE] == DEFAULT_ALARM_AWAY_MODE: flags |= SUPPORT_ALARM_ARM_AWAY - if not self.entry_data[CONF_ALARM_HOME_MODE] == "": + if not self.entry_data[CONF_ALARM_HOME_MODE] == DEFAULT_ALARM_HOME_MODE: flags |= SUPPORT_ALARM_ARM_HOME - if not self.entry_data[CONF_ALARM_NIGHT_MODE] == "": + if not self.entry_data[CONF_ALARM_NIGHT_MODE] == DEFAULT_ALARM_NIGHT_MODE: flags |= SUPPORT_ALARM_ARM_NIGHT - if not self.entry_data[CONF_ALARM_CUSTOM_BYPASS_MODE] == "": + if ( + not self.entry_data[CONF_ALARM_CUSTOM_BYPASS_MODE] + == DEFAULT_ALARM_CUSTOM_BYPASS_MODE + ): flags |= SUPPORT_ALARM_ARM_CUSTOM_BYPASS - if not self.entry_data[CONF_ALARM_VACATION_MODE] == "": + if not self.entry_data[CONF_ALARM_VACATION_MODE] == DEFAULT_ALARM_VACATION_MODE: flags |= SUPPORT_ALARM_ARM_VACATION return flags diff --git a/custom_components/control4/config_flow.py b/custom_components/control4/config_flow.py index e315843..ac804ba 100644 --- a/custom_components/control4/config_flow.py +++ b/custom_components/control4/config_flow.py @@ -15,6 +15,7 @@ from homeassistant.helpers.device_registry import format_mac from .const import ( + CONF_ALARM_ARM_STATES, CONF_ALARM_AWAY_MODE, CONF_ALARM_CUSTOM_BYPASS_MODE, CONF_ALARM_HOME_MODE, @@ -186,10 +187,13 @@ def __init__(self, config_entry: config_entries.ConfigEntry) -> None: async def async_step_init(self, user_input=None): """Handle options flow.""" if user_input is not None: + _LOGGER.debug(user_input) return self.async_create_entry(title="", data=user_input) # TODO: figure out how to accept empty strings to disable modes # TODO: figure out how to only show alarm options if a alarm_control_panel entity exists + self.entry_data = self.hass.data[DOMAIN][self.config_entry.entry_id] + _LOGGER.debug(self.entry_data[CONF_ALARM_ARM_STATES]) data_schema = vol.Schema( { vol.Optional( @@ -197,32 +201,33 @@ async def async_step_init(self, user_input=None): default=self.config_entry.options.get( CONF_ALARM_AWAY_MODE, DEFAULT_ALARM_AWAY_MODE ), - ): str, + ): vol.In(self.entry_data[CONF_ALARM_ARM_STATES]), vol.Optional( CONF_ALARM_HOME_MODE, default=self.config_entry.options.get( CONF_ALARM_HOME_MODE, DEFAULT_ALARM_HOME_MODE ), - ): str, + ): vol.In(self.entry_data[CONF_ALARM_ARM_STATES]), vol.Optional( CONF_ALARM_NIGHT_MODE, default=self.config_entry.options.get( CONF_ALARM_NIGHT_MODE, DEFAULT_ALARM_NIGHT_MODE ), - ): str, + ): vol.In(self.entry_data[CONF_ALARM_ARM_STATES]), vol.Optional( CONF_ALARM_CUSTOM_BYPASS_MODE, default=self.config_entry.options.get( CONF_ALARM_CUSTOM_BYPASS_MODE, DEFAULT_ALARM_CUSTOM_BYPASS_MODE ), - ): str, + ): vol.In(self.entry_data[CONF_ALARM_ARM_STATES]), vol.Optional( CONF_ALARM_VACATION_MODE, default=self.config_entry.options.get( CONF_ALARM_VACATION_MODE, DEFAULT_ALARM_VACATION_MODE ), - ): str, - }, required=False + ): vol.In(self.entry_data[CONF_ALARM_ARM_STATES]), + }, + required=False, ) return self.async_show_form(step_id="init", data_schema=data_schema) diff --git a/custom_components/control4/const.py b/custom_components/control4/const.py index 84059da..0c0a821 100644 --- a/custom_components/control4/const.py +++ b/custom_components/control4/const.py @@ -3,15 +3,15 @@ DOMAIN = "control4" CONF_ALARM_HOME_MODE = "alarm_home_mode" -DEFAULT_ALARM_HOME_MODE = "" +DEFAULT_ALARM_HOME_MODE = "(not set)" CONF_ALARM_AWAY_MODE = "alarm_away_mode" -DEFAULT_ALARM_AWAY_MODE = "" +DEFAULT_ALARM_AWAY_MODE = "(not set)" CONF_ALARM_NIGHT_MODE = "alarm_night_mode" -DEFAULT_ALARM_NIGHT_MODE = "" +DEFAULT_ALARM_NIGHT_MODE = "(not set)" CONF_ALARM_CUSTOM_BYPASS_MODE = "alarm_custom_bypass_mode" -DEFAULT_ALARM_CUSTOM_BYPASS_MODE = "" +DEFAULT_ALARM_CUSTOM_BYPASS_MODE = "(not set)" CONF_ALARM_VACATION_MODE = "alarm_vacation_mode" -DEFAULT_ALARM_VACATION_MODE = "" +DEFAULT_ALARM_VACATION_MODE = "(not set)" CONF_ACCOUNT = "account" CONF_DIRECTOR = "director" @@ -21,6 +21,7 @@ CONF_DIRECTOR_MODEL = "director_model" CONF_DIRECTOR_ALL_ITEMS = "director_all_items" CONF_CONTROLLER_UNIQUE_ID = "controller_unique_id" +CONF_ALARM_ARM_STATES = "alarm_arm_states" CONF_CONFIG_LISTENER = "config_listener" diff --git a/custom_components/control4/manifest.json b/custom_components/control4/manifest.json index 93d9526..0b90f0e 100644 --- a/custom_components/control4/manifest.json +++ b/custom_components/control4/manifest.json @@ -11,6 +11,6 @@ ], "codeowners": ["@lawtancool"], "iot_class": "local_push", - "version": "1.1.6", + "version": "1.1.7", "issue_tracker": "https://github.com/lawtancool/hass-control4/issues" }