1+ """Base abstractions for pyscript decorators and decorator managers."""
2+
13from __future__ import annotations
24
3- import logging
45from abc import ABC , abstractmethod
5- from dataclasses import field , dataclass
6+ from dataclasses import dataclass , field
67from enum import StrEnum
7- from typing import ClassVar , Any , TypeVar , Type , final
8+ import logging
9+ from typing import Any , ClassVar , final
810
911import voluptuous as vol
12+
1013from homeassistant .core import Context , HomeAssistant
1114
1215from . import trigger
@@ -109,6 +112,7 @@ async def stop(self):
109112 """Stop the decorator."""
110113
111114 def __repr__ (self ):
115+ """Represent the decorator as a string with the decorator name and arguments."""
112116 parts = []
113117 if self .raw_args is not None :
114118 parts .append ("," .join (map (str , self .raw_args )))
@@ -117,15 +121,13 @@ def __repr__(self):
117121 return f"@{ self .name } ({ ', ' .join (parts )} )"
118122
119123
120- DecoratorType = TypeVar ("DecoratorType" , bound = Decorator )
121-
122-
123124class DecoratorManager (ABC ):
124- """Maintain and validate a set of decorators"""
125+ """Maintain and validate a set of decorators. """
125126
126127 hass : ClassVar [HomeAssistant ]
127128
128129 def __init__ (self , ast_ctx : AstEval , name : str ) -> None :
130+ """Initialize the manager."""
129131 self .ast_ctx = ast_ctx
130132 self .name = name
131133 self .func_name = name .split ("." )[- 1 ]
@@ -154,7 +156,7 @@ def add(self, decorator: Decorator) -> None:
154156 self ._decorators .append (decorator )
155157 decorator .dm = self
156158
157- def get_decorators (self , decorator_type : Type [ DecoratorType ] | None = None ) -> list [DecoratorType ]:
159+ def get_decorators [ DT ] (self , decorator_type : type [ DT ] | None = None ) -> list [DT ]: # noqa: D102
158160 """Get decorators of a specific type."""
159161 if decorator_type is None :
160162 return self ._decorators .copy ()
@@ -221,16 +223,18 @@ async def stop(self):
221223
222224 @abstractmethod
223225 async def dispatch (self , data : DispatchData ) -> None :
224- pass
226+ """Dispatch a trigger call."""
225227
226228 def __repr__ (self ):
229+ """Return a string representation of the manager with status and decorators."""
227230 return f"{ self .__class__ .__name__ } ({ self .status } ) { self ._decorators } for { self .name } ()>"
228231
229232
230233class TriggerDecorator (Decorator , ABC ):
231234 """Base class for trigger-based decorators."""
232235
233236 def __init_subclass__ (cls , ** kwargs ):
237+ """Initialize the decorator class."""
234238 super ().__init_subclass__ (** kwargs )
235239 # kwargs for all triggers
236240 if "kwargs" not in cls .kwargs_schema .schema .keys ():
@@ -280,7 +284,6 @@ class CallHandlerDecorator(Decorator, ABC):
280284 @abstractmethod
281285 async def handle_call (self , data : DispatchData ) -> bool | None :
282286 """Handle an action call. Return False for stop calling."""
283- pass
284287
285288
286289class CallResultHandlerDecorator (Decorator , ABC ):
@@ -289,4 +292,3 @@ class CallResultHandlerDecorator(Decorator, ABC):
289292 @abstractmethod
290293 async def handle_call_result (self , data : DispatchData , result : Any ) -> None :
291294 """Handle an action call result."""
292- pass
0 commit comments