File tree Expand file tree Collapse file tree 3 files changed +18
-13
lines changed
Expand file tree Collapse file tree 3 files changed +18
-13
lines changed Original file line number Diff line number Diff line change 1- from typing import Optional
2-
3-
41class EntityInstanceId :
52 def __init__ (self , entity : str , key : str ):
63 self .entity = entity
@@ -20,7 +17,7 @@ def __lt__(self, other):
2017 return str (self ) < str (other )
2118
2219 @staticmethod
23- def parse (entity_id : str ) -> Optional [ "EntityInstanceId" ] :
20+ def parse (entity_id : str ) -> "EntityInstanceId" :
2421 """Parse a string representation of an entity ID into an EntityInstanceId object.
2522
2623 Parameters
@@ -30,8 +27,13 @@ def parse(entity_id: str) -> Optional["EntityInstanceId"]:
3027
3128 Returns
3229 -------
33- Optional[EntityInstanceId]
34- The parsed EntityInstanceId object, or None if the input is None.
30+ EntityInstanceId
31+ The parsed EntityInstanceId object.
32+
33+ Raises
34+ ------
35+ ValueError
36+ If the input string is not in the correct format.
3537 """
3638 try :
3739 _ , entity , key = entity_id .split ("@" , 2 )
Original file line number Diff line number Diff line change @@ -44,8 +44,9 @@ def __init__(self,
4444
4545 @staticmethod
4646 def from_entity_response (entity_response : pb .GetEntityResponse , includes_state : bool ):
47- entity_id = EntityInstanceId .parse (entity_response .entity .instanceId )
48- if not entity_id :
47+ try :
48+ entity_id = EntityInstanceId .parse (entity_response .entity .instanceId )
49+ except ValueError :
4950 raise ValueError ("Invalid entity instance ID in entity response." )
5051 entity_state = None
5152 if includes_state :
Original file line number Diff line number Diff line change @@ -750,9 +750,10 @@ def _execute_entity_batch(
750750 for operation in req .operations :
751751 start_time = datetime .now (timezone .utc )
752752 executor = _EntityExecutor (self ._registry , self ._logger )
753- entity_instance_id = EntityInstanceId .parse (instance_id )
754- if not entity_instance_id :
755- raise RuntimeError (f"Invalid entity instance ID '{ operation .requestId } ' in entity operation request." )
753+ try :
754+ entity_instance_id = EntityInstanceId .parse (instance_id )
755+ except ValueError :
756+ raise RuntimeError (f"Invalid entity instance ID '{ instance_id } ' in entity operation request." )
756757
757758 operation_result = None
758759
@@ -1656,8 +1657,9 @@ def process_event(
16561657 raise _get_wrong_action_type_error (
16571658 entity_call_id , expected_method_name , action
16581659 )
1659- entity_id = EntityInstanceId .parse (event .entityOperationCalled .targetInstanceId .value )
1660- if not entity_id :
1660+ try :
1661+ entity_id = EntityInstanceId .parse (event .entityOperationCalled .targetInstanceId .value )
1662+ except ValueError :
16611663 raise RuntimeError (f"Could not parse entity ID from targetInstanceId '{ event .entityOperationCalled .targetInstanceId .value } '" )
16621664 ctx ._entity_task_id_map [event .entityOperationCalled .requestId ] = (entity_id , entity_call_id )
16631665 elif event .HasField ("entityOperationSignaled" ):
You can’t perform that action at this time.
0 commit comments