From 2ea8fd070ee4c08cf43bcc69af471d460d7f4bdb Mon Sep 17 00:00:00 2001 From: Alex Wijnholds Date: Tue, 9 May 2023 19:42:03 +0200 Subject: [PATCH] Fixed mode detection, again --- custom_components/sat/__init__.py | 6 +++- custom_components/sat/binary_sensor.py | 8 +++-- custom_components/sat/config_flow.py | 47 +++++++++++++------------- custom_components/sat/const.py | 2 +- custom_components/sat/number.py | 8 +++-- custom_components/sat/sensor.py | 8 +++-- 6 files changed, 47 insertions(+), 32 deletions(-) diff --git a/custom_components/sat/__init__.py b/custom_components/sat/__init__.py index 7b047bd2..660906d0 100644 --- a/custom_components/sat/__init__.py +++ b/custom_components/sat/__init__.py @@ -63,10 +63,14 @@ async def async_unload_entry(_hass: HomeAssistant, _entry: ConfigEntry) -> bool: This function is called by Home Assistant when the integration is being removed. """ + # Retrieve the defaults and override it with the user options + options = OPTIONS_DEFAULTS.copy() + options.update(_entry.data) + # Unload the entry and its dependent components unloaded = all( await asyncio.gather( - _hass.config_entries.async_forward_entry_unload(_entry, _entry.data.get(CONF_MODE)), + _hass.config_entries.async_forward_entry_unload(_entry, options.get(CONF_MODE)), _hass.config_entries.async_unload_platforms(_entry, [CLIMATE, SENSOR, NUMBER, BINARY_SENSOR]), ) ) diff --git a/custom_components/sat/binary_sensor.py b/custom_components/sat/binary_sensor.py index 47209a4a..8719d5be 100644 --- a/custom_components/sat/binary_sensor.py +++ b/custom_components/sat/binary_sensor.py @@ -2,7 +2,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import CONF_MODE, MODE_OPENTHERM +from .const import CONF_MODE, MODE_OPENTHERM, OPTIONS_DEFAULTS from .opentherm import binary_sensor as opentherm_binary_sensor @@ -11,7 +11,11 @@ async def async_setup_entry(_hass: HomeAssistant, _config_entry: ConfigEntry, _a Add binary sensors for the OpenTherm protocol if the integration is set to use it. """ + # Retrieve the defaults and override it with the user options + options = OPTIONS_DEFAULTS.copy() + options.update(_config_entry.data) + # Check if integration is set to use the OpenTherm protocol - if _config_entry.data.get(CONF_MODE) == MODE_OPENTHERM: + if options.get(CONF_MODE) == MODE_OPENTHERM: # Call function to set up OpenTherm binary sensors await opentherm_binary_sensor.async_setup_entry(_hass, _config_entry, _async_add_entities) diff --git a/custom_components/sat/config_flow.py b/custom_components/sat/config_flow.py index e17b9b78..b7c0a491 100644 --- a/custom_components/sat/config_flow.py +++ b/custom_components/sat/config_flow.py @@ -134,7 +134,6 @@ class SatOptionsFlowHandler(config_entries.OptionsFlow): def __init__(self, config_entry: ConfigEntry): self._config_entry = config_entry self._options = dict(config_entry.options) - self._mode = config_entry.data.get(CONF_MODE) async def async_step_init(self, _user_input=None): return await self.async_step_user(_user_input) @@ -154,19 +153,19 @@ async def async_step_general(self, _user_input=None) -> FlowResult: if _user_input is not None: return await self.update_options(_user_input) - defaults = await self.get_options() + options = await self.get_options() schema = { - vol.Required(CONF_HEATING_CURVE_COEFFICIENT, default=defaults[CONF_HEATING_CURVE_COEFFICIENT]): selector.NumberSelector( + vol.Required(CONF_HEATING_CURVE_COEFFICIENT, default=options[CONF_HEATING_CURVE_COEFFICIENT]): selector.NumberSelector( selector.NumberSelectorConfig(min=0.1, max=12, step=0.1) ), - vol.Required(CONF_TARGET_TEMPERATURE_STEP, default=defaults[CONF_TARGET_TEMPERATURE_STEP]): selector.NumberSelector( + vol.Required(CONF_TARGET_TEMPERATURE_STEP, default=options[CONF_TARGET_TEMPERATURE_STEP]): selector.NumberSelector( selector.NumberSelectorConfig(min=0.1, max=1, step=0.05) ), } - if self._mode == MODE_OPENTHERM: - schema[vol.Required(CONF_HEATING_SYSTEM, default=defaults[CONF_HEATING_SYSTEM])] = selector.SelectSelector( + if options.get(CONF_MODE) == MODE_OPENTHERM: + schema[vol.Required(CONF_HEATING_SYSTEM, default=options[CONF_HEATING_SYSTEM])] = selector.SelectSelector( selector.SelectSelectorConfig(options=[ {"value": HEATING_SYSTEM_RADIATOR_HIGH_TEMPERATURES, "label": "Radiators ( High Temperatures )"}, {"value": HEATING_SYSTEM_RADIATOR_MEDIUM_TEMPERATURES, "label": "Radiators ( Medium Temperatures )"}, @@ -175,18 +174,18 @@ async def async_step_general(self, _user_input=None) -> FlowResult: ]) ) - if self._mode == MODE_SWITCH: + if options.get(CONF_MODE) == MODE_SWITCH: schema[vol.Required(CONF_SETPOINT, default=50)] = selector.NumberSelector( selector.NumberSelectorConfig(min=0, max=100, step=1) ) - if not defaults.get(CONF_AUTOMATIC_GAINS): - schema[vol.Required(CONF_PROPORTIONAL, default=defaults.get(CONF_PROPORTIONAL))] = str - schema[vol.Required(CONF_INTEGRAL, default=defaults.get(CONF_INTEGRAL))] = str - schema[vol.Required(CONF_DERIVATIVE, default=defaults.get(CONF_DERIVATIVE))] = str + if not options.get(CONF_AUTOMATIC_GAINS): + schema[vol.Required(CONF_PROPORTIONAL, default=options.get(CONF_PROPORTIONAL))] = str + schema[vol.Required(CONF_INTEGRAL, default=options.get(CONF_INTEGRAL))] = str + schema[vol.Required(CONF_DERIVATIVE, default=options.get(CONF_DERIVATIVE))] = str - if not defaults.get(CONF_AUTOMATIC_DUTY_CYCLE): - schema[vol.Required(CONF_DUTY_CYCLE, default=defaults.get(CONF_DUTY_CYCLE))] = selector.TimeSelector() + if not options.get(CONF_AUTOMATIC_DUTY_CYCLE): + schema[vol.Required(CONF_DUTY_CYCLE, default=options.get(CONF_DUTY_CYCLE))] = selector.TimeSelector() return self.async_show_form(step_id="general", data_schema=vol.Schema(schema)) @@ -260,23 +259,23 @@ async def async_step_advanced(self, _user_input=None) -> FlowResult: if _user_input is not None: return await self.update_options(_user_input) - defaults = await self.get_options() + options = await self.get_options() schema = { - vol.Required(CONF_SIMULATION, default=defaults[CONF_SIMULATION]): bool, - vol.Required(CONF_AUTOMATIC_GAINS, default=defaults.get(CONF_AUTOMATIC_GAINS)): bool, - vol.Required(CONF_AUTOMATIC_DUTY_CYCLE, default=defaults.get(CONF_AUTOMATIC_DUTY_CYCLE)): bool, + vol.Required(CONF_SIMULATION, default=options[CONF_SIMULATION]): bool, + vol.Required(CONF_AUTOMATIC_GAINS, default=options.get(CONF_AUTOMATIC_GAINS)): bool, + vol.Required(CONF_AUTOMATIC_DUTY_CYCLE, default=options.get(CONF_AUTOMATIC_DUTY_CYCLE)): bool, } - if self._mode == MODE_OPENTHERM: - schema[vol.Required(CONF_FORCE_PULSE_WIDTH_MODULATION, default=defaults[CONF_FORCE_PULSE_WIDTH_MODULATION])] = bool - schema[vol.Required(CONF_OVERSHOOT_PROTECTION, default=defaults[CONF_OVERSHOOT_PROTECTION])] = bool + if options.get(CONF_MODE) == MODE_OPENTHERM: + schema[vol.Required(CONF_FORCE_PULSE_WIDTH_MODULATION, default=options[CONF_FORCE_PULSE_WIDTH_MODULATION])] = bool + schema[vol.Required(CONF_OVERSHOOT_PROTECTION, default=options[CONF_OVERSHOOT_PROTECTION])] = bool - schema[vol.Required(CONF_SAMPLE_TIME, default=defaults.get(CONF_SAMPLE_TIME))] = selector.TimeSelector() - schema[vol.Required(CONF_SENSOR_MAX_VALUE_AGE, default=defaults.get(CONF_SENSOR_MAX_VALUE_AGE))] = selector.TimeSelector() - schema[vol.Required(CONF_WINDOW_MINIMUM_OPEN_TIME, default=defaults.get(CONF_WINDOW_MINIMUM_OPEN_TIME))] = selector.TimeSelector() + schema[vol.Required(CONF_SAMPLE_TIME, default=options.get(CONF_SAMPLE_TIME))] = selector.TimeSelector() + schema[vol.Required(CONF_SENSOR_MAX_VALUE_AGE, default=options.get(CONF_SENSOR_MAX_VALUE_AGE))] = selector.TimeSelector() + schema[vol.Required(CONF_WINDOW_MINIMUM_OPEN_TIME, default=options.get(CONF_WINDOW_MINIMUM_OPEN_TIME))] = selector.TimeSelector() - schema[vol.Required(CONF_CLIMATE_VALVE_OFFSET, default=defaults[CONF_CLIMATE_VALVE_OFFSET])] = selector.NumberSelector( + schema[vol.Required(CONF_CLIMATE_VALVE_OFFSET, default=options[CONF_CLIMATE_VALVE_OFFSET])] = selector.NumberSelector( selector.NumberSelectorConfig(min=-1, max=1, step=0.1) ) diff --git a/custom_components/sat/const.py b/custom_components/sat/const.py index ccd10c75..e5e01066 100644 --- a/custom_components/sat/const.py +++ b/custom_components/sat/const.py @@ -3,7 +3,7 @@ # Base component constants NAME = "Smart Autotune Thermostat" DOMAIN = "sat" -VERSION = "2.0.2" +VERSION = "2.0.3" COORDINATOR = "coordinator" CONFIG_STORE = "config_store" diff --git a/custom_components/sat/number.py b/custom_components/sat/number.py index fcbf1d61..55187674 100644 --- a/custom_components/sat/number.py +++ b/custom_components/sat/number.py @@ -2,7 +2,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import CONF_MODE, MODE_OPENTHERM +from .const import CONF_MODE, MODE_OPENTHERM, OPTIONS_DEFAULTS from .opentherm import number as opentherm_number @@ -11,7 +11,11 @@ async def async_setup_entry(_hass: HomeAssistant, _config_entry: ConfigEntry, _a Add sensors for the OpenTherm protocol if the integration is set to use it. """ + # Retrieve the defaults and override it with the user options + options = OPTIONS_DEFAULTS.copy() + options.update(_config_entry.data) + # Check if integration is set to use the OpenTherm protocol - if _config_entry.data.get(CONF_MODE) == MODE_OPENTHERM: + if options.get(CONF_MODE) == MODE_OPENTHERM: # Call function to set up OpenTherm numbers await opentherm_number.async_setup_entry(_hass, _config_entry, _async_add_entities) diff --git a/custom_components/sat/sensor.py b/custom_components/sat/sensor.py index c5cf32cf..5f993501 100644 --- a/custom_components/sat/sensor.py +++ b/custom_components/sat/sensor.py @@ -2,7 +2,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import CONF_MODE, MODE_OPENTHERM +from .const import CONF_MODE, MODE_OPENTHERM, OPTIONS_DEFAULTS from .opentherm import sensor as opentherm_sensor @@ -11,7 +11,11 @@ async def async_setup_entry(_hass: HomeAssistant, _config_entry: ConfigEntry, _a Add sensors for the OpenTherm protocol if the integration is set to use it. """ + # Retrieve the defaults and override it with the user options + options = OPTIONS_DEFAULTS.copy() + options.update(_config_entry.data) + # Check if integration is set to use the OpenTherm protocol - if _config_entry.data.get(CONF_MODE) == MODE_OPENTHERM: + if options.get(CONF_MODE) == MODE_OPENTHERM: # Call function to set up OpenTherm sensors await opentherm_sensor.async_setup_entry(_hass, _config_entry, _async_add_entities)