diff --git a/custom_components/hnap_device/binary_sensor.py b/custom_components/hnap_device/binary_sensor.py index 92ec35f..28b5343 100644 --- a/custom_components/hnap_device/binary_sensor.py +++ b/custom_components/hnap_device/binary_sensor.py @@ -75,7 +75,7 @@ async def async_setup_entry( unique_id=f"{config_entry.entry_id}-{PLATFORM}", device_info=device_info, device=device, - auto_reboot=config_entry.data[CONF_AUTO_REBOOT], + auto_reboot=config_entry.options[CONF_AUTO_REBOOT], ) ], update_before_add=True, diff --git a/custom_components/hnap_device/config_flow.py b/custom_components/hnap_device/config_flow.py index 3c9d3d3..a29b7a9 100644 --- a/custom_components/hnap_device/config_flow.py +++ b/custom_components/hnap_device/config_flow.py @@ -19,6 +19,7 @@ """Config flow for HNAP device integration.""" +from __future__ import annotations import functools import logging @@ -28,20 +29,24 @@ import requests import voluptuous as vol from homeassistant import config_entries -from homeassistant.const import CONF_NAME, CONF_HOST, CONF_PASSWORD, CONF_USERNAME -from homeassistant.core import HomeAssistant +from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME +from homeassistant.core import HomeAssistant, callback from homeassistant.data_entry_flow import FlowResult from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers.schema_config_entry_flow import ( + SchemaFlowFormStep, + SchemaOptionsFlowHandler, +) from .const import ( + CONF_AUTO_REBOOT, CONF_PLATFORMS, + DEFAULT_AUTO_REBOOT, + DEFAULT_USERNAME, DOMAIN, PLATFORM_BINARY_SENSOR, PLATFORM_CAMERA, PLATFORM_SIREN, - DEFAULT_USERNAME, - CONF_AUTO_REBOOT, - DEFAULT_AUTO_REBOOT, ) _LOGGER = logging.getLogger(__name__) @@ -51,10 +56,19 @@ vol.Required(CONF_HOST): str, vol.Required(CONF_USERNAME, default=DEFAULT_USERNAME): str, vol.Required(CONF_PASSWORD): str, + } +) + +OPTIONS_SCHEMA = vol.Schema( + { vol.Required(CONF_AUTO_REBOOT, default=DEFAULT_AUTO_REBOOT): bool, } ) +OPTIONS_FLOW = { + "init": SchemaFlowFormStep(OPTIONS_SCHEMA), +} + async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]: """Validate the user input allows us to connect. @@ -97,7 +111,7 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, CONF_PASSWORD: data[CONF_PASSWORD], CONF_USERNAME: data[CONF_USERNAME], CONF_PLATFORMS: platforms, - CONF_AUTO_REBOOT: data[CONF_AUTO_REBOOT], + CONF_AUTO_REBOOT: DEFAULT_AUTO_REBOOT, } @@ -135,6 +149,13 @@ async def async_step_user( step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors ) + @staticmethod + @callback + def async_get_options_flow( + config_entry: config_entries.ConfigEntry, + ) -> SchemaOptionsFlowHandler: + return SchemaOptionsFlowHandler(config_entry, OPTIONS_FLOW) + class CannotConnect(HomeAssistantError): """Error to indicate we cannot connect.""" diff --git a/custom_components/hnap_device/siren.py b/custom_components/hnap_device/siren.py index b097a4e..7669df9 100644 --- a/custom_components/hnap_device/siren.py +++ b/custom_components/hnap_device/siren.py @@ -99,7 +99,7 @@ async def async_setup_entry( unique_id=f"{config_entry.entry_id}-{PLATFORM}", device_info=device_info, device=device, - auto_reboot=config_entry.data[CONF_AUTO_REBOOT], + auto_reboot=config_entry.options[CONF_AUTO_REBOOT], ) ], update_before_add=True, diff --git a/custom_components/hnap_device/strings.json b/custom_components/hnap_device/strings.json index 4dae1d4..55173af 100644 --- a/custom_components/hnap_device/strings.json +++ b/custom_components/hnap_device/strings.json @@ -18,5 +18,14 @@ "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_device%]" } + }, + "options": { + "step": { + "init": { + "data": { + "auto_reboot": "Reboot device each 12 hours" + } + } + } } } \ No newline at end of file diff --git a/custom_components/hnap_device/translations/en.json b/custom_components/hnap_device/translations/en.json index 31a77b8..a0aa68c 100644 --- a/custom_components/hnap_device/translations/en.json +++ b/custom_components/hnap_device/translations/en.json @@ -14,7 +14,15 @@ "data": { "host": "Host", "password": "Password", - "username": "Username", + "username": "Username" + } + } + } + }, + "options": { + "step": { + "init": { + "data": { "auto_reboot": "Reboot device each 12 hours" } } diff --git a/custom_components/hnap_device/translations/es.json b/custom_components/hnap_device/translations/es.json new file mode 100644 index 0000000..64ef2d3 --- /dev/null +++ b/custom_components/hnap_device/translations/es.json @@ -0,0 +1,11 @@ +{ + "options": { + "step": { + "init": { + "data": { + "auto_reboot": "Reiniciar el dispositivo cada 12 horas" + } + } + } + } +} \ No newline at end of file