reduce cognitive complexity

This commit is contained in:
Stefan Allius
2024-09-03 18:32:44 +02:00
parent a76c0ac440
commit ab5256659b

View File

@@ -427,20 +427,42 @@ 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:
return self.__ha_conf(row, key, ha_prfx, node_id, snr, sug_area)
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'] ha = row['ha']
if 'comp' in ha: if 'comp' in ha:
component = ha['comp'] component = ha['comp']
else: else:
component = 'sensor' 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 = {} attr = {}
ha = row['ha']
if 'name' in ha: if 'name' in ha:
attr['name'] = ha['name'] attr['name'] = ha['name']
else: else:
attr['name'] = row['name'][-1] attr['name'] = row['name'][-1]
prfx = ha_prfx + node_id
attr['stat_t'] = prfx + row['name'][0] attr['stat_t'] = prfx + row['name'][0]
attr['dev_cla'] = ha['dev_cla'] attr['dev_cla'] = ha['dev_cla']
attr['stat_cla'] = ha['stat_cla'] attr['stat_cla'] = ha['stat_cla']
@@ -470,21 +492,35 @@ class Infos:
# assistant. tested with 'Home Assistant 2023.10.4' # assistant. tested with 'Home Assistant 2023.10.4'
# if 'en' in ha: # enabled_by_default # if 'en' in ha: # enabled_by_default
# attr['en'] = ha['en'] # attr['en'] = ha['en']
if 'dev' in ha: return attr
device = self.info_devs[ha['dev']]
if 'dep' in device and self.ignore_this_device(device['dep']): # noqa: E501 def __build_dev(self, device, key, ha, snr, sug_area):
return None
dev = {} dev = {}
singleton = 'singleton' in device and device['singleton']
# the same name for 'name' and 'suggested area', so we get # the same name for 'name' and 'suggested area', so we get
# dedicated devices in home assistant with short value # dedicated devices in home assistant with short value
# name and headline # name and headline
if (sug_area == '' or if (sug_area == '' or singleton):
('singleton' in device and device['singleton'])):
dev['name'] = device['name'] dev['name'] = device['name']
dev['sa'] = device['name'] dev['sa'] = device['name']
else: else:
dev['name'] = device['name']+' - '+sug_area dev['name'] = device['name']+' - '+sug_area
dev['sa'] = 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 if 'via' in device: # add the link to the parent device
via = device['via'] via = device['via']
if via in self.info_devs: if via in self.info_devs:
@@ -498,28 +534,12 @@ class Infos:
logging.error(f"Infos.info_defs: the row for " logging.error(f"Infos.info_defs: the row for "
f"{key} has an invalid via value: " f"{key} has an invalid via value: "
f"{via}") f"{via}")
for key in ('mdl', 'mf', 'sw', 'hw'): # add optional
# values fpr 'modell', 'manufacturer', 'sw version' and def __build_origin(self):
# '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 = {}
origin['name'] = self.app_name origin['name'] = self.app_name
origin['sw'] = self.version origin['sw'] = self.version
attr['o'] = origin return 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
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