Skip to content

Commit

Permalink
added component option flow
Browse files Browse the repository at this point in the history
  • Loading branch information
vaproloff committed Nov 11, 2024
1 parent a944b2b commit 95a0b76
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 27 deletions.
18 changes: 15 additions & 3 deletions custom_components/tion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,15 @@ async def async_setup(hass: HomeAssistant, config: ConfigType):

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up this integration using UI."""

if hass.data.get(DOMAIN) is None:
hass.data.setdefault(DOMAIN, {})

if entry.options:
entry_data = dict(entry.data)
entry_data.update(entry.options)
hass.config_entries.async_update_entry(entry, data=entry_data, options={})

async def update_auth_data(**kwargs):
hass.config_entries.async_update_entry(entry, data=kwargs)

Expand Down Expand Up @@ -141,13 +147,19 @@ async def update_auth_data(**kwargs):
else:
_LOGGER.info("Unsupported device type: %s", device.type)

await hass.async_create_task(
hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
)
if not entry.update_listeners:
entry.add_update_listener(async_update_options)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

return True


async def async_update_options(hass: HomeAssistant, entry: ConfigEntry):
"""Handle updating entry options."""
await hass.config_entries.async_reload(entry.entry_id)


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Handle removal of an entry."""
unloaded = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
Expand Down
88 changes: 65 additions & 23 deletions custom_components/tion/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@

import voluptuous as vol

from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlow,
)
from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.aiohttp_client import async_create_clientsession

from .client import TionClient
Expand All @@ -33,6 +40,12 @@ async def _get_auth_data(
)
return await api.authorization

@staticmethod
@callback
def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
"""Create the options flow."""
return TionOptionsFlow(config_entry)

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
Expand All @@ -42,10 +55,6 @@ async def async_step_user(
if user_input is not None:
self._async_abort_entries_match({CONF_USERNAME: user_input[CONF_USERNAME]})

sha256_hash = hashlib.new("sha256")
sha256_hash.update(user_input[CONF_USERNAME].encode())
sha256_hex = sha256_hash.hexdigest()

try:
interval = int(user_input.get(CONF_SCAN_INTERVAL))
except ValueError:
Expand All @@ -61,22 +70,25 @@ async def async_step_user(

if auth_data is None:
errors["base"] = "invalid_auth"
else:
unique_id = f"{sha256_hex}"

# Checks that the device is actually unique, otherwise abort
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()

return self.async_create_entry(
title=user_input[CONF_USERNAME],
data={
CONF_USERNAME: user_input[CONF_USERNAME],
CONF_PASSWORD: user_input[CONF_PASSWORD],
CONF_SCAN_INTERVAL: interval,
AUTH_DATA: auth_data,
},
)
raise ConfigEntryAuthFailed

sha256_hash = hashlib.new("sha256")
sha256_hash.update(user_input[CONF_USERNAME].encode())
unique_id = f"{sha256_hash.hexdigest()}"

# Checks that the device is actually unique, otherwise abort
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()

return self.async_create_entry(
title=user_input[CONF_USERNAME],
data={
CONF_USERNAME: user_input[CONF_USERNAME],
CONF_PASSWORD: user_input[CONF_PASSWORD],
CONF_SCAN_INTERVAL: interval,
AUTH_DATA: auth_data,
},
)

return self.async_show_form(
step_id="user",
Expand All @@ -85,8 +97,8 @@ async def async_step_user(
vol.Required(CONF_USERNAME, default=""): str,
vol.Required(CONF_PASSWORD, default=""): str,
vol.Required(
CONF_SCAN_INTERVAL, default=f"{DEFAULT_SCAN_INTERVAL}"
): str,
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
): vol.Coerce(int),
}
),
errors=errors,
Expand All @@ -100,3 +112,33 @@ async def async_step_import(
{CONF_USERNAME: import_config.get(CONF_USERNAME)}
)
return await self.async_step_user(import_config)


class TionOptionsFlow(OptionsFlow):
"""Tion options flow handler."""

def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize Tion options flow."""
self._entry_data = dict(config_entry.data)

async def async_step_init(self, user_input=None):
"""Manage the options."""

if user_input is not None:
return self.async_create_entry(title="", data=user_input)

return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Required(
CONF_PASSWORD,
default=self.config_entry.data.get(CONF_PASSWORD),
): str,
vol.Required(
CONF_SCAN_INTERVAL,
default=self.config_entry.data.get(CONF_SCAN_INTERVAL),
): vol.Coerce(int),
}
),
)
2 changes: 1 addition & 1 deletion custom_components/tion/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/vaproloff/tion_home_assistant/issues",
"requirements": [],
"version": "2024.11.1"
"version": "2024.11.2"
}
13 changes: 13 additions & 0 deletions custom_components/tion/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
"unknown": "Unexpected error"
}
},
"options": {
"step": {
"init": {
"data": {
"password": "Password",
"scan_interval": "API poll interval"
},
"data_description": {
"scan_interval": "Tion devices data minimum update period."
}
}
}
},
"entity": {
"climate": {
"tion_breezer": {
Expand Down
13 changes: 13 additions & 0 deletions custom_components/tion/translations/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
"unknown": "Неизвестная ошибка"
}
},
"options": {
"step": {
"init": {
"data": {
"password": "Пароль",
"scan_interval": "Интервал обновления"
},
"data_description": {
"scan_interval": "Минимальный период обновления данных устройств Tion от API."
}
}
}
},
"entity": {
"climate": {
"tion_breezer": {
Expand Down

0 comments on commit 95a0b76

Please sign in to comment.