Skip to content

Commit

Permalink
Cleaning up and added some sanity checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexwijn committed May 16, 2023
1 parent e177ae6 commit ac0675d
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 21 deletions.
16 changes: 9 additions & 7 deletions custom_components/sat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ async def async_setup_entry(_hass: HomeAssistant, _entry: ConfigEntry):
This function is called by Home Assistant when the integration is set up with the UI.
"""
if _hass.data.get(DOMAIN) is None:
_hass.data.setdefault(DOMAIN, {})

# Create a new dictionary
_hass.data[DOMAIN] = {_entry.entry_id: {}}
_hass.data[DOMAIN][_entry.entry_id] = {}

# Create a new config store for this entry and initialize it
_hass.data[DOMAIN][_entry.entry_id][CONFIG_STORE] = store = SatConfigStore(_hass, _entry)
Expand All @@ -47,9 +50,6 @@ async def async_setup_entry(_hass: HomeAssistant, _entry: ConfigEntry):
await _hass.async_add_job(_hass.config_entries.async_forward_entry_setup(_entry, CLIMATE_DOMAIN))
await _hass.async_add_job(_hass.config_entries.async_forward_entry_setups(_entry, [SENSOR_DOMAIN, NUMBER_DOMAIN, BINARY_SENSOR_DOMAIN]))

# Add an update listener for this entry
_entry.async_on_unload(_entry.add_update_listener(async_reload_entry))

return True


Expand All @@ -59,11 +59,13 @@ async def async_unload_entry(_hass: HomeAssistant, _entry: ConfigEntry) -> bool:
This function is called by Home Assistant when the integration is being removed.
"""
# Unload the entry and its dependent components

climate = _hass.data[DOMAIN][_entry.entry_id][CLIMATE]
await _hass.data[DOMAIN][_entry.entry_id][COORDINATOR].async_will_remove_from_hass(climate)

unloaded = all(
await asyncio.gather(
_hass.data[DOMAIN][_entry.entry_id][COORDINATOR].async_will_remove_from_hass(_hass.data[DOMAIN][_entry.entry_id][CLIMATE]),
_hass.config_entries.async_unload_platforms(_entry, [CLIMATE, SENSOR_DOMAIN, NUMBER_DOMAIN, BINARY_SENSOR_DOMAIN]),
_hass.config_entries.async_unload_platforms(_entry, [CLIMATE_DOMAIN, SENSOR_DOMAIN, NUMBER_DOMAIN, BINARY_SENSOR_DOMAIN]),
)
)

Expand Down
19 changes: 10 additions & 9 deletions custom_components/sat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ async def reset_integral(_call: ServiceCall):
self.hass.services.async_register(
DOMAIN,
SERVICE_SET_OVERSHOOT_PROTECTION_VALUE,
partial(set_overshoot_protection_value, self._coordinator)
partial(set_overshoot_protection_value, self._coordinator, self)
)

async def track_sensor_temperature(self, entity_id):
Expand Down Expand Up @@ -561,12 +561,12 @@ def pulse_width_modulation_enabled(self) -> bool:
if not self._coordinator.supports_setpoint_management or self._force_pulse_width_modulation:
return True

return self._overshoot_protection and self._calculate_control_setpoint() < (self._store.get(STORAGE_OVERSHOOT_PROTECTION_VALUE) - 2)
return self._overshoot_protection and self._calculate_control_setpoint() < (self._coordinator.minimum_setpoint - 2)

@property
def relative_modulation_enabled(self):
"""Return True if relative modulation is enabled, False otherwise."""
if self._setpoint is None or not self._coordinator.support_relative_modulation_management:
if not self._coordinator.support_relative_modulation_management:
return False

if self._coordinator.hot_water_active:
Expand Down Expand Up @@ -821,13 +821,14 @@ async def _async_control_pid(self, reset: bool = False):
async def _async_control_setpoint(self, pwm_state: PWMState):
"""Control the setpoint of the heating system."""
if self.hvac_mode == HVACMode.HEAT:
if self.pulse_width_modulation_enabled and pwm_state != pwm_state.IDLE:
self._setpoint = self._coordinator.minimum_setpoint if pwm_state == pwm_state.ON else MINIMUM_SETPOINT
_LOGGER.info(f"Running pulse width modulation cycle: {pwm_state}")
else:
self._outputs.append(self._calculate_control_setpoint())
self._outputs.append(self._calculate_control_setpoint())

if not self.pulse_width_modulation_enabled or pwm_state == pwm_state.IDLE:
_LOGGER.info("Running Normal cycle")
self._setpoint = mean(list(self._outputs)[-5:])
_LOGGER.info("Running normal cycle")
else:
_LOGGER.info(f"Running PWM cycle: {pwm_state}")
self._setpoint = self._coordinator.minimum_setpoint if pwm_state == pwm_state.ON else MINIMUM_SETPOINT
else:
self._outputs.clear()
self._setpoint = MINIMUM_SETPOINT
Expand Down
2 changes: 1 addition & 1 deletion custom_components/sat/const.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Base component constants
NAME = "Smart Autotune Thermostat"
DOMAIN = "sat"
VERSION = "2.3.0"
VERSION = "2.3.x"
CLIMATE = "climate"
COORDINATOR = "coordinator"
CONFIG_STORE = "config_store"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/sat/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
_LOGGER: logging.Logger = logging.getLogger(__name__)


class DeviceState(Enum):
class DeviceState(str, Enum):
ON = "on"
OFF = "off"

Expand Down
2 changes: 1 addition & 1 deletion custom_components/sat/pwm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ON_TIME_80_PERCENT = 900


class PWMState(Enum):
class PWMState(str, Enum):
ON = "on"
OFF = "off"
IDLE = "idle"
Expand Down
3 changes: 2 additions & 1 deletion custom_components/sat/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
from .climate import SatClimate


async def set_overshoot_protection_value(coordinator: SatDataUpdateCoordinator, call: ServiceCall):
async def set_overshoot_protection_value(coordinator: SatDataUpdateCoordinator, climate: SatClimate, call: ServiceCall):
"""Service to set the overshoot protection value."""
coordinator.store.update(STORAGE_OVERSHOOT_PROTECTION_VALUE, call.data.get("value"))
climate.async_write_ha_state()


async def start_overshoot_protection_calculation(coordinator: SatDataUpdateCoordinator, climate: SatClimate, call: ServiceCall):
Expand Down
5 changes: 4 additions & 1 deletion custom_components/sat/switch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ def maximum_setpoint(self) -> float:

@property
def device_active(self) -> bool:
return self.hass.states.get(self._entity_id).state == STATE_ON
if (state := self.hass.states.get(self._entity_id)) is None:
return False

return state.state == STATE_ON

async def async_set_heater_state(self, state: DeviceState) -> None:
if not self._simulation:
Expand Down

0 comments on commit ac0675d

Please sign in to comment.