reduce cognitive complexity
This commit is contained in:
198
app/src/infos.py
198
app/src/infos.py
@@ -427,100 +427,120 @@ class Infos:
|
|||||||
return None
|
return None
|
||||||
elif singleton:
|
elif singleton:
|
||||||
return None
|
return None
|
||||||
prfx = ha_prfx + node_id
|
|
||||||
|
|
||||||
# check if we have details for home assistant
|
# check if we have details for home assistant
|
||||||
if 'ha' in row:
|
if 'ha' in row:
|
||||||
ha = row['ha']
|
return self.__ha_conf(row, key, ha_prfx, node_id, snr, sug_area)
|
||||||
if 'comp' in ha:
|
|
||||||
component = ha['comp']
|
|
||||||
else:
|
|
||||||
component = 'sensor'
|
|
||||||
attr = {}
|
|
||||||
if 'name' in ha:
|
|
||||||
attr['name'] = ha['name']
|
|
||||||
else:
|
|
||||||
attr['name'] = row['name'][-1]
|
|
||||||
attr['stat_t'] = prfx + row['name'][0]
|
|
||||||
attr['dev_cla'] = ha['dev_cla']
|
|
||||||
attr['stat_cla'] = ha['stat_cla']
|
|
||||||
attr['uniq_id'] = ha['id']+snr
|
|
||||||
if 'val_tpl' in ha:
|
|
||||||
attr['val_tpl'] = ha['val_tpl']
|
|
||||||
elif 'fmt' in ha:
|
|
||||||
attr['val_tpl'] = '{{value_json' + f"['{row['name'][-1]}'] {ha['fmt']}" + '}}' # eg. 'val_tpl': "{{ value_json['Output_Power']|float }} # noqa: E501
|
|
||||||
else:
|
|
||||||
self.inc_counter('Internal_Error')
|
|
||||||
logging.error(f"Infos.info_defs: the row for {key} do"
|
|
||||||
" not have a 'val_tpl' nor a 'fmt' value")
|
|
||||||
# add unit_of_meas only, if status_class isn't none. If
|
|
||||||
# status_cla is None we want a number format and not line
|
|
||||||
# graph in home assistant. A unit will change the number
|
|
||||||
# format to a line graph
|
|
||||||
if 'unit' in row and attr['stat_cla'] is not None:
|
|
||||||
attr['unit_of_meas'] = row['unit'] # 'unit_of_meas'
|
|
||||||
if 'icon' in ha:
|
|
||||||
attr['ic'] = ha['icon'] # icon for the entity
|
|
||||||
if 'nat_prc' in ha: # pragma: no cover
|
|
||||||
attr['sug_dsp_prc'] = ha['nat_prc'] # precison of floats
|
|
||||||
if 'ent_cat' in ha:
|
|
||||||
attr['ent_cat'] = ha['ent_cat'] # diagnostic, config
|
|
||||||
# enabled_by_default is deactivated, since it avoid the via
|
|
||||||
# setup of the devices. It seems, that there is a bug in home
|
|
||||||
# assistant. tested with 'Home Assistant 2023.10.4'
|
|
||||||
# if 'en' in ha: # enabled_by_default
|
|
||||||
# attr['en'] = ha['en']
|
|
||||||
if 'dev' in ha:
|
|
||||||
device = self.info_devs[ha['dev']]
|
|
||||||
if 'dep' in device and self.ignore_this_device(device['dep']): # noqa: E501
|
|
||||||
return None
|
|
||||||
dev = {}
|
|
||||||
# the same name for 'name' and 'suggested area', so we get
|
|
||||||
# dedicated devices in home assistant with short value
|
|
||||||
# name and headline
|
|
||||||
if (sug_area == '' or
|
|
||||||
('singleton' in device and device['singleton'])):
|
|
||||||
dev['name'] = device['name']
|
|
||||||
dev['sa'] = device['name']
|
|
||||||
else:
|
|
||||||
dev['name'] = device['name']+' - '+sug_area
|
|
||||||
dev['sa'] = device['name']+' - '+sug_area
|
|
||||||
if 'via' in device: # add the link to the parent device
|
|
||||||
via = device['via']
|
|
||||||
if via in self.info_devs:
|
|
||||||
via_dev = self.info_devs[via]
|
|
||||||
if 'singleton' in via_dev and via_dev['singleton']:
|
|
||||||
dev['via_device'] = via
|
|
||||||
else:
|
|
||||||
dev['via_device'] = f"{via}_{snr}"
|
|
||||||
else:
|
|
||||||
self.inc_counter('Internal_Error')
|
|
||||||
logging.error(f"Infos.info_defs: the row for "
|
|
||||||
f"{key} has an invalid via value: "
|
|
||||||
f"{via}")
|
|
||||||
for key in ('mdl', 'mf', 'sw', 'hw'): # add optional
|
|
||||||
# values fpr 'modell', 'manufacturer', 'sw version' and
|
|
||||||
# 'hw version'
|
|
||||||
if key in device:
|
|
||||||
data = self.dev_value(device[key])
|
|
||||||
if data is not None:
|
|
||||||
dev[key] = data
|
|
||||||
if 'singleton' in device and device['singleton']:
|
|
||||||
dev['ids'] = [f"{ha['dev']}"]
|
|
||||||
else:
|
|
||||||
dev['ids'] = [f"{ha['dev']}_{snr}"]
|
|
||||||
attr['dev'] = dev
|
|
||||||
origin = {}
|
|
||||||
origin['name'] = self.app_name
|
|
||||||
origin['sw'] = self.version
|
|
||||||
attr['o'] = origin
|
|
||||||
else:
|
|
||||||
self.inc_counter('Internal_Error')
|
|
||||||
logging.error(f"Infos.info_defs: the row for {key} "
|
|
||||||
"missing 'dev' value for ha register")
|
|
||||||
return json.dumps(attr), component, node_id, attr['uniq_id']
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def __ha_conf(self, row, key, ha_prfx, node_id, snr,
|
||||||
|
sug_area: str) -> tuple[str, str, str, str] | None:
|
||||||
|
ha = row['ha']
|
||||||
|
if 'comp' in ha:
|
||||||
|
component = ha['comp']
|
||||||
|
else:
|
||||||
|
component = 'sensor'
|
||||||
|
attr = self.__build_attr(row, key, ha_prfx, node_id, snr)
|
||||||
|
if 'dev' in ha:
|
||||||
|
device = self.info_devs[ha['dev']]
|
||||||
|
if 'dep' in device and self.ignore_this_device(device['dep']): # noqa: E501
|
||||||
|
return None
|
||||||
|
attr['dev'] = self.__build_dev(device, key, ha, snr,
|
||||||
|
sug_area)
|
||||||
|
attr['o'] = self.__build_origin()
|
||||||
|
|
||||||
|
else:
|
||||||
|
self.inc_counter('Internal_Error')
|
||||||
|
logging.error(f"Infos.info_defs: the row for {key} "
|
||||||
|
"missing 'dev' value for ha register")
|
||||||
|
return json.dumps(attr), component, node_id, attr['uniq_id']
|
||||||
|
|
||||||
|
def __build_attr(self, row, key, ha_prfx, node_id, snr):
|
||||||
|
attr = {}
|
||||||
|
ha = row['ha']
|
||||||
|
if 'name' in ha:
|
||||||
|
attr['name'] = ha['name']
|
||||||
|
else:
|
||||||
|
attr['name'] = row['name'][-1]
|
||||||
|
prfx = ha_prfx + node_id
|
||||||
|
attr['stat_t'] = prfx + row['name'][0]
|
||||||
|
attr['dev_cla'] = ha['dev_cla']
|
||||||
|
attr['stat_cla'] = ha['stat_cla']
|
||||||
|
attr['uniq_id'] = ha['id']+snr
|
||||||
|
if 'val_tpl' in ha:
|
||||||
|
attr['val_tpl'] = ha['val_tpl']
|
||||||
|
elif 'fmt' in ha:
|
||||||
|
attr['val_tpl'] = '{{value_json' + f"['{row['name'][-1]}'] {ha['fmt']}" + '}}' # eg. 'val_tpl': "{{ value_json['Output_Power']|float }} # noqa: E501
|
||||||
|
else:
|
||||||
|
self.inc_counter('Internal_Error')
|
||||||
|
logging.error(f"Infos.info_defs: the row for {key} do"
|
||||||
|
" not have a 'val_tpl' nor a 'fmt' value")
|
||||||
|
# add unit_of_meas only, if status_class isn't none. If
|
||||||
|
# status_cla is None we want a number format and not line
|
||||||
|
# graph in home assistant. A unit will change the number
|
||||||
|
# format to a line graph
|
||||||
|
if 'unit' in row and attr['stat_cla'] is not None:
|
||||||
|
attr['unit_of_meas'] = row['unit'] # 'unit_of_meas'
|
||||||
|
if 'icon' in ha:
|
||||||
|
attr['ic'] = ha['icon'] # icon for the entity
|
||||||
|
if 'nat_prc' in ha: # pragma: no cover
|
||||||
|
attr['sug_dsp_prc'] = ha['nat_prc'] # precison of floats
|
||||||
|
if 'ent_cat' in ha:
|
||||||
|
attr['ent_cat'] = ha['ent_cat'] # diagnostic, config
|
||||||
|
# enabled_by_default is deactivated, since it avoid the via
|
||||||
|
# setup of the devices. It seems, that there is a bug in home
|
||||||
|
# assistant. tested with 'Home Assistant 2023.10.4'
|
||||||
|
# if 'en' in ha: # enabled_by_default
|
||||||
|
# attr['en'] = ha['en']
|
||||||
|
return attr
|
||||||
|
|
||||||
|
def __build_dev(self, device, key, ha, snr, sug_area):
|
||||||
|
dev = {}
|
||||||
|
singleton = 'singleton' in device and device['singleton']
|
||||||
|
# the same name for 'name' and 'suggested area', so we get
|
||||||
|
# dedicated devices in home assistant with short value
|
||||||
|
# name and headline
|
||||||
|
if (sug_area == '' or singleton):
|
||||||
|
dev['name'] = device['name']
|
||||||
|
dev['sa'] = device['name']
|
||||||
|
else:
|
||||||
|
dev['name'] = device['name']+' - '+sug_area
|
||||||
|
dev['sa'] = device['name']+' - '+sug_area
|
||||||
|
self.__add_via_dev(dev, device, key, snr)
|
||||||
|
for key in ('mdl', 'mf', 'sw', 'hw'): # add optional
|
||||||
|
# values fpr 'modell', 'manufacturer', 'sw version' and
|
||||||
|
# 'hw version'
|
||||||
|
if key in device:
|
||||||
|
data = self.dev_value(device[key])
|
||||||
|
if data is not None:
|
||||||
|
dev[key] = data
|
||||||
|
if singleton:
|
||||||
|
dev['ids'] = [f"{ha['dev']}"]
|
||||||
|
else:
|
||||||
|
dev['ids'] = [f"{ha['dev']}_{snr}"]
|
||||||
|
return dev
|
||||||
|
|
||||||
|
def __add_via_dev(self, dev, device, key, snr):
|
||||||
|
if 'via' in device: # add the link to the parent device
|
||||||
|
via = device['via']
|
||||||
|
if via in self.info_devs:
|
||||||
|
via_dev = self.info_devs[via]
|
||||||
|
if 'singleton' in via_dev and via_dev['singleton']:
|
||||||
|
dev['via_device'] = via
|
||||||
|
else:
|
||||||
|
dev['via_device'] = f"{via}_{snr}"
|
||||||
|
else:
|
||||||
|
self.inc_counter('Internal_Error')
|
||||||
|
logging.error(f"Infos.info_defs: the row for "
|
||||||
|
f"{key} has an invalid via value: "
|
||||||
|
f"{via}")
|
||||||
|
|
||||||
|
def __build_origin(self):
|
||||||
|
origin = {}
|
||||||
|
origin['name'] = self.app_name
|
||||||
|
origin['sw'] = self.version
|
||||||
|
return origin
|
||||||
|
|
||||||
def ha_remove(self, key, node_id, snr) -> tuple[str, str, str, str] | None:
|
def ha_remove(self, key, node_id, snr) -> tuple[str, str, str, str] | None:
|
||||||
'''Method to build json unregister struct for home-assistant
|
'''Method to build json unregister struct for home-assistant
|
||||||
to remove topics per auto configuration. Only for inverer topics.
|
to remove topics per auto configuration. Only for inverer topics.
|
||||||
|
|||||||
Reference in New Issue
Block a user