SmartObject now create its linked entity with create_entity

This commit is contained in:
Pierre committed 2026-10-11 11:28:20 +02:00
1 parent 97501875b8
commit bf72dbb223
1 file changed
+10 -9
+10 -9
View File
@@ -113,12 +113,11 @@ class SmartObject(hass.Hass,LoggerInterface):
if device_class is not None: attributes['device_class'] = device_class if device_class is not None: attributes['device_class'] = device_class
if state_class is not None: attributes['state_class'] = state_class if state_class is not None: attributes['state_class'] = state_class
if state is not None: if state is None:
state = entity.get_state() if entity.exists() else None
if state is None:
state = 'unknown'
entity.set_state(state=state, attributes=attributes, check_existence=False) entity.set_state(state=state, attributes=attributes, check_existence=False)
else:
if not entity.exists():
entity.set_state(state='unknown', check_existence=False)
entity.set_state(attributes=attributes)
return entity return entity
@@ -219,14 +218,16 @@ class SmartObject(hass.Hass,LoggerInterface):
# entity: <entity_id> → link app to a HA entity # entity: <entity_id> → link app to a HA entity
if "entity" in self.args: if "entity" in self.args:
self.entity_id = self.args["entity"] self.entity_id = self.args["entity"]
self.entity = self.get_entity(self.entity_id) self.entity = self.get_entity(self.entity_id, check_existence=False)
initial_state = None
if self.entity.exists(): if self.entity.exists():
self.log_info(f"Linked to {self.entity_id}") self.log_info(f"Linked to {self.entity_id}")
elif 'default_entity_state' in self.args: elif 'default_entity_state' in self.args:
self.entity.set_state(state = self.args["default_entity_state"]) initial_state = self.args["default_entity_state"]
self.log_info(f"Creating {self.entity_id}, default_state = {self.args['default_entity_state']}") self.log_info(f"Creating {self.entity_id}, default_state = {initial_state}")
else: else:
self.entity.set_state(state = self.get_default_entity_state()) initial_state = self.get_default_entity_state()
self.entity = self.create_entity(self.entity_id, state=initial_state)
self.constants['self'] = self.entity_id self.constants['self'] = self.entity_id
else: else: