register proxy entities under a unique device (singleton)

This commit is contained in:
Stefan Allius
2023-10-15 23:05:56 +02:00
parent 909d5ca44a
commit 10ec949a5b
2 changed files with 26 additions and 11 deletions

View File

@@ -40,9 +40,9 @@ class Infos:
0x00000032: {'name':['inverter', 'Equipment_Model'], 'level': logging.DEBUG, 'unit': ''},
# proxy:
0xffffff00: {'name':['proxy', 'Inverter_Cnt'], 'ha':{'dev':'proxy', 'dev_cla': None, 'stat_cla': None, 'id':'inv_count_', 'fmt':'| int', 'name': 'Inverter Count', 'icon':'mdi:counter'}},
0xffffff01: {'name':['proxy', 'Unknown_SNR'], 'ha':{'dev':'proxy', 'dev_cla': None, 'stat_cla': None, 'id':'unknown_snr_', 'fmt':'| int', 'name': 'Unknown Serial No', 'icon':'mdi:counter', 'ent_cat':'diagnostic'}},
0xffffff02: {'name':['proxy', 'Unknown_Msg'], 'ha':{'dev':'proxy', 'dev_cla': None, 'stat_cla': None, 'id':'unknown_msg_', 'fmt':'| int', 'name': 'Unknown Msg Type', 'icon':'mdi:counter', 'ent_cat':'diagnostic'}},
0xffffff00: {'name':['proxy', 'Inverter_Cnt'], 'singleton': True, 'ha':{'dev':'proxy', 'dev_cla': None, 'stat_cla': None, 'id':'inv_count_', 'fmt':'| int', 'name': 'Inverter Connection Count', 'icon':'mdi:counter'}},
0xffffff01: {'name':['proxy', 'Unknown_SNR'], 'singleton': True, 'ha':{'dev':'proxy', 'dev_cla': None, 'stat_cla': None, 'id':'unknown_snr_', 'fmt':'| int', 'name': 'Unknown Serial No', 'icon':'mdi:counter', 'ent_cat':'diagnostic'}},
0xffffff02: {'name':['proxy', 'Unknown_Msg'], 'singleton': True, 'ha':{'dev':'proxy', 'dev_cla': None, 'stat_cla': None, 'id':'unknown_msg_', 'fmt':'| int', 'name': 'Unknown Msg Type', 'icon':'mdi:counter', 'ent_cat':'diagnostic'}},
# 0xffffff03: {'name':['proxy', 'Voltage'], 'level': logging.DEBUG, 'unit': 'V', 'ha':{'dev':'proxy', 'dev_cla': 'voltage', 'stat_cla': 'measurement', 'id':'proxy_volt_', 'fmt':'| float','name': 'Grid Voltage'}},
# events
@@ -138,7 +138,7 @@ class Infos:
return not value <= dep['less_eq']
return True
def ha_confs(self, prfx="tsun/garagendach/", snr='123', sug_area =''):
def ha_confs(self, ha_prfx, inv_node_id, inv_snr, proxy_node_id, proxy_unique_id, sug_area =''):
'''Generator function yields a json register struct for home-assistant auto configuration and a unique entity string
arguments:
@@ -148,6 +148,13 @@ class Infos:
tab = self.__info_defs
for key in tab:
row = tab[key]
if 'singleton' in row and row['singleton']:
node_id = proxy_node_id
snr = proxy_unique_id
else:
node_id = inv_node_id
snr = inv_snr
prfx = ha_prfx + node_id
#check if we have details for home assistant
if 'ha' in row:
@@ -228,7 +235,7 @@ class Infos:
attr['o'] = origin
yield json.dumps (attr), component, attr['uniq_id']
yield json.dumps (attr), component, node_id, attr['uniq_id']
def __init_counter (self, counter:str) -> dict:
'''init proxy statistic counter, when its missing'''

View File

@@ -16,20 +16,26 @@ class Inverter(AsyncStream):
self.mqtt = Mqtt()
self.ha_restarts = -1
ha = Config.get('ha')
self.entitiy_prfx = ha['entity_prefix'] + '/'
self.entity_prfx = ha['entity_prefix'] + '/'
self.discovery_prfx = ha['discovery_prefix'] + '/'
self.proxy_node_id = ha['proxy_node_id'] + '/'
self.proxy_unique_id = ha['proxy_unique_id']
async def server_loop(self, addr):
'''Loop for receiving messages from the inverter (server-side)'''
logging.info(f'Accept connection from {addr}')
self.inc_counter ('Inverter_Cnt')
await self.loop()
self.dec_counter ('Inverter_Cnt')
logging.info(f'Server loop stopped for {addr}')
# if the server connection closes, we also have to disconnect the connection to te TSUN cloud
if self.remoteStream:
logging.debug ("disconnect client connection")
self.remoteStream.disc()
await self.async_publ_mqtt()
async def client_loop(self, addr):
'''Loop for receiving messages from the TSUN cloud (client-side)'''
@@ -77,23 +83,25 @@ class Inverter(AsyncStream):
self.ha_restarts = self.mqtt.ha_restarts
for key in self.new_data:
if self.new_data[key]:
if self.new_data[key]:
if key in db:
data_json = json.dumps(db[key])
node_id = self.node_id
elif key in stat:
data_json = json.dumps(stat[key])
node_id = self.proxy_node_id
else:
continue
logger_mqtt.debug(f'{key}: {data_json}')
await self.mqtt.publish(f"{self.entitiy_prfx}{self.node_id}{key}", data_json)
await self.mqtt.publish(f"{self.entity_prfx}{node_id}{key}", data_json)
self.new_data[key] = False
async def __register_home_assistant(self) -> None:
'''register all our topics at home assistant'''
try:
for data_json, component, id in self.db.ha_confs(self.entitiy_prfx + self.node_id, self.unique_id, self.sug_area):
logger_mqtt.debug(f'MQTT Register: {data_json}')
await self.mqtt.publish(f"{self.discovery_prfx}{component}/{self.node_id}{id}/config", data_json)
for data_json, component, node_id, id in self.db.ha_confs(self.entity_prfx, self.node_id, self.unique_id, self.proxy_node_id, self.proxy_unique_id, self.sug_area):
logger_mqtt.debug(f"MQTT Register: cmp:'{component}' node_id:'{node_id}' {data_json}")
await self.mqtt.publish(f"{self.discovery_prfx}{component}/{node_id}{id}/config", data_json)
except Exception:
logging.error(
f"Inverter: Exception:\n"