OK, this a mini example of what I try to do.
I open connections to pyatv, aiohue, … and receives data from them. If I don’t close the open streams, I will get multiple streams since each reload of a rule opens a new connection.
Code:
import HABApp
async def connect():
return 'connect to SSE stream'
async def disconnect():
return 'disconnect from SSE stream'
class CleanupTest(HABApp.Rule):
def __init__(self):
super().__init__()
self.resource = None
self.run.soon(self.connect)
def on_rule_removed(self):
self.run.soon(self.disconnect)
async def connect(self):
self.resource = await connect()
async def disconnect(self):
if self.resource is not None:
self.resource = await disconnect()
CleanupTest()
Log:
2022-07-27 11:06:02.883 [ERROR] [HABApp.Rule ] - Error "'NoneType' object has no attribute 'rule_name'" in unload_rule:
2022-07-27 11:06:02.886 [ERROR] [HABApp.Rule ] - Error while formatting traceback: Expected one value, found 0
2022-07-27 11:06:02.887 [ERROR] [HABApp.Rule ] - Traceback (most recent call last):
2022-07-27 11:06:02.888 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/HABApp/rule_ctx/rule_ctx.py", line 52, in unload_rule
2022-07-27 11:06:02.888 [ERROR] [HABApp.Rule ] - rule.on_rule_removed()
2022-07-27 11:06:02.889 [ERROR] [HABApp.Rule ] - File "/etc/openhab/habapp/rules/CleanupTest.py", line 17, in on_rule_removed
2022-07-27 11:06:02.892 [ERROR] [HABApp.Rule ] - self.run.soon(self.disconnect)
2022-07-27 11:06:02.893 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/HABApp/rule/scheduler/habappschedulerview.py", line 80, in soon
2022-07-27 11:06:02.893 [ERROR] [HABApp.Rule ] - return self.at(None, callback, *args, **kwargs)
2022-07-27 11:06:02.894 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/HABApp/rule/scheduler/habappschedulerview.py", line 25, in at
2022-07-27 11:06:02.894 [ERROR] [HABApp.Rule ] - callback = wrap_func(callback, context=self._habapp_rule_ctx)
2022-07-27 11:06:02.895 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/HABApp/core/internals/wrapped_function/wrapper.py", line 21, in wrap_func
2022-07-27 11:06:02.896 [ERROR] [HABApp.Rule ] - return WrappedAsyncFunction(func, name=name, logger=logger, context=context)
2022-07-27 11:06:02.897 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/HABApp/core/internals/wrapped_function/wrapped_async.py", line 17, in __init__
2022-07-27 11:06:02.897 [ERROR] [HABApp.Rule ] - super(WrappedAsyncFunction, self).__init__(name=name, func=func, logger=logger, context=context)
2022-07-27 11:06:02.898 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/HABApp/core/internals/wrapped_function/base.py", line 25, in __init__
2022-07-27 11:06:02.898 [ERROR] [HABApp.Rule ] - name = self._habapp_ctx.get_callback_name(func)
2022-07-27 11:06:02.899 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/HABApp/rule_ctx/rule_ctx.py", line 22, in get_callback_name
2022-07-27 11:06:02.911 [ERROR] [HABApp.Rule ] - return f'{self.rule.rule_name}.{callback.__name__}' if self.rule.rule_name else None
2022-07-27 11:06:02.912 [ERROR] [HABApp.Rule ] - AttributeError: 'NoneType' object has no attribute 'rule_name'
2022-07-27 11:06:02.913 [ERROR] [HABApp.Rule ] -
2022-07-27 11:06:02.913 [ERROR] [HABApp.Rule ] - During handling of the above exception, another exception occurred:
2022-07-27 11:06:02.914 [ERROR] [HABApp.Rule ] -
2022-07-27 11:06:02.917 [ERROR] [HABApp.Rule ] - Traceback (most recent call last):
2022-07-27 11:06:02.918 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/HABApp/core/lib/exceptions/format.py", line 46, in format_exception
2022-07-27 11:06:02.918 [ERROR] [HABApp.Rule ] - added = format_frame_info(tb, frame_info, is_last=i == last_frame)
2022-07-27 11:06:02.919 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/HABApp/core/lib/exceptions/format_frame.py", line 46, in format_frame_info
2022-07-27 11:06:02.920 [ERROR] [HABApp.Rule ] - if frame_info.lines:
2022-07-27 11:06:02.922 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
2022-07-27 11:06:02.923 [ERROR] [HABApp.Rule ] - value = obj.__dict__[self.func.__name__] = self.func(obj)
2022-07-27 11:06:02.923 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/stack_data/core.py", line 698, in lines
2022-07-27 11:06:02.926 [ERROR] [HABApp.Rule ] - pieces = self.included_pieces
2022-07-27 11:06:02.926 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
2022-07-27 11:06:02.927 [ERROR] [HABApp.Rule ] - value = obj.__dict__[self.func.__name__] = self.func(obj)
2022-07-27 11:06:02.928 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/stack_data/core.py", line 649, in included_pieces
2022-07-27 11:06:02.928 [ERROR] [HABApp.Rule ] - pos = scope_pieces.index(self.executing_piece)
2022-07-27 11:06:02.929 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
2022-07-27 11:06:02.929 [ERROR] [HABApp.Rule ] - value = obj.__dict__[self.func.__name__] = self.func(obj)
2022-07-27 11:06:02.930 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/stack_data/core.py", line 628, in executing_piece
2022-07-27 11:06:02.931 [ERROR] [HABApp.Rule ] - return only(
2022-07-27 11:06:02.931 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/executing/executing.py", line 164, in only
2022-07-27 11:06:02.932 [ERROR] [HABApp.Rule ] - raise NotOneValueFound('Expected one value, found 0')
2022-07-27 11:06:02.933 [ERROR] [HABApp.Rule ] - executing.executing.NotOneValueFound: Expected one value, found 0
2022-07-27 11:06:02.933 [ERROR] [HABApp.Rule ] -
2022-07-27 11:06:02.935 [ERROR] [HABApp.Rule ] - --------------------------------------------------------------------------------
2022-07-27 11:06:02.935 [ERROR] [HABApp.Rule ] - Partial Traceback:
2022-07-27 11:06:02.936 [ERROR] [HABApp.Rule ] - --------------------------------------------------------------------------------
2022-07-27 11:06:02.937 [ERROR] [HABApp.Rule ] - File "/opt/habapp/lib/python3.9/site-packages/HABApp/rule_ctx/rule_ctx.py", line 52 in unload_rule
2022-07-27 11:06:02.937 [ERROR] [HABApp.Rule ] - --------------------------------------------------------------------------------
2022-07-27 11:06:02.938 [ERROR] [HABApp.Rule ] - 32 | def unload_rule(self):
2022-07-27 11:06:02.938 [ERROR] [HABApp.Rule ] - (...)
2022-07-27 11:06:02.939 [ERROR] [HABApp.Rule ] - 48 | self.rule = None
2022-07-27 11:06:02.940 [ERROR] [HABApp.Rule ] - 49 | rule._habapp_rule_ctx = None
2022-07-27 11:06:02.940 [ERROR] [HABApp.Rule ] - 51 | # user implementation
2022-07-27 11:06:02.941 [ERROR] [HABApp.Rule ] - --> 52 | rule.on_rule_removed()
2022-07-27 11:06:02.941 [ERROR] [HABApp.Rule ] - ------------------------------------------------------------
2022-07-27 11:06:02.942 [ERROR] [HABApp.Rule ] - rule = <CleanupTest>
2022-07-27 11:06:02.942 [ERROR] [HABApp.Rule ] - rule._habapp_rule_ctx = None
2022-07-27 11:06:02.943 [ERROR] [HABApp.Rule ] - rule.run = <HABApp.rule.scheduler.habappschedulerview.HABAppSchedulerView object at 0xa7e3d118>
2022-07-27 11:06:02.944 [ERROR] [HABApp.Rule ] - rule.run._habapp_ctx = None
2022-07-27 11:06:02.945 [ERROR] [HABApp.Rule ] - rule.run._scheduler = <HABApp.rule.scheduler.scheduler.HABAppScheduler object at 0xa7e3d580>
2022-07-27 11:06:02.945 [ERROR] [HABApp.Rule ] - self = <HABApp.rule_ctx.rule_ctx.HABAppRuleContext object at 0xa7e3d358>
2022-07-27 11:06:02.946 [ERROR] [HABApp.Rule ] - self.objs = None
2022-07-27 11:06:02.946 [ERROR] [HABApp.Rule ] - self.rule = None
2022-07-27 11:06:02.947 [ERROR] [HABApp.Rule ] - log = <Logger HABApp.Rule (INFO)>
2022-07-27 11:06:02.948 [ERROR] [HABApp.Rule ] - ------------------------------------------------------------
2022-07-27 11:06:02.948 [ERROR] [HABApp.Rule ] -
2022-07-27 11:06:03.068 [INFO ] [HABApp.Rules ] - Added rule "CleanupTest" from rules/CleanupTest.py