Currently I have the option to configure my integration via YAML and via GUI.
Now I want to add an import / convert method to convert the YAML config to a GUI config.
So I try to detect all configured sensors for my domain in config_flow.py
So far I have this code
async def migrate_another_mvg_sensors_to_gui(self, hass):
"""Migrate all 'another_mvg' sensors from YAML to GUI Config Entries."""
domain = "another_mvg"
# Listen für YAML- und GUI-Sensoren
yaml_sensors = []
gui_sensors = []
# Alle Konfigurationseinträge der Integration abrufen
gui_entries = hass.config_entries.async_entries(domain)
_LOGGER.warning("Gefunden GUI-Einträge: %d", len(gui_entries))
# Alle Entitäten abrufen
all_entities = hass.states.async_all()
_LOGGER.warning("Gefunden Entitäten: %d", len(all_entities))
# Durchlaufe alle Entitäten
for entity in all_entities:
#_LOGGER.warning("Überprüfen der Entität: %s", entity.entity_id)
#_LOGGER.warning("Entitätsattribute: %s", entity.attributes)
# Überprüfen, ob die Entität zur 'another_mvg'-Integration gehört
if 'globalid' in entity.attributes or 'unique_id' in entity.attributes or 'config' in entity.attributes:
_LOGGER.warning("Sensor zur Domain gefunden: %s", entity.entity_id)
_LOGGER.warning("Entitätsattribute: %s", entity.attributes)
_LOGGER.warning("Entität: %s", entity)
# Überprüfen, ob diese Entität einen zugehörigen Config Entry hat
unique_id = entity.attributes.get("unique_id")
found_entry = None
for entry in gui_entries:
_LOGGER.warning("GUI Entität: %s", entry)
if entry.unique_id == unique_id:
found_entry = entry
break
if found_entry:
gui_sensors.append(entity)
_LOGGER.warning("Gefundener GUI-Sensor: %s", entity.entity_id)
else:
yaml_sensors.append(entity)
_LOGGER.warning("Gefundener YAML-Sensor: %s", entity.entity_id)
# YAML-Sensoren in GUI-Config-Einträge umwandeln
for sensor in yaml_sensors:
unique_id = sensor.attributes.get("unique_id")
config_data = {
"globalid": sensor.attributes.get("globalid"),
"globalid2": sensor.attributes.get("globalid2"),
"limit": sensor.attributes.get("limit"),
"onlyline": sensor.attributes.get("onlyline"),
"hidedestination": sensor.attributes.get("hidedestination"),
"onlydestination": sensor.attributes.get("onlydestination"),
"name": sensor.attributes.get("friendly_name", "Another MVG Sensor"),
"doublestationnumber": sensor.attributes.get("doublestationnumber"),
"transporttypes": sensor.attributes.get("transporttypes"),
"hidename": sensor.attributes.get("hidename"),
"show_clock": sensor.attributes.get("show_clock"),
"departure_format": sensor.attributes.get("departure_format"),
"timezoneFrom": sensor.attributes.get("timezoneFrom"),
"timezoneTo": sensor.attributes.get("timezoneTo"),
"alert_for": sensor.attributes.get("alert_for"),
}
# Config Entry erstellen
#hass.config_entries.async_create_entry(
# title=config_data["name"],
# data=config_data,
# unique_id=unique_id
#)
_LOGGER.warning("Migration abgeschlossen: %d YAML-Sensoren migriert.", len(yaml_sensors))
_LOGGER.warning("Migration abgeschlossen: %d GUI-Sensoren gefunden.", len(gui_sensors))
My idea was to check for the attribute globalid
but it looks like that it not returns all attributes of the sensor. Also it looks like that you can not query hass.states.async_all()
for a simble domain. So for the moment I used this
if 'globalid' in entity.attributes or 'unique_id' in entity.attributes or 'config' in entity.attributes:
to see at least some results
I know the code is currently a little bit confusing ^^ but maybe someone has an better idea how to get all sensors for my domain including the YAML sensors and including all attributes