so because I recently noticed the Z-waveJS integration is displayed under many Domains, I would love to develop on this script, to list these multiple subdomains with their respective domains. Eg I now have
binary_sensor.zwave_js
climate.zwave_js
cover.zwave_js
fan.zwave_js
light.zwave_js
lock.zwave_js
number.zwave_js
sensor.zwave_js
switch.zwave_js
and finally zwave_js,
and I would love to have a template or python script creating an output like
zwave_js:
- binary_sensor
- climate
- cover
- fan
- light
- lock
- number
- sensor
- switch
for all subdomains belonging to other domains.
This must be an easy reversal in the scripts logic, but all my thoughts on it lead to nothing… please have a look with me how to create that?
Thanks!
update
progress but nothing ripe for production just yet:
attributes = {}
components = hass.states.get('sensor.ha_rpi4_config').attributes['components']
cnt = len(components)
components.sort()
# Make a dictionary of all main domains, add each sub domain to the main domain list.
compdict = {}
subdict = {}
for component in components:
if component.count('.') == 0 and component not in compdict:
compdict[component] = []
if component.count('.') == 1:
domain, subdomain = component.split('.')
# compdict[domain].append(subdomain)
compdict.setdefault(domain, []).append(subdomain)
if len(subdomain) > 1:
subdict.setdefault(subdomain, []).append(domain)
# Make the dictionary into a flat list of strings.
complist = []
for key, value in compdict.items():
if value:
value.sort()
# Adding a domain & series of sub domains
complist.append('- {}: \n for: {}'.format(key, ', '.join(value)))
# else:
# complist.append('- {}'.format(key))
sublist = []
cntsub = 0
for key, value in subdict.items():
if value:
value.sort()
# Adding a subdomain & series of domains
sublist.append('- {}: \n for: {}'.format(key, ', '.join(value)))
cntsub = cntsub + 1
else:
sublist.append('- {}'.format(key))
# join each component with a carriage return
complist = '\n'.join(complist)
sublist = '\n'.join(sublist)
#text = '{} Loaded Components:\n' \
# '{}'.format(cnt, complist)
#subtext = '{} Multiple subdomains:\n' \
# '{}'.format(cntsub, sublist)
attributes['friendly_name'] = 'Components'
attributes['icon'] = 'mdi:format-list-bulleted-type'
attributes['Components'] = cnt
attributes['---------------'] = '--------'
attributes['text'] = complist #text
attributes['Subdomains'] = cntsub
attributes[**************'] = '--------'
attributes['subtext'] = sublist
hass.states.set('sensor.overview_components', cnt, attributes)
results in a listing like:
- xbox:
for: binary_sensor, media_player, remote, sensor
- zwave_js:
for: binary_sensor, climate, cover, fan, light, lock, number, sensor, switch
- buienradar:
for: camera, sensor, weather
but it has 2 major flaws just yet, in that it also lists the single subdomain entries. I thought I prevented that with
if len(subdomain) > 1:
subdict.setdefault(subdomain, []).append(domain)
but apparently it doesn’t
Secondly, I would love to sort() the subdomains alphabetically. Right now, the list of domains (for: binary_sensor, media_player, remote, sensor) is sorted correctly, but the main listing isnt.