diff --git a/custom_components/saleryd_hrv/__init__.py b/custom_components/saleryd_hrv/__init__.py index ddcbc6f..b601e4e 100644 --- a/custom_components/saleryd_hrv/__init__.py +++ b/custom_components/saleryd_hrv/__init__.py @@ -40,7 +40,11 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Migrate config entry.""" - if entry.version == 1: + if entry.version is not None and entry.version > CONFIG_VERSION: + # This means the user has downgraded from a future version + return False + + if entry.version is None or entry.version == 1: new_data = entry.data.copy() new_data |= { CONF_ENABLE_MAINTENANCE_SETTINGS: False, diff --git a/custom_components/saleryd_hrv/config_flow.py b/custom_components/saleryd_hrv/config_flow.py index 266719f..cc8855f 100644 --- a/custom_components/saleryd_hrv/config_flow.py +++ b/custom_components/saleryd_hrv/config_flow.py @@ -22,6 +22,21 @@ NAME, ) +RECONFIG_DATA = { + vol.Required(CONF_WEBSOCKET_IP): str, + vol.Required(CONF_WEBSOCKET_PORT): int, + vol.Optional(CONF_ENABLE_MAINTENANCE_SETTINGS): vol.Coerce(bool), + vol.Optional(CONF_MAINTENANCE_PASSWORD): str, +} + +CONFIG_DATA = { + vol.Required(CONF_NAME): str, + **RECONFIG_DATA, +} + +CONFIG_SCHEMA = vol.Schema({**CONFIG_DATA}) +RECONFIG_SCHEMA = vol.Schema({**RECONFIG_DATA}) + class SalerydLokeFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Config flow for SalerydLoke.""" @@ -55,74 +70,72 @@ async def async_step_user(self, user_input=None): title=user_input[CONF_NAME], data=user_input ) - return await self._show_config_form("user", user_input) - - user_input = {} - # Provide defaults for form - user_input[CONF_WEBSOCKET_IP] = "192.168.1.151" - user_input[CONF_WEBSOCKET_PORT] = 3001 - user_input[CONF_NAME] = NAME - user_input[CONF_ENABLE_MAINTENANCE_SETTINGS] = False - user_input[CONF_MAINTENANCE_PASSWORD] = "" - - return await self._show_config_form("user", user_input) + return self.async_show_form( + data_schema=self.add_suggested_values_to_schema( + CONFIG_SCHEMA, user_input + ), + errors=self._errors, + ) + else: + suggested_values = { + CONF_NAME: NAME, + CONF_WEBSOCKET_IP: "192.168.1.151", + CONF_WEBSOCKET_PORT: 3001, + CONF_ENABLE_MAINTENANCE_SETTINGS: False, + CONF_MAINTENANCE_PASSWORD: "", + } + + return self.async_show_form( + data_schema=self.add_suggested_values_to_schema( + CONFIG_SCHEMA, suggested_values + ), + errors=self._errors, + ) async def async_step_reconfigure( - self, entry_data: Mapping[str, Any] + self, user_input: Mapping[str, Any] ) -> config_entries.ConfigFlowResult: """Handle a reconfiguration flow initialized by the user.""" config_entry = self.hass.config_entries.async_get_entry( self.context["entry_id"] ) self._config_entry = config_entry - return await self.async_step_reconfigure_confirm() + return await self.async_step_reconfigure_confirm(user_input) async def async_step_reconfigure_confirm( self, user_input: dict[str, Any] | None = None ) -> config_entries.ConfigFlowResult: """Handle a reconfiguration flow initialized by the user.""" - if not user_input: - return await self._show_config_form( - step_id="reconfigure_confirm", user_input={**self._config_entry.data} - ) - return self.async_update_reload_and_abort( - self._config_entry, - data=user_input, - reason="reconfigure_successful", - ) + self._errors = {} - async def _show_config_form( - self, step_id, user_input - ): # pylint: disable=unused-argument - """Show the configuration form to edit configuration data.""" - return self.async_show_form( - step_id=step_id, - data_schema=vol.Schema( - { - vol.Required( - CONF_NAME, - default=user_input[CONF_NAME], - ): str, - vol.Required( - CONF_WEBSOCKET_IP, - default=user_input[CONF_WEBSOCKET_IP], - ): str, - vol.Required( - CONF_WEBSOCKET_PORT, - default=user_input[CONF_WEBSOCKET_PORT], - ): int, - vol.Optional( - CONF_ENABLE_MAINTENANCE_SETTINGS, - default=user_input[CONF_ENABLE_MAINTENANCE_SETTINGS], - ): vol.Coerce(bool), - vol.Optional( - CONF_MAINTENANCE_PASSWORD, - default=user_input[CONF_MAINTENANCE_PASSWORD], - ): str, - } - ), - errors=self._errors, - ) + if user_input is not None: + try: + async with async_timeout.timeout(10): + await self._test_connection( + user_input[CONF_WEBSOCKET_IP], user_input[CONF_WEBSOCKET_PORT] + ) + except TimeoutError: + self._errors["base"] = "connect" + else: + return self.async_update_reload_and_abort( + self._config_entry, + data=user_input, + reason="reconfigure_successful", + ) + + return self.async_show_form( + data_schema=self.add_suggested_values_to_schema( + RECONFIG_SCHEMA, user_input + ), + errors=self._errors, + ) + else: + return self.async_show_form( + data_schema=self.add_suggested_values_to_schema( + RECONFIG_SCHEMA, self._config_entry.data + ), + errors=self._errors, + ) async def _test_connection(self, ip, port): """Return true if connection is working""" diff --git a/custom_components/saleryd_hrv/strings.json b/custom_components/saleryd_hrv/strings.json index 4663847..1b1ef05 100644 --- a/custom_components/saleryd_hrv/strings.json +++ b/custom_components/saleryd_hrv/strings.json @@ -2,7 +2,7 @@ "config": { "step": { "user": { - "description": "Follow instructions in manual to connect the HRV unit to local network and enter connection details below. If you need help with the configuration have a look here: https://github.com/bj00rn/ha-saleryd-ftx", + "description": "Follow instructions in manual to connect the HRV unit to local network and enter connection details below.", "data": { "name": "Name", "websocket_ip": "IP adress", @@ -17,8 +17,8 @@ "maintenance_password": "Required for maintenance settings" } }, - "reconfigure_confirm": { - "description": "Follow instructions in manual to connect the HRV unit to local network and enter connection details below. If you need help with the configuration have a look here: https://github.com/bj00rn/ha-saleryd-ftx", + "reconfigure": { + "description": "Follow instructions in manual to connect the HRV unit to local network and enter connection details below.", "data": { "name": "Name", "websocket_ip": "IP adress", diff --git a/custom_components/saleryd_hrv/translations/en.json b/custom_components/saleryd_hrv/translations/en.json index 4663847..e4bbdaa 100644 --- a/custom_components/saleryd_hrv/translations/en.json +++ b/custom_components/saleryd_hrv/translations/en.json @@ -2,7 +2,7 @@ "config": { "step": { "user": { - "description": "Follow instructions in manual to connect the HRV unit to local network and enter connection details below. If you need help with the configuration have a look here: https://github.com/bj00rn/ha-saleryd-ftx", + "description": "Follow instructions in manual to connect the HRV unit to local network and enter connection details below", "data": { "name": "Name", "websocket_ip": "IP adress", @@ -17,8 +17,8 @@ "maintenance_password": "Required for maintenance settings" } }, - "reconfigure_confirm": { - "description": "Follow instructions in manual to connect the HRV unit to local network and enter connection details below. If you need help with the configuration have a look here: https://github.com/bj00rn/ha-saleryd-ftx", + "reconfigure": { + "description": "Follow instructions in manual to connect the HRV unit to local network and enter connection details below.", "data": { "name": "Name", "websocket_ip": "IP adress", diff --git a/requirements_dev.txt b/requirements_dev.txt index 2634dd1..b321220 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -1,4 +1,5 @@ -r requirements_test.txt homeassistant==2024.10.3 pre-commit==3.6.0 +colorlog debugpy