Skip to content

Commit

Permalink
Fixed mode detection, again
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexwijn committed May 9, 2023
1 parent c643e4f commit 2ea8fd0
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 32 deletions.
6 changes: 5 additions & 1 deletion custom_components/sat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
)
)
Expand Down
8 changes: 6 additions & 2 deletions custom_components/sat/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)
47 changes: 23 additions & 24 deletions custom_components/sat/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 )"},
Expand All @@ -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))

Expand Down Expand Up @@ -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)
)

Expand Down
2 changes: 1 addition & 1 deletion custom_components/sat/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
8 changes: 6 additions & 2 deletions custom_components/sat/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)
8 changes: 6 additions & 2 deletions custom_components/sat/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)

0 comments on commit 2ea8fd0

Please sign in to comment.