feat: add app_name parameter to VirtualSensors and SmartObject for per app grouping in the dashboard

This commit is contained in:
2026-05-10 22:41:03 +02:00
parent 875e0abd37
commit 05a910ba80
2 changed files with 20 additions and 35 deletions

View File

@@ -105,13 +105,6 @@ class SmartObject(hass.Hass,LoggerInterface):
else: else:
attributes = dict() attributes = dict()
# elif state is not None and entity.exists():
# state_data = self.get_state(entity_id, attribute='all') or {}
# current_attributes = state_data.get('attributes', {})
# if current_attributes.get('ad_app') != self.name:
# attributes = dict(current_attributes)
# attributes['ad_app'] = self.name
attributes['ad_app'] = self.name attributes['ad_app'] = self.name
if friendly_name is not None: attributes['friendly_name'] = friendly_name if friendly_name is not None: attributes['friendly_name'] = friendly_name
@@ -123,19 +116,10 @@ class SmartObject(hass.Hass,LoggerInterface):
if state is not None: if state is not None:
entity.set_state(state=state, attributes=attributes) entity.set_state(state=state, attributes=attributes)
else: else:
entity.set_state(state='unknown', attributes=attributes) if not entity.exists():
entity.set_state(state='unknown')
entity.set_state(attributes=attributes)
# if state is not None and attributes is not None:
# self.set_state(entity_id, state=state, attributes=attributes)
# elif state is not None:
# if entity.exists():
# self.set_state(entity_id, state=state)
# else:
# self.set_state(entity_id, state=state, attributes={'ad_app': self.name})
# elif attributes is not None:
# self.set_state(entity_id, attributes=attributes)
# elif not entity.exists():
# self.set_state(entity_id, state='unknown', attributes={'ad_app': self.name})
return entity return entity
# Override this in a subclass to return a default state string when the # Override this in a subclass to return a default state string when the
@@ -238,7 +222,7 @@ class SmartObject(hass.Hass,LoggerInterface):
# virtual_sensors: {...} → create derived sensors from YAML spec # virtual_sensors: {...} → create derived sensors from YAML spec
if 'virtual_sensors' in self.args: if 'virtual_sensors' in self.args:
self.virtual_sensors = VirtualSensors(ad_api = self,logger_interface = self,super_entity_id = self.entity_id,yaml_block = self.args['virtual_sensors'],templates_library = self.templates_library,constants = self.constants) self.virtual_sensors = VirtualSensors(ad_api = self,logger_interface = self,super_entity_id = self.entity_id,yaml_block = self.args['virtual_sensors'],templates_library = self.templates_library,constants = self.constants,app_name = self.name)
# attributes_override: {attr: entity_id | static_value, ...} # attributes_override: {attr: entity_id | static_value, ...}
# Each attribute either mirrors a live HA sensor or holds a # Each attribute either mirrors a live HA sensor or holds a

View File

