Skip to content

Commit

Permalink
Move auto reboot to options
Browse files Browse the repository at this point in the history
  • Loading branch information
ldotlopez committed Mar 17, 2023
1 parent d17797e commit 2b9803a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
2 changes: 1 addition & 1 deletion custom_components/hnap_device/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 27 additions & 6 deletions custom_components/hnap_device/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


"""Config flow for HNAP device integration."""
from __future__ import annotations

import functools
import logging
Expand All @@ -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__)
Expand All @@ -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.
Expand Down Expand Up @@ -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,
}


Expand Down Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hnap_device/siren.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions custom_components/hnap_device/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
}
10 changes: 9 additions & 1 deletion custom_components/hnap_device/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
"data": {
"host": "Host",
"password": "Password",
"username": "Username",
"username": "Username"
}
}
}
},
"options": {
"step": {
"init": {
"data": {
"auto_reboot": "Reboot device each 12 hours"
}
}
Expand Down
11 changes: 11 additions & 0 deletions custom_components/hnap_device/translations/es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"options": {
"step": {
"init": {
"data": {
"auto_reboot": "Reiniciar el dispositivo cada 12 horas"
}
}
}
}
}

0 comments on commit 2b9803a

Please sign in to comment.