Exemple #1
0
def async_migrate_entry_from_climacell(
    hass: HomeAssistant,
    dev_reg: dr.DeviceRegistry,
    entry: ConfigEntry,
    device: dr.DeviceEntry,
) -> None:
    """Migrate a config entry from a Climacell entry."""
    # Remove the old config entry ID from the entry data so we don't try this again
    # on the next setup
    data = entry.data.copy()
    old_config_entry_id = data.pop("old_config_entry_id")
    hass.config_entries.async_update_entry(entry, data=data)
    _LOGGER.debug(
        (
            "Setting up imported climacell entry %s for the first time as "
            "tomorrowio entry %s"
        ),
        old_config_entry_id,
        entry.entry_id,
    )

    ent_reg = er.async_get(hass)
    for entity_entry in er.async_entries_for_config_entry(ent_reg, old_config_entry_id):
        old_platform = entity_entry.platform
        # In case the API key has changed due to a V3 -> V4 change, we need to
        # generate the new entity's unique ID
        new_unique_id = (
            f"{entry.data[CONF_API_KEY]}_"
            f"{'_'.join(entity_entry.unique_id.split('_')[1:])}"
        )
        ent_reg.async_update_entity_platform(
            entity_entry.entity_id,
            DOMAIN,
            new_unique_id=new_unique_id,
            new_config_entry_id=entry.entry_id,
            new_device_id=device.id,
        )
        assert entity_entry
        _LOGGER.debug(
            "Migrated %s from %s to %s",
            entity_entry.entity_id,
            old_platform,
            DOMAIN,
        )

    # We only have one device in the registry but we will do a loop just in case
    for old_device in dr.async_entries_for_config_entry(dev_reg, old_config_entry_id):
        if old_device.name_by_user:
            dev_reg.async_update_device(device.id, name_by_user=old_device.name_by_user)

    # Remove the old config entry and now the entry is fully migrated
    hass.async_create_task(hass.config_entries.async_remove(old_config_entry_id))
Exemple #2
0
async def _remove_device(
    hass: HomeAssistant,
    config_entry: ConfigEntry,
    mac: str,
    tasmota_mqtt: TasmotaMQTTClient,
    device_registry: DeviceRegistry,
) -> None:
    """Remove a discovered Tasmota device."""
    device = device_registry.async_get_device(set(),
                                              {(CONNECTION_NETWORK_MAC, mac)})

    if device is None or config_entry.entry_id not in device.config_entries:
        return

    _LOGGER.debug("Removing tasmota from device %s", mac)
    device_registry.async_update_device(
        device.id, remove_config_entry_id=config_entry.entry_id)
Exemple #3
0
async def _remove_device(
    hass: HomeAssistant,
    config_entry: ConfigEntry,
    mac: str,
    tasmota_mqtt: TasmotaMQTTClient,
    device_registry: DeviceRegistry,
) -> None:
    """Remove device from device registry."""
    device = device_registry.async_get_device(set(),
                                              {(CONNECTION_NETWORK_MAC, mac)})

    if device is None or config_entry.entry_id not in device.config_entries:
        return

    _LOGGER.debug("Removing tasmota from device %s", mac)
    device_registry.async_update_device(
        device.id, remove_config_entry_id=config_entry.entry_id)
    await clear_discovery_topic(mac, config_entry.data[CONF_DISCOVERY_PREFIX],
                                tasmota_mqtt)