33from collections import defaultdict
44from collections .abc import Callable , Iterable
55from dataclasses import dataclass , field
6- from datetime import timedelta
6+ from datetime import datetime , timedelta
77from logging import Logger
88from typing import TYPE_CHECKING , Any , overload
99
1010from appdaemon import utils
1111
1212from .exceptions import TimeOutException
13- from .state import StateCallback
13+ from .state import StateCallbackType
1414
1515if TYPE_CHECKING :
1616 from appdaemon import ADAPI
@@ -28,8 +28,6 @@ class Entity:
2828 name : str = field (init = False )
2929
3030 adapi : "ADAPI"
31- namespace : str
32- entity_id : str | None
3331 _async_events : dict [str , asyncio .Event ] = field (default_factory = lambda : defaultdict (asyncio .Event ))
3432 # states_attrs = EntityAttrs()
3533
@@ -115,7 +113,7 @@ async def set_state(
115113
116114 """
117115 self .logger .debug ("set state: %s, %s from %s" , self .entity_id , kwargs , self .name )
118- return await self .adapi .set_state (
116+ return await self .adapi .set_state ( # pyright: ignore[reportGeneralTypeIssues]
119117 entity_id = self .entity_id ,
120118 namespace = self .namespace ,
121119 state = state ,
@@ -175,7 +173,7 @@ async def get_state(
175173
176174 """
177175 self .logger .debug ("get state: %s, %s from %s" , self .entity_id , self .namespace , self .name )
178- return await self .adapi .get_state (
176+ return await self .adapi .get_state ( # pyright: ignore[reportGeneralTypeIssues]
179177 namespace = self .namespace ,
180178 entity_id = self .entity_id ,
181179 attribute = attribute ,
@@ -187,7 +185,7 @@ async def get_state(
187185 @utils .sync_decorator
188186 async def listen_state (
189187 self ,
190- callback : StateCallback ,
188+ callback : StateCallbackType ,
191189 new : str | Callable [[Any ], bool ] | None = None ,
192190 old : str | Callable [[Any ], bool ] | None = None ,
193191 duration : str | int | float | timedelta | None = None ,
@@ -197,10 +195,11 @@ async def listen_state(
197195 oneshot : bool = False ,
198196 pin : bool | None = None ,
199197 pin_thread : int | None = None ,
198+ ** kwargs : Any ,
200199 ) -> str : ...
201200
202201 @utils .sync_decorator
203- async def listen_state (self , callback : StateCallback , ** kwargs : Any ) -> str :
202+ async def listen_state (self , callback : StateCallbackType , ** kwargs : Any ) -> str :
204203 """Registers a callback to react to state changes.
205204
206205 This function allows the user to register a callback for a wide variety of state changes.
@@ -301,15 +300,15 @@ async def listen_state(self, callback: StateCallback, **kwargs: Any) -> str:
301300
302301 >>> self.handle = self.my_entity.listen_state(self.my_callback, new="on", duration=60, immediate=True)
303302 """
304- return await self .adapi .listen_state (
303+ return await self .adapi .listen_state ( # pyright: ignore[reportGeneralTypeIssues]
305304 callback ,
306305 entity_id = self .entity_id ,
307306 namespace = self .namespace ,
308307 ** kwargs ,
309308 )
310309
311310 @utils .sync_decorator
312- async def add (self , state : str | int | float = None , attributes : dict = None ) -> None :
311+ async def add (self , state : str | int | float | None = None , attributes : dict [ str , Any ] | None = None ) -> None :
313312 """Adds a non-existent entity, by creating it within a namespaces.
314313
315314 It should be noted that this api call, is mainly for creating AD internal entities.
@@ -383,9 +382,9 @@ async def call_service(
383382 async def wait_state (
384383 self ,
385384 state : Any ,
386- attribute : str | int = None ,
385+ attribute : str | None = None ,
387386 duration : int | float = 0 ,
388- timeout : int | float = None ,
387+ timeout : int | float | None = None ,
389388 ) -> None :
390389 """Used to wait for the state of an entity's attribute
391390
@@ -419,7 +418,7 @@ async def wait_state(
419418 async_event = self ._async_events [wait_id ]
420419
421420 try :
422- handle = await self .listen_state (
421+ handle = await self .listen_state ( # pyright: ignore[reportGeneralTypeIssues]
423422 self .entity_state_changed ,
424423 new = state ,
425424 attribute = attribute ,
@@ -556,7 +555,7 @@ def entity_id(self) -> str:
556555 return self ._entity_id
557556
558557 @entity_id .setter
559- def entity_id (self , new : str ) -> str :
558+ def entity_id (self , new : str ) -> None :
560559 """Get the entity's entity_id"""
561560 self ._entity_id = new
562561 try :
@@ -574,7 +573,6 @@ def state(self) -> Any:
574573 @property
575574 def namespace (self ) -> str :
576575 """Get the entity's namespace name"""
577-
578576 return self ._namespace
579577
580578 @namespace .setter
@@ -592,15 +590,15 @@ def friendly_name(self) -> str:
592590 return self .attributes .get ("friendly_name" , self .entity_id )
593591
594592 @property
595- def last_changed (self ) -> str :
593+ def last_changed (self ) -> str | None :
596594 """Get the entity's last changed time in iso format"""
597595 return self ._simple_state .get ("last_changed" )
598596
599597 @property
600598 def last_changed_delta (self ) -> timedelta | None :
601599 """A timedelta object representing the time since the entity was last changed"""
602600 if time_str := self .last_changed :
603- compare = self . adapi . parse_datetime (time_str , aware = True )
601+ compare = datetime . fromisoformat (time_str )
604602 now = self .AD .sched .get_now_sync ()
605603 return (now - compare )
606604
0 commit comments