@@ -7,7 +7,7 @@ import time
from logger_interface import LoggerInterface from logger_interface import LoggerInterface
class VirtualSensorBase: class VirtualSensorBase:
def __init__(self,ad_api,logger_interface, virtual_sensor_name = None, sensor_name = None,super_entity_id = None, yaml_block = None,templates_library = None, constants = None,self_initialize = False): def __init__(self,ad_api,logger_interface, virtual_sensor_name = None, sensor_name = None,super_entity_id = None, yaml_block = None,templates_library = None, constants = None, app_name = None,self_initialize = False):
self.ad_api = ad_api self.ad_api = ad_api
self.logger_interface = logger_interface self.logger_interface = logger_interface
@@ -32,6 +32,9 @@ class VirtualSensorBase:
try: self.attributes = self.yaml_block['attributes'] try: self.attributes = self.yaml_block['attributes']
except (TypeError,KeyError): self.attributes = dict() except (TypeError,KeyError): self.attributes = dict()
if app_name:
self.attributes['ad_app'] = app_name
if constants: if constants:
self.constants = dict(constants) # we don't want to modify the parent dict self.constants = dict(constants) # we don't want to modify the parent dict
self.constants['self'] = self.sensor_name # we need to set self by ourself self.constants['self'] = self.sensor_name # we need to set self by ourself
@@ -554,7 +557,7 @@ class ValueSensor(VirtualSensorBase):
class VirtualSensors(): class VirtualSensors():
def __init__(self, ad_api = None,logger_interface = None, super_entity_id = None, yaml_block = None ,args_to_ignore_in_validation = [], constants = None, templates_library = None): def __init__(self, ad_api = None,logger_interface = None, super_entity_id = None, yaml_block = None ,args_to_ignore_in_validation = [], constants = None, templates_library = None, app_name = None):
assert ad_api assert ad_api
assert yaml_block assert yaml_block
assert logger_interface assert logger_interface
@@ -572,20 +575,20 @@ class VirtualSensors():
averagers = parser.parse_args('averagers',{}) averagers = parser.parse_args('averagers',{})
for averager in averagers: for averager in averagers:
self.virtual_sensors[f"sensor.{averager}"] = Averager(self.ad_api,self.logger_interface,averager,super_entity_id = super_entity_id,yaml_block = yaml_block['averagers'][averager],constants = constants, templates_library = templates_library) self.virtual_sensors[f"sensor.{averager}"] = Averager(self.ad_api,self.logger_interface,averager,super_entity_id = super_entity_id,yaml_block = yaml_block['averagers'][averager],constants = constants, templates_library = templates_library,app_name = app_name)
continuous_conditions = parser.parse_args('continuous_conditions',{}) continuous_conditions = parser.parse_args('continuous_conditions',{})
for continuous_condition in continuous_conditions: for continuous_condition in continuous_conditions:
self.virtual_sensors[f"binary_sensor.{continuous_condition}"] = ContinuousCondition(self.ad_api,self.logger_interface,continuous_condition,super_entity_id = super_entity_id,yaml_block = yaml_block['continuous_conditions'][continuous_condition],templates_library = templates_library) self.virtual_sensors[f"binary_sensor.{continuous_condition}"] = ContinuousCondition(self.ad_api,self.logger_interface,continuous_condition,super_entity_id = super_entity_id,yaml_block = yaml_block['continuous_conditions'][continuous_condition],templates_library = templates_library,app_name = app_name)
binary_sensors = parser.parse_args('binary_sensors',{}) binary_sensors = parser.parse_args('binary_sensors',{})
for binary_sensor in binary_sensors: for binary_sensor in binary_sensors:
#self.logger_interface.log_info(f"Creating Binary Sensor: binary_sensor = {binary_sensor}, super_entity_id = {super_entity_id}, yaml_block = {yaml_block['binary_sensors']}") #self.logger_interface.log_info(f"Creating Binary Sensor: binary_sensor = {binary_sensor}, super_entity_id = {super_entity_id}, yaml_block = {yaml_block['binary_sensors']}")
self.virtual_sensors[f"binary_sensor.{binary_sensor}"] = BinarySensor(self.ad_api,self.logger_interface,binary_sensor,super_entity_id = super_entity_id,yaml_block = yaml_block['binary_sensors'][binary_sensor],constants = constants,templates_library = templates_library) self.virtual_sensors[f"binary_sensor.{binary_sensor}"] = BinarySensor(self.ad_api,self.logger_interface,binary_sensor,super_entity_id = super_entity_id,yaml_block = yaml_block['binary_sensors'][binary_sensor],constants = constants,templates_library = templates_library,app_name = app_name)
value_selectors = parser.parse_args('value_selectors',{}) value_selectors = parser.parse_args('value_selectors',{})
for value_selector in value_selectors: for value_selector in value_selectors:
self.virtual_sensors[f"sensor.{value_selector}"] = ValueSelector(self.ad_api,self.logger_interface,value_selector,super_entity_id = super_entity_id,yaml_block = yaml_block['value_selectors'][value_selector],constants = constants,templates_library = templates_library) self.virtual_sensors[f"sensor.{value_selector}"] = ValueSelector(self.ad_api,self.logger_interface,value_selector,super_entity_id = super_entity_id,yaml_block = yaml_block['value_selectors'][value_selector],constants = constants,templates_library = templates_library,app_name = app_name)
sensors = parser.parse_args('sensors',{}) sensors = parser.parse_args('sensors',{})
for sensor in sensors: for sensor in sensors:
@@ -594,22 +597,20 @@ class VirtualSensors():
self.logger_interface.log_error(f"Invalid sensor name {splitted_sensor}") self.logger_interface.log_error(f"Invalid sensor name {splitted_sensor}")
if splitted_sensor[0] == 'binary_sensor': if splitted_sensor[0] == 'binary_sensor':
self.virtual_sensors[f"binary_sensor.{splitted_sensor[1]}"] = BinarySensor(self.ad_api,self.logger_interface,splitted_sensor[1],super_entity_id = super_entity_id,yaml_block = yaml_block['sensors'][sensor],constants = constants,templates_library = templates_library) self.virtual_sensors[f"binary_sensor.{splitted_sensor[1]}"] = BinarySensor(self.ad_api,self.logger_interface,splitted_sensor[1],super_entity_id = super_entity_id,yaml_block = yaml_block['sensors'][sensor],constants = constants,templates_library = templates_library,app_name = app_name)
elif splitted_sensor[0] == 'sensor': elif splitted_sensor[0] == 'sensor':
self.virtual_sensors[f"sensor.{splitted_sensor[1]}"] = ValueSensor(self.ad_api,self.logger_interface,splitted_sensor[1],super_entity_id = super_entity_id,yaml_block = yaml_block['sensors'][sensor],constants = constants,templates_library = templates_library) self.virtual_sensors[f"sensor.{splitted_sensor[1]}"] = ValueSensor(self.ad_api,self.logger_interface,splitted_sensor[1],super_entity_id = super_entity_id,yaml_block = yaml_block['sensors'][sensor],constants = constants,templates_library = templates_library,app_name = app_name)
elif splitted_sensor[0] == 'continuous_condition': elif splitted_sensor[0] == 'continuous_condition':
self.virtual_sensors[f"binary_sensor.{splitted_sensor[1]}"] = ContinuousCondition(self.ad_api,self.logger_interface,splitted_sensor[1],super_entity_id = super_entity_id,yaml_block = yaml_block['sensors'][sensor],constants = constants,templates_library = templates_library) self.virtual_sensors[f"binary_sensor.{splitted_sensor[1]}"] = ContinuousCondition(self.ad_api,self.logger_interface,splitted_sensor[1],super_entity_id = super_entity_id,yaml_block = yaml_block['sensors'][sensor],constants = constants,templates_library = templates_library,app_name = app_name)
elif splitted_sensor[0] == 'averager': elif splitted_sensor[0] == 'averager':
self.virtual_sensors[f"sensor.{splitted_sensor[1]}"] = Averager(self.ad_api,self.logger_interface,splitted_sensor[1],super_entity_id = super_entity_id,yaml_block = yaml_block['sensors'][sensor],constants = constants,templates_library = templates_library) self.virtual_sensors[f"sensor.{splitted_sensor[1]}"] = Averager(self.ad_api,self.logger_interface,splitted_sensor[1],super_entity_id = super_entity_id,yaml_block = yaml_block['sensors'][sensor],constants = constants,templates_library = templates_library,app_name = app_name)
elif splitted_sensor[0] == 'value_selector': elif splitted_sensor[0] == 'value_selector':
self.virtual_sensors[f"sensor.{splitted_sensor[1]}"] = ValueSelector(self.ad_api,self.logger_interface,splitted_sensor[1],super_entity_id = super_entity_id,yaml_block = yaml_block['sensors'][sensor],constants = constants,templates_library = templates_library) self.virtual_sensors[f"sensor.{splitted_sensor[1]}"] = ValueSelector(self.ad_api,self.logger_interface,splitted_sensor[1],super_entity_id = super_entity_id,yaml_block = yaml_block['sensors'][sensor],constants = constants,templates_library = templates_library,app_name = app_name)
elif splitted_sensor[0] == 'retain_condition': elif splitted_sensor[0] == 'retain_condition':
self.virtual_sensors[f"binary_sensor.{splitted_sensor[1]}"] = RetainCondition(self.ad_api,self.logger_interface,splitted_sensor[1],super_entity_id = super_entity_id,yaml_block = yaml_block['sensors'][sensor],constants = constants,templates_library = templates_library) self.virtual_sensors[f"binary_sensor.{splitted_sensor[1]}"] = RetainCondition(self.ad_api,self.logger_interface,splitted_sensor[1],super_entity_id = super_entity_id,yaml_block = yaml_block['sensors'][sensor],constants = constants,templates_library = templates_library,app_name = app_name)
else: else:
self.logger_interface.log_error(f"Invalid sensor prefix {splitted_sensor[0]}") self.logger_interface.log_error(f"Invalid sensor prefix {splitted_sensor[0]}")
parser.validate_args(args_to_ignore_in_validation)
dependencies_graph = dict() dependencies_graph = dict()
for sensor_name, virtual_sensor in self.virtual_sensors.items(): for sensor_name, virtual_sensor in self.virtual_sensors.items():
try: try:
@@ -672,4 +673,4 @@ class VirtualSensors():
class VirtualSensorsApp(hass.Hass): class VirtualSensorsApp(hass.Hass):
def initialize(self): def initialize(self):
self.logger_interface = LoggerInterface(self.get_ad_api(),default_log = "virtualsensors_log") self.logger_interface = LoggerInterface(self.get_ad_api(),default_log = "virtualsensors_log")
self.virtual_sensors = VirtualSensors(ad_api = self.get_ad_api(),logger_interface = self.logger_interface,yaml_block = self.args,args_to_ignore_in_validation = ['module','class','global_dependencies','priority','name','config_path']) self.virtual_sensors = VirtualSensors(ad_api = self.get_ad_api(),logger_interface = self.logger_interface,yaml_block = self.args,args_to_ignore_in_validation = ['module','class','global_dependencies','priority','name','config_path'],app_name = self.name)