Skip to content

Commit

Permalink
Merge pull request #51 from bj00rn/chore/rework-schema
Browse files Browse the repository at this point in the history
Chore/rework schema
  • Loading branch information
bj00rn authored Nov 5, 2024
2 parents 222e201 + 31126c2 commit 205e2e1
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 61 deletions.
6 changes: 5 additions & 1 deletion custom_components/saleryd_hrv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
121 changes: 67 additions & 54 deletions custom_components/saleryd_hrv/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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"""
Expand Down
6 changes: 3 additions & 3 deletions custom_components/saleryd_hrv/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions custom_components/saleryd_hrv/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-r requirements_test.txt
homeassistant==2024.10.3
pre-commit==3.6.0
colorlog
debugpy

0 comments on commit 205e2e1

Please sign in to comment.