Skip to content

Commit

Permalink
feat: add options config flow
Browse files Browse the repository at this point in the history
muhlba91 committed Dec 15, 2023
1 parent d8bc6ba commit e37ae4d
Showing 4 changed files with 68 additions and 10 deletions.
62 changes: 59 additions & 3 deletions custom_components/hella_onyx/config_flow.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,14 @@
import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
OptionsFlow,
FlowResult,
CONN_CLASS_LOCAL_POLL,
)
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_SCAN_INTERVAL,
@@ -38,11 +45,11 @@
)


class OnyxFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
class OnyxFlowHandler(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for ONYX."""

VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
CONNECTION_CLASS = CONN_CLASS_LOCAL_POLL

async def async_step_user(self, user_input=None):
"""Handle a flow initiated by the user."""
@@ -88,6 +95,14 @@ async def async_step_init(self, user_input, is_import=False):
errors=errors,
)

@staticmethod
@callback
def async_get_options_flow(
config_entry: ConfigEntry,
) -> "OptionsFlowHandler":
"""Get the options flow for this handler."""
return OptionsFlowHandler(config_entry)

async def _async_exists(self, fingerprint):
"""Check if the endpoint exists already."""
existing_fingerprints = [
@@ -103,3 +118,44 @@ async def _async_verify_conn(self, fingerprint, token):
access_token=token,
client_session=async_get_clientsession(self.hass, False),
).verify()


class OptionsFlowHandler(OptionsFlow):
"""Handle a option flow for ONYX."""

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

async def async_step_init(self, user_input=None) -> FlowResult:
"""Handle options flow."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

data_schema = vol.Schema(
{
vol.Required(
CONF_MIN_DIM_DURATION,
default=self.config_entry.options.get(
CONF_MIN_DIM_DURATION, DEFAULT_MIN_DIM_DURATION
),
): cv.positive_int,
vol.Required(
CONF_MAX_DIM_DURATION,
default=self.config_entry.options.get(
CONF_MAX_DIM_DURATION, DEFAULT_MAX_DIM_DURATION
),
): cv.positive_int,
vol.Required(
CONF_SCAN_INTERVAL,
default=self.config_entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_INTERVAL
),
): cv.positive_int,
vol.Required(
CONF_FORCE_UPDATE,
default=self.config_entry.options.get(CONF_FORCE_UPDATE, False),
): cv.boolean,
}
)
return self.async_show_form(step_id="init", data_schema=data_schema)
4 changes: 2 additions & 2 deletions custom_components/hella_onyx/const.py
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@

INCREASED_INTERVAL_DELTA = 1
DEFAULT_INTERVAL = 60
DEFAULT_MIN_DIM_DURATION = 500
DEFAULT_MAX_DIM_DURATION = 6000
DEFAULT_MIN_DIM_DURATION = 250
DEFAULT_MAX_DIM_DURATION = 5000

MAX_BACKOFF_TIME = 1
2 changes: 2 additions & 0 deletions custom_components/hella_onyx/strings.json
Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@
"data": {
"fingerprint": "[%key:common::config_flow::data::fingerprint%]",
"token": "[%key:common::config_flow::data::token%]",
"min_dim_duration": "[%key:common::config_flow::data::min_dim_duration%]",
"max_dim_duration": "[%key:common::config_flow::data::max_dim_duration%]",
"scan_interval": "[%key:common::config_flow::data::scan_interval%]",
"force_update": "[%key:common::config_flow::data::force_update%]"
}
10 changes: 5 additions & 5 deletions tests/sensors/test_light.py
Original file line number Diff line number Diff line change
@@ -173,7 +173,7 @@ def test_turn_on(self, mock_run_coroutine_threadsafe, api, entity, device):
"uuid",
{
"target_brightness": 4,
"dim_duration": 5780,
"dim_duration": 4810,
},
)
assert mock_run_coroutine_threadsafe.called
@@ -216,7 +216,7 @@ def test__get_dim_duration(self, api, entity, device):
value=14645, maximum=65535, minimum=0, read_only=False
)
api.device.return_value = device
assert entity._get_dim_duration(31) == 1726
assert entity._get_dim_duration(31) == 1309
assert api.device.called

def test__get_dim_duration_custom_max(self, api, entity, device):
@@ -225,7 +225,7 @@ def test__get_dim_duration_custom_max(self, api, entity, device):
)
api.config.max_dim_duration = 2000
api.device.return_value = device
assert entity._get_dim_duration(31) == 834
assert entity._get_dim_duration(31) == 640
assert api.device.called

def test__get_dim_duration_custom_min(self, api, entity, device):
@@ -234,7 +234,7 @@ def test__get_dim_duration_custom_min(self, api, entity, device):
)
api.config.min_dim_duration = 2000
api.device.return_value = device
assert entity._get_dim_duration(31) == 2891
assert entity._get_dim_duration(31) == 2668
assert api.device.called

def test__get_dim_duration_same(self, api, entity, device):
@@ -250,7 +250,7 @@ def test__get_dim_duration_invalid_value(self, api, entity, device):
value=None, maximum=100, minimum=0, read_only=False
)
api.device.return_value = device
assert entity._get_dim_duration(90) == 5450
assert entity._get_dim_duration(90) == 4525
assert api.device.called

def test__get_dim_duration_actual_lower_than_new(self, api, entity, device):

0 comments on commit e37ae4d

Please sign in to comment.