From 6bc54898eff10cb571d2afd1388f93b64ffc21a5 Mon Sep 17 00:00:00 2001 From: Jules Dejaeghere Date: Mon, 20 May 2024 22:03:28 +0200 Subject: [PATCH] Simplify values in options --- custom_components/irm_kmi/config_flow.py | 8 +------- custom_components/irm_kmi/utils.py | 11 ++++------- tests/conftest.py | 8 +++++--- tests/test_config_flow.py | 4 ++-- tests/test_coordinator.py | 2 +- tests/test_warning_sensors.py | 2 +- 6 files changed, 14 insertions(+), 21 deletions(-) diff --git a/custom_components/irm_kmi/config_flow.py b/custom_components/irm_kmi/config_flow.py index 2742ab5..68db696 100644 --- a/custom_components/irm_kmi/config_flow.py +++ b/custom_components/irm_kmi/config_flow.py @@ -41,9 +41,6 @@ class IrmKmiConfigFlow(ConfigFlow, domain=DOMAIN): errors = {} if user_input: - if CONF_LANGUAGE_OVERRIDE in user_input: - user_input[CONF_LANGUAGE_OVERRIDE] = None if user_input[CONF_LANGUAGE_OVERRIDE] == 'none' \ - else user_input[CONF_LANGUAGE_OVERRIDE] _LOGGER.debug(f"Provided config user is: {user_input}") if (zone := self.hass.states.get(user_input[CONF_ZONE])) is None: @@ -115,9 +112,6 @@ class IrmKmiOptionFlow(OptionsFlow): async def async_step_init(self, user_input: dict | None = None) -> FlowResult: """Manage the options.""" if user_input is not None: - if CONF_LANGUAGE_OVERRIDE in user_input: - user_input[CONF_LANGUAGE_OVERRIDE] = None if user_input[CONF_LANGUAGE_OVERRIDE] == 'none' \ - else user_input[CONF_LANGUAGE_OVERRIDE] _LOGGER.debug(user_input) return self.async_create_entry(data=user_input) @@ -139,7 +133,7 @@ class IrmKmiOptionFlow(OptionsFlow): translation_key=CONF_USE_DEPRECATED_FORECAST)), vol.Optional(CONF_LANGUAGE_OVERRIDE, - default=str(get_config_value(self.config_entry, CONF_LANGUAGE_OVERRIDE)).lower()): + default=get_config_value(self.config_entry, CONF_LANGUAGE_OVERRIDE)): SelectSelector(SelectSelectorConfig(options=CONF_LANGUAGE_OVERRIDE_OPTIONS, mode=SelectSelectorMode.DROPDOWN, translation_key=CONF_LANGUAGE_OVERRIDE)) diff --git a/custom_components/irm_kmi/utils.py b/custom_components/irm_kmi/utils.py index e4f2d11..e9bb7af 100644 --- a/custom_components/irm_kmi/utils.py +++ b/custom_components/irm_kmi/utils.py @@ -28,16 +28,13 @@ def modify_from_config(hass: HomeAssistant, config_entry_id: str, enable: bool): def get_config_value(config_entry: ConfigEntry, key: str) -> Any: - try: - if config_entry.options and key in config_entry.options: - return config_entry.options[key] - return config_entry.data[key] - except KeyError: - return None + if config_entry.options and key in config_entry.options: + return config_entry.options[key] + return config_entry.data[key] def preferred_language(hass: HomeAssistant, config_entry: ConfigEntry) -> str: - if get_config_value(config_entry, CONF_LANGUAGE_OVERRIDE) is None: + if get_config_value(config_entry, CONF_LANGUAGE_OVERRIDE) == 'none': return hass.config.language if hass.config.language in LANGS else 'en' return get_config_value(config_entry, CONF_LANGUAGE_OVERRIDE) diff --git a/tests/conftest.py b/tests/conftest.py index 80a33f1..e9134a5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,7 +15,7 @@ from custom_components.irm_kmi.api import (IrmKmiApiError, from custom_components.irm_kmi.const import ( CONF_DARK_MODE, CONF_STYLE, CONF_USE_DEPRECATED_FORECAST, DOMAIN, OPTION_DEPRECATED_FORECAST_NOT_USED, - OPTION_DEPRECATED_FORECAST_TWICE_DAILY, OPTION_STYLE_STD) + OPTION_DEPRECATED_FORECAST_TWICE_DAILY, OPTION_STYLE_STD, CONF_LANGUAGE_OVERRIDE) def get_api_data(fixture: str) -> dict: @@ -52,7 +52,8 @@ def mock_config_entry() -> MockConfigEntry: data={CONF_ZONE: "zone.home", CONF_STYLE: OPTION_STYLE_STD, CONF_DARK_MODE: True, - CONF_USE_DEPRECATED_FORECAST: OPTION_DEPRECATED_FORECAST_NOT_USED}, + CONF_USE_DEPRECATED_FORECAST: OPTION_DEPRECATED_FORECAST_NOT_USED, + CONF_LANGUAGE_OVERRIDE: 'none'}, unique_id="zone.home", ) @@ -66,7 +67,8 @@ def mock_config_entry_with_deprecated() -> MockConfigEntry: data={CONF_ZONE: "zone.home", CONF_STYLE: OPTION_STYLE_STD, CONF_DARK_MODE: True, - CONF_USE_DEPRECATED_FORECAST: OPTION_DEPRECATED_FORECAST_TWICE_DAILY}, + CONF_USE_DEPRECATED_FORECAST: OPTION_DEPRECATED_FORECAST_TWICE_DAILY, + CONF_LANGUAGE_OVERRIDE: 'none'}, unique_id="zone.home", ) diff --git a/tests/test_config_flow.py b/tests/test_config_flow.py index 23c5014..549148c 100644 --- a/tests/test_config_flow.py +++ b/tests/test_config_flow.py @@ -42,7 +42,7 @@ async def test_full_user_flow( CONF_STYLE: OPTION_STYLE_STD, CONF_DARK_MODE: False, CONF_USE_DEPRECATED_FORECAST: OPTION_DEPRECATED_FORECAST_NOT_USED, - CONF_LANGUAGE_OVERRIDE: None} + CONF_LANGUAGE_OVERRIDE: 'none'} async def test_config_flow_out_benelux_zone( @@ -131,7 +131,7 @@ async def test_option_flow( CONF_STYLE: OPTION_STYLE_SATELLITE, CONF_DARK_MODE: True, CONF_USE_DEPRECATED_FORECAST: OPTION_DEPRECATED_FORECAST_NOT_USED, - CONF_LANGUAGE_OVERRIDE: None + CONF_LANGUAGE_OVERRIDE: 'none' } diff --git a/tests/test_coordinator.py b/tests/test_coordinator.py index 9103d6a..bf62186 100644 --- a/tests/test_coordinator.py +++ b/tests/test_coordinator.py @@ -92,7 +92,7 @@ async def test_daily_forecast( ) -> None: api_data = get_api_data("forecast.json").get('for', {}).get('daily') - mock_config_entry.data = {CONF_LANGUAGE_OVERRIDE: 'fr'} + mock_config_entry.data = mock_config_entry.data | {CONF_LANGUAGE_OVERRIDE: 'fr'} coordinator = IrmKmiCoordinator(hass, mock_config_entry) result = coordinator.daily_list_to_forecast(api_data) diff --git a/tests/test_warning_sensors.py b/tests/test_warning_sensors.py index 431b111..c0a07a0 100644 --- a/tests/test_warning_sensors.py +++ b/tests/test_warning_sensors.py @@ -66,7 +66,7 @@ async def test_next_warning_when_data_available( mock_config_entry: MockConfigEntry ) -> None: api_data = get_api_data("be_forecast_warning.json") - mock_config_entry.data = {CONF_LANGUAGE_OVERRIDE: 'de'} + mock_config_entry.data = mock_config_entry.data | {CONF_LANGUAGE_OVERRIDE: 'de'} coordinator = IrmKmiCoordinator(hass, mock_config_entry)