You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometime action can't be completed and we need sort of cleanup code, which can be run after any action.
In my case (for irrigation automation) : confirm the valve is closed and closed it if its not.
Idea to solve:
Ability to issue special "from any state" transition to the cleanup handling final state. Specify the error handler in FSM configuration. Specify which states can be handled by the handler.
The current workaround
# the decorator to enter functions which could be handleddefcatch_errors(handler=None):
defactual_decorator(func):
@wraps(func)asyncdef_wrapper(*args, **kwargs):
try:
returnawaitfunc(*args, **kwargs)
exceptExceptionase:
ifhandler:
returnawaithandler(*args, **kwargs)
returnNonereturn_wrapperreturnactual_decoratorclassWorkItem(StateMachine):
created=State('created', initial=True)
open=State('open')
opened=State('opened')
close=State('close')
closed=State('closed', final=True)
error=State('error', final=True)
do_work= (created.to(open) |open.to(opened, cond="is_open") |opened.to(close, cond="can_close") |close.to(closed, cond="is_closed"))
# the error transition mapdo_error= (created.to(error) |open.to(error) |opened.to(error) |close.to(error))
# this will be sent if error (e.g. exception in enter handler occur)asyncdefon_error(self, *args, **kwargs):
awaitself.async_send("do_error")
# the decorated handler@catch_errors(handler=on_error)asyncdefon_enter_created(self):
self._app.log(f"Entering 'created' state.")
ifnotself.is_closed():
asyncwithasyncio.timeout(30):
self._app.error(f"Valve {self.zone.valve} is already open, closing it")
# this will throw exceptionawaitself._app.call_service('homeassistant/turn_off1', entity_id=self.zone.valve)
# cleanup handlerasyncdefon_enter_error(self):
self._app.error(f"Error in {self.zone.valve}{self.zone.moisture}")
try:
asyncwithasyncio.timeout(60):
self._app.log(f"Closing valve {self.zone.valve} because of error")
awaitself._app.call_service('homeassistant/turn_off', entity_id=self.zone.valve)
whilenotself.is_closed():
awaitasyncio.sleep(1)
finally:
self._app.error(
f"Error in {self.zone.valve}{self.zone.moisture} the valve status is {self.zone.get_valve_state()}")
The text was updated successfully, but these errors were encountered:
Hi @AlexMKX , how are you? Thanks for your suggestion. I think that in the end this relates to #386, as an alternative implementation solving the same issue.
Do you believe that some kind of a "finalize event handler" may solve your issue as well?
Description
Sometime action can't be completed and we need sort of cleanup code, which can be run after any action.
In my case (for irrigation automation) : confirm the valve is closed and closed it if its not.
Idea to solve:
Ability to issue special "from any state" transition to the cleanup handling final state. Specify the error handler in FSM configuration. Specify which states can be handled by the handler.
The current workaround
The text was updated successfully, but these errors were encountered: