11import logging
2- from datetime import timedelta
32import math
43
54import voluptuous as vol
4847from . import (
4948 calculate_continuous_times ,
5049 calculate_intermittent_times ,
51- compare_config ,
50+ compare_config_to_attributes ,
5251 create_weighting ,
5352 extract_config ,
5453 get_fixed_applicable_time_periods ,
@@ -275,11 +274,13 @@ async def async_added_to_hass(self):
275274 state .attributes ,
276275 []
277276 )
277+ self .update_default_attributes ()
278278
279279 self ._target_timeframes = self ._attributes ["target_times" ] if "target_times" in self ._attributes else []
280280
281281 # Reset everything if our settings have changed
282- if compare_config (self ._config , self ._attributes ) == False :
282+ if compare_config_to_attributes (self .expand_config_attributes (self ._config ), self ._attributes ) == False :
283+ _LOGGER .debug (f'Not restoring target times for { self ._config [CONFIG_TARGET_NAME ]} as attributes have changed' )
283284 self ._state = False
284285 self ._attributes = self ._config .copy ()
285286 self .update_default_attributes ()
@@ -363,29 +364,37 @@ async def async_update_target_timeframe_config(self, target_start_time=None, tar
363364 data = new_config_data
364365 )
365366
366- def update_default_attributes (self ):
367- """Update the default attributes."""
368- self ._attributes ["data_source_id" ] = self ._data_source_id
367+ def expand_config_attributes (self , attributes : dict ):
368+ new_attributes = attributes .copy ()
369369
370370 is_rolling_target = True
371- if CONFIG_TARGET_ROLLING_TARGET in self . _config :
372- is_rolling_target = self . _config [CONFIG_TARGET_ROLLING_TARGET ]
373- self . _attributes [CONFIG_TARGET_ROLLING_TARGET ] = is_rolling_target
371+ if CONFIG_TARGET_ROLLING_TARGET in new_attributes :
372+ is_rolling_target = new_attributes [CONFIG_TARGET_ROLLING_TARGET ]
373+ new_attributes [CONFIG_TARGET_ROLLING_TARGET ] = is_rolling_target
374374
375375 find_last_rates = False
376- if CONFIG_TARGET_LATEST_VALUES in self ._config :
377- find_last_rates = self ._config [CONFIG_TARGET_LATEST_VALUES ]
378- self ._attributes [CONFIG_TARGET_LATEST_VALUES ] = find_last_rates
379-
380- calculate_with_incomplete_data = False
381- if CONFIG_TARGET_DANGEROUS_SETTINGS in self ._config and CONFIG_TARGET_CALCULATE_WITH_INCOMPLETE_DATA in self ._config [CONFIG_TARGET_DANGEROUS_SETTINGS ]:
382- calculate_with_incomplete_data = self ._config [CONFIG_TARGET_DANGEROUS_SETTINGS ][CONFIG_TARGET_CALCULATE_WITH_INCOMPLETE_DATA ]
383- self ._attributes [CONFIG_TARGET_CALCULATE_WITH_INCOMPLETE_DATA ] = calculate_with_incomplete_data
384-
385- minimum_required_minutes_in_slot = CONFIG_TARGET_DEFAULT_MINIMUM_REQUIRED_MINUTES_IN_SLOT
386- if CONFIG_TARGET_DANGEROUS_SETTINGS in self ._config and CONFIG_TARGET_MINIMUM_REQUIRED_MINUTES_IN_SLOT in self ._config [CONFIG_TARGET_DANGEROUS_SETTINGS ]:
387- minimum_required_minutes_in_slot = self ._config [CONFIG_TARGET_DANGEROUS_SETTINGS ][CONFIG_TARGET_MINIMUM_REQUIRED_MINUTES_IN_SLOT ]
388- self ._attributes [CONFIG_TARGET_MINIMUM_REQUIRED_MINUTES_IN_SLOT ] = minimum_required_minutes_in_slot
389-
390- if CONFIG_TARGET_DANGEROUS_SETTINGS in self ._attributes :
391- del self ._attributes [CONFIG_TARGET_DANGEROUS_SETTINGS ]
376+ if CONFIG_TARGET_LATEST_VALUES in new_attributes :
377+ find_last_rates = new_attributes [CONFIG_TARGET_LATEST_VALUES ]
378+ new_attributes [CONFIG_TARGET_LATEST_VALUES ] = find_last_rates
379+
380+ if CONFIG_TARGET_CALCULATE_WITH_INCOMPLETE_DATA not in new_attributes :
381+ calculate_with_incomplete_data = False
382+ if CONFIG_TARGET_DANGEROUS_SETTINGS in new_attributes and CONFIG_TARGET_CALCULATE_WITH_INCOMPLETE_DATA in new_attributes [CONFIG_TARGET_DANGEROUS_SETTINGS ]:
383+ calculate_with_incomplete_data = new_attributes [CONFIG_TARGET_DANGEROUS_SETTINGS ][CONFIG_TARGET_CALCULATE_WITH_INCOMPLETE_DATA ]
384+ new_attributes [CONFIG_TARGET_CALCULATE_WITH_INCOMPLETE_DATA ] = calculate_with_incomplete_data
385+
386+ if CONFIG_TARGET_MINIMUM_REQUIRED_MINUTES_IN_SLOT not in new_attributes :
387+ minimum_required_minutes_in_slot = CONFIG_TARGET_DEFAULT_MINIMUM_REQUIRED_MINUTES_IN_SLOT
388+ if CONFIG_TARGET_DANGEROUS_SETTINGS in new_attributes and CONFIG_TARGET_MINIMUM_REQUIRED_MINUTES_IN_SLOT in new_attributes [CONFIG_TARGET_DANGEROUS_SETTINGS ]:
389+ minimum_required_minutes_in_slot = new_attributes [CONFIG_TARGET_DANGEROUS_SETTINGS ][CONFIG_TARGET_MINIMUM_REQUIRED_MINUTES_IN_SLOT ]
390+ new_attributes [CONFIG_TARGET_MINIMUM_REQUIRED_MINUTES_IN_SLOT ] = minimum_required_minutes_in_slot
391+
392+ if CONFIG_TARGET_DANGEROUS_SETTINGS in new_attributes :
393+ del new_attributes [CONFIG_TARGET_DANGEROUS_SETTINGS ]
394+
395+ return new_attributes
396+
397+ def update_default_attributes (self ):
398+ """Update the default attributes."""
399+ self ._attributes ["data_source_id" ] = self ._data_source_id
400+ self ._attributes = self .expand_config_attributes (self ._attributes )
0 commit comments