Skip to content

Commit 7c0058f

Browse files
authored
Merge pull request #292 from ndbroadbent/delay_and_override_on_startup
Ignore state change events while Home Assistant is starting and initializing entity states, override controllers correctly on startup
2 parents 67c4158 + 7e58810 commit 7c0058f

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

custom_components/entity_controller/__init__.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
111111

112112
_LOGGER = logging.getLogger(__name__)
113113

114+
# Configure delay before starting to monitor state change events
115+
STARTUP_DELAY = 70
116+
114117
devices = []
115118
MODE_SCHEMA = vol.Schema(
116119
{
@@ -181,17 +184,22 @@ async def async_setup(hass, config):
181184

182185
machine = Machine(
183186
states=STATES,
184-
initial="idle",
187+
initial="pending",
185188
# title=self.name+" State Diagram",
186189
# show_conditions=True
187190
# show_auto_transitions = True,
188191
finalize_event="finalize",
189192
)
193+
machine.add_transition(
194+
trigger="start_monitoring",
195+
source="pending",
196+
dest="idle",
197+
)
190198

191199
machine.add_transition(trigger="constrain", source="*", dest="constrained")
192200
machine.add_transition(
193201
trigger="override",
194-
source=["idle", "active_timer", "blocked"],
202+
source=["pending", "idle", "active_timer", "blocked"],
195203
dest="overridden",
196204
)
197205

@@ -485,6 +493,8 @@ class Model:
485493
""" Represents the transitions state machine model """
486494

487495
def __init__(self, hass, config, machine, entity):
496+
self.ec_startup_time = datetime.now()
497+
488498
self.hass = hass # backwards reference to hass object
489499
self.entity = entity # backwards reference to entity containing this model
490500

@@ -531,6 +541,11 @@ def __init__(self, hass, config, machine, entity):
531541
machine.add_model(
532542
self
533543
) # add here because machine generated methods are being used in methods below.
544+
545+
event.async_call_later(self.hass, STARTUP_DELAY, self.startup_delay_callback)
546+
547+
def startup_delay_callback(self, evt):
548+
config = self.config
534549
self.config_static_strings(config)
535550
self.config_control_entities(config)
536551
self.config_state_entities(
@@ -550,6 +565,12 @@ def __init__(self, hass, config, machine, entity):
550565
self.config_other(config)
551566
self.prepare_service_data()
552567

568+
if len(self.overrideEntities) > 0 and self.is_override_state_on():
569+
self.override()
570+
self.update(overridden_at=str(datetime.now()))
571+
else:
572+
self.start_monitoring()
573+
553574
def update(self, wait=False, **kwargs):
554575
""" Called from different methods to report a state attribute change """
555576
# self.log.debug("Update called with {}".format(str(kwargs)))

custom_components/entity_controller/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
MODE_NIGHT = 'night'
8282
CONSTRAIN_START = 1
8383
CONSTRAIN_END = 2
84-
STATES = ['idle', 'overridden', 'constrained', 'blocked',
84+
STATES = ['pending', 'idle', 'overridden', 'constrained', 'blocked',
8585
{'name': 'active', 'children': ['timer', 'stay_on'],
8686
'initial': False}]
8787
CONF_IGNORE_STATE_CHANGES_UNTIL = "grace_period"

0 commit comments

Comments
 (0)