Ejemplo n.º 1
0
async def async_migrate_entry(hass, config_entry: ConfigEntry):
    """Migrate old entry."""
    LOGGER.debug("Migrating from version %s", config_entry.version)

    if config_entry.version == 1:

        new = {**config_entry.data}
        new[ENABLE_MOTION_SENSOR] = True
        new[CLOUD_PASSWORD] = ""

        config_entry.data = {**new}

        config_entry.version = 2

    if config_entry.version == 2:

        new = {**config_entry.data}
        new[CLOUD_PASSWORD] = ""

        config_entry.data = {**new}

        config_entry.version = 3

    LOGGER.info("Migration to version %s successful", config_entry.version)

    return True
Ejemplo n.º 2
0
async def async_migrate_entry(hass: HomeAssistant,
                              config_entry: ConfigEntry) -> bool:
    """Migrate old entry."""
    _LOGGER.debug("Migrating from version %d", config_entry.version)

    if config_entry.version == 1:
        new: Dict[str, Any] = {**config_entry.data, CONF_VERIFY_SSL: True}

        device_id = "deviceid"
        devices = new.pop(device_id, {})
        new.pop("show_color_rooms")
        new.pop("live_map")

        new[CONF_DEVICES] = devices.get(device_id, [])

        config_entry.data = {**new}
        config_entry.version = 2

    if config_entry.version == 2:
        new = {**config_entry.data}

        if new.get(CONF_USERNAME) == CONF_BUMPER:
            new[CONF_CLIENT_DEVICE_ID] = get_bumper_device_id(hass)

        config_entry.data = {**new}
        config_entry.version = 3

    _LOGGER.info("Migration to version %d successful", config_entry.version)

    return True
Ejemplo n.º 3
0
async def async_migrate_entry(hass, config_entry: ConfigEntry):
    """Migrate old entry."""
    LOGGER.debug("Migrating from version %s", config_entry.version)

    if config_entry.version == 1:

        new = {**config_entry.data}
        new[ENABLE_MOTION_SENSOR] = True
        new[CLOUD_PASSWORD] = ""

        config_entry.data = {**new}

        config_entry.version = 2

    if config_entry.version == 2:

        new = {**config_entry.data}
        new[CLOUD_PASSWORD] = ""

        config_entry.data = {**new}

        config_entry.version = 3

    if config_entry.version == 3:

        new = {**config_entry.data}
        new[ENABLE_STREAM] = True

        config_entry.data = {**new}

        config_entry.version = 4

    if config_entry.version == 4:

        new = {**config_entry.data}
        new[ENABLE_TIME_SYNC] = False

        config_entry.data = {**new}

        config_entry.version = 5

    if config_entry.version == 5:

        new = {**config_entry.data}
        new[ENABLE_SOUND_DETECTION] = False
        new[SOUND_DETECTION_PEAK] = -50
        new[SOUND_DETECTION_DURATION] = 1
        new[SOUND_DETECTION_RESET] = 10

        config_entry.data = {**new}

        config_entry.version = 6

    LOGGER.info("Migration to version %s successful", config_entry.version)

    return True
Ejemplo n.º 4
0
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry):
    """Migrate old entry."""
    _LOGGER.debug("Migrating from version %s", config_entry.version)

    if config_entry.version == 1:

        MAPPING = {
            "Cozytouch": "atlantic_cozytouch",
            "eedomus": "nexity",
            "Hi Kumo": "hi_kumo_europe",
            "Rexel Energeasy Connect": "rexel",
            "Somfy Connexoon IO": "somfy_europe",
            "Somfy Connexoon RTS": "somfy_oceania",
            "Somfy TaHoma": "somfy_europe",
        }

        entry_data = {**config_entry.data}
        old_hub = entry_data.get(CONF_HUB, "Somfy TaHoma")
        entry_data[CONF_HUB] = MAPPING[old_hub]

        _LOGGER.debug("Migrated %s to %s", old_hub, entry_data[CONF_HUB])

        config_entry.data = {**entry_data}
        config_entry.version = 2

    _LOGGER.debug("Migration to version %s successful", config_entry.version)

    return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
    """Set up fhwise from a config entry."""
    port = entry.data[CONF_PORT]
    host = entry.data(CONF_HOST)
    name = entry.data(CONF_NAME)
    fhPlayer = FhwisePlayer(host, port)
    _LOGGER.info(f"Initializing with {host}:{port}")

    try:
        fhPlayer.connect()
        model = fhPlayer.send_heartbeat()
        fhPlayer.disconnect()
        _LOGGER.info(f"{model} detected")
    except Exception as err:
        _LOGGER.error(f"Error connecting to fhwise at {host}:{port}")
        raise ConfigEntryNotReady from err

    hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
        FHWISE_OBJECT: monoprice,
        FHWISE_MODEL: model
    }

    for component in PLATFORMS:
        hass.async_create_task(
            hass.config_entries.async_forward_entry_setup(entry, component))

    return True
Ejemplo n.º 6
0
async def async_migrate_entry(hass, config_entry: ConfigEntry):
    """Migrate old entry."""
    _LOGGER.debug("Migrating from version %s", config_entry.version)

    #  Flatten configuration but keep old data if user rollbacks HASS
    if config_entry.version == 1:

        options = {**config_entry.options}
        options.setdefault(
            OPTIONS_GENERAL, {
                OPTIONS_GENERAL_POLL_INTERVAL:
                config_entry.data.get(CONF_POLL_INTERVAL,
                                      DEFAULT_POLL_INTERVAL)
            })
        config_entry.options = {**options}

        new = {**config_entry.data}
        try:
            new.pop(CONF_POLL_INTERVAL)
            new.pop(
                CONF_OPTIONS
            )  # get rid of errorneously migrated options from integration 1.0
        except:
            pass
        config_entry.data = {**new}

        config_entry.version = 2

    _LOGGER.info("Migration to version %s successful", config_entry.version)

    return True
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
    """Load the saved entities."""
    username = entry.data[CONF_USERNAME]
    password = entry.data[CONF_PASSWORD]
    list_id = entry.data[CONF_LISTS]
    name = entry.data.get(CONF_NAME, list_id)
    locale = entry.data(CONF_LOCALE, DEFAULT_LOCALE)

    coordinator = BringDataUpdateCoordinator(hass, username, password, list_id,
                                             name, locale)
    await coordinator.async_refresh()

    if not coordinator.last_update_success:
        raise ConfigEntryNotReady

    hass.data.setdefault(DOMAIN, {})
    hass.data[DOMAIN][entry.entry_id] = coordinator

    if entry.unique_id is None:
        hass.config_entries.async_update_entry(
            entry, unique_id=f"{SENSOR_PREFIX}{list_id}")

    hass.async_create_task(
        hass.config_entries.async_forward_entry_setup(entry, "sensor"))
    return True
Ejemplo n.º 8
0
async def async_migrate_entry(hass, config_entry: ConfigEntry):
    """Migrate old entry."""
    _LOGGER.debug("Migrating from version %s", config_entry.version)

    if config_entry.version == 1:
        new = {**config_entry.data}
        new[CONF_WEATHER_INTERVAL] = 24
        config_entry.data = {**new}
        config_entry.version = 2

    if config_entry.version == 2:
        new = {**config_entry.data}
        new[CONF_WIND_DIRECTION_TYPE] = DEFAULT_WIND_DIRECTION_TYPE
        config_entry.data = {**new}
        config_entry.version = 3

    _LOGGER.info("Migration to version %s successful", config_entry.version)
    return True
async def async_migrate_entry(hass, config_entry: ConfigEntry):
    """Migrate old entry."""
    _LOGGER.debug("Migrating from version %s", config_entry.version)

    if config_entry.version == 1:
        config_entry.version = 2
        config_entry.data = {"migrate_entities": True}

    return True
Ejemplo n.º 10
0
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry):
    """Migrate old Config entries."""
    LOGGER.debug("Migrating from version %s", config_entry.version)

    if config_entry.version == 1:
        entry_data = {**config_entry.data}
        entry_data[CONF_ACCOUNT_TYPE] = "production"

        config_entry.data = {**entry_data}
        config_entry.version = 2

    LOGGER.debug("Migration to version %s successful", config_entry.version)

    return True
Ejemplo n.º 11
0
async def async_migrate_entry(hass, config_entry: ConfigEntry) -> bool:
    """Migrate old schema configuration entry to new."""
    # This function is called when I change VERSION in the ConfigFlow
    # If the config schema ever changes then use this function to convert from old to new config parameters
    _LOGGER.debug("Migrating from version %s", config_entry.version)

    if config_entry.version == 1:
        new = {**config_entry.data}
        # TODO: modify Config Entry data

        config_entry.data = {**new}
        config_entry.version = 2

    _LOGGER.info("Migration to version %s successful", config_entry.version)

    return True
Ejemplo n.º 12
0
async def updater(hass: HomeAssistant, entry: ConfigEntry):
    """
    Обновляется конфигурация
    :param hass:
    :param entry:
    :return:
    """
    hub: MegaD = hass.data[DOMAIN][entry.data[CONF_ID]]
    hub.poll_interval = entry.options[CONF_SCAN_INTERVAL]
    hub.port_to_scan = entry.options.get(CONF_PORT_TO_SCAN, 0)
    entry.data = entry.options
    for platform in PLATFORMS:
        await hass.config_entries.async_forward_entry_unload(entry, platform)
    await async_remove_entry(hass, entry)
    await async_setup_entry(hass, entry)
    return True
Ejemplo n.º 13
0
async def async_migrate_entry(hass, config_entry: ConfigEntry):
    """Migrate old entry."""
    _LOGGER.debug("Migrating from version %s to version %s",
                  config_entry.version, ConfigFlow.VERSION)
    hub = await get_hub(hass, config_entry)
    new = dict(config_entry.data)
    await hub.start()
    cfg = await hub.get_config()
    await hub.stop()
    new.update(cfg)
    _LOGGER.debug(f'new config: %s', new)
    config_entry.data = new
    config_entry.version = ConfigFlow.VERSION

    _LOGGER.info("Migration to version %s successful", config_entry.version)

    return True
Ejemplo n.º 14
0
async def async_migrate_entry(hass, config_entry: ConfigEntry):
    """Migrate old entry."""
    _LOGGER.debug("Migrating from version %s", config_entry.version)

    if config_entry.version == 1:
        new = {
            CONF_NAME: config_entry.data[CONF_NAME],
            CONF_HOST: config_entry.data[CONF_HOST],
            CONF_PORT: config_entry.data[CONF_PORT],
            CONF_SERVER_TYPE: CONF_SERVER_TYPE_JAVA,
        }

        config_entry.data = {**new}
        config_entry.version = 2

    _LOGGER.info("Migration to version %s successful", config_entry.version)

    return True
Ejemplo n.º 15
0
async def async_migrate_entry(hass, config_entry: ConfigEntry):
    """Migrate old entry."""
    _LOGGER.debug("Migrating from version %s to version %s",
                  config_entry.version, CONFIGFLOW_VERSION)

    new_data = {**config_entry.data}
    new_options = {**config_entry.options}

    if config_entry.version == 1:
        config_entry.unique_id = config_entry.data["unique_id"]
        del new_data["unique_id"]
        config_entry.version = CONFIGFLOW_VERSION
        config_entry.data = {**new_data}
        _LOGGER.info("Migration of entry %s done to version %s",
                     config_entry.title, config_entry.version)
        return True

    _LOGGER.info("Migration not required")
    return True
Ejemplo n.º 16
0
async def async_migrate_entry(hass, config_entry: ConfigEntry):
    """Migrate old entry."""
    _LOGGER.debug("Migrating config_entry from version %s",
                  config_entry.version)

    if config_entry.version is None or config_entry.version == 1:

        new = {**config_entry.data}

        # modify config data, i.c. change config tag
        if CONF_MQTT_PREFIX not in new:
            new[CONF_MQTT_PREFIX] = new["MQTT prefix"]
            del new["MQTT prefix"]

        config_entry.data = {**new}

        config_entry.version = 2

    _LOGGER.info("Migration to version %s successful", config_entry.version)

    return True
Ejemplo n.º 17
0
async def async_migrate_entry(hass, config_entry: ConfigEntry):
    """Migrate old entry."""
    LOGGER.debug("Migrating from version %s", config_entry.version)

    if config_entry.version == 1:
        # Change unique id
        @callback
        def update_unique_id(entry):
            return {"new_unique_id": config_entry.entry_id}

        await async_migrate_entries(hass, config_entry.entry_id,
                                    update_unique_id)

        config_entry.unique_id = None

        # Get RTSP port from the camera or use the fallback one and store it in data
        camera = FoscamCamera(
            config_entry.data[CONF_HOST],
            config_entry.data[CONF_PORT],
            config_entry.data[CONF_USERNAME],
            config_entry.data[CONF_PASSWORD],
            verbose=False,
        )

        ret, response = await hass.async_add_executor_job(camera.get_port_info)

        rtsp_port = DEFAULT_RTSP_PORT

        if ret != 0:
            rtsp_port = response.get("rtspPort") or response.get("mediaPort")

        config_entry.data = {**config_entry.data, CONF_RTSP_PORT: rtsp_port}

        # Change entry version
        config_entry.version = 2

    LOGGER.info("Migration to version %s successful", config_entry.version)

    return True
Ejemplo n.º 18
0
async def async_migrate_entry(hass, config_entry: ConfigEntry):
    """Migrate old entry."""
    LOGGER.debug("Migrating from version %s", config_entry.version)

    if config_entry.version == 1:
        # move from everything in data to splitting data and options
        save_data = config_entry.data
        config_keys = [CONF_HOST, CONF_PORT]
        config_entry.data = MappingProxyType({
            CONF_HOST:
            save_data[CONF_HOST],
            CONF_PORT:
            save_data.get(CONF_PORT, DEFAULT_PORT),
        })
        config_entry.options = MappingProxyType({
            key: value
            for (key, value) in save_data.items() if key not in config_keys
        })
        config_entry.version = 2

    LOGGER.info("Migration to version %s successful", config_entry.version)

    return True
Ejemplo n.º 19
0
async def async_migrate_entry(hass, config_entry: ConfigEntry):
    """Migrate old entry."""
    LOGGER.debug("Migrating config entry from version %s", config_entry.version)

    if config_entry.version == 1:

        new = get_merged_config(config_entry)

        only_publish_changed = new.get(CONF_ONLY_PUBLISH_CHANGED, False)
        new[CONF_PUBLISH_MODE] = (
            PUBLISH_MODE_ANY_CHANGES if only_publish_changed else PUBLISH_MODE_ALL
        )

        if CONF_ONLY_PUBLISH_CHANGED in new:
            del new[CONF_ONLY_PUBLISH_CHANGED]

        config_entry.data = {**new}

        config_entry.version = 2

    LOGGER.info("Migration to version %s successful", config_entry.version)

    return True
Ejemplo n.º 20
0
async def async_migrate_entry(hass, config_entry: ConfigEntry):
    """Migrate old entry."""
    _LOGGER.debug("Migrating from version %s to version %s",
                  config_entry.version, CONFIGFLOW_VERSION)

    new_data = {**config_entry.data}
    new_options = {**config_entry.options}

    if config_entry.version <= 2:
        _LOGGER.warning(
            "Impossible to migrate config version from version %s to version %s.\r\nPlease consider to delete and recreate the entry.",
            config_entry.version, CONFIGFLOW_VERSION)
        return False
    elif config_entry.version == 3:
        config_entry.unique_id = config_entry.data["unique_id"]
        del new_data["unique_id"]
        config_entry.version = CONFIGFLOW_VERSION
        config_entry.data = {**new_data}
        _LOGGER.info("Migration of entry %s done to version %s",
                     config_entry.title, config_entry.version)
        return True

    _LOGGER.info("Migration not required")
    return True
Ejemplo n.º 21
0
async def async_migrate_entry(hass, entry: ConfigEntry):
    """Migrate to latest config format."""

    CONF_TYPE_AUTO = "auto"
    CONF_DISPLAY_LIGHT = "display_light"
    CONF_CHILD_LOCK = "child_lock"

    if entry.version == 1:
        # Removal of Auto detection.
        config = {**entry.data, **entry.options, "name": entry.title}
        opts = {**entry.options}
        if config[CONF_TYPE] == CONF_TYPE_AUTO:
            device = setup_device(hass, config)
            config[CONF_TYPE] = await device.async_inferred_type()
            if config[CONF_TYPE] is None:
                return False

        entry.data = {
            CONF_DEVICE_ID: config[CONF_DEVICE_ID],
            CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
            CONF_HOST: config[CONF_HOST],
        }
        if CONF_CHILD_LOCK in config:
            opts.pop(CONF_CHILD_LOCK, False)
            opts[CONF_LOCK] = config[CONF_CHILD_LOCK]
        if CONF_DISPLAY_LIGHT in config:
            opts.pop(CONF_DISPLAY_LIGHT, False)
            opts[CONF_LIGHT] = config[CONF_DISPLAY_LIGHT]

        entry.options = {**opts}
        entry.version = 2

    if entry.version == 2:
        # CONF_TYPE is not configurable, move it from options to the main config.
        config = {**entry.data, **entry.options, "name": entry.title}
        opts = {**entry.options}
        # Ensure type has been migrated.  Some users are reporting errors which
        # suggest it was removed completely.  But that is probably due to
        # overwriting options without CONF_TYPE.
        if config.get(CONF_TYPE, CONF_TYPE_AUTO) == CONF_TYPE_AUTO:
            device = setup_device(hass, config)
            config[CONF_TYPE] = await device.async_inferred_type()
            if config[CONF_TYPE] is None:
                return False
        entry.data = {
            CONF_DEVICE_ID: config[CONF_DEVICE_ID],
            CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
            CONF_HOST: config[CONF_HOST],
            CONF_TYPE: config[CONF_TYPE],
        }
        opts.pop(CONF_TYPE, None)
        entry.options = {**opts}
        entry.version = 3

    if entry.version == 3:
        # Migrate to filename based config_type, to avoid needing to
        # parse config files to find the right one.
        config = {**entry.data, **entry.options, "name": entry.title}
        config_type = get_config(config[CONF_TYPE]).config_type

        # Special case for kogan_switch.  Consider also v2.
        if config_type == "smartplugv1":
            device = setup_device(hass, config)
            config_type = await device.async_inferred_type()
            if config_type != "smartplugv2":
                config_type = "smartplugv1"

        entry.data = {
            CONF_DEVICE_ID: config[CONF_DEVICE_ID],
            CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
            CONF_HOST: config[CONF_HOST],
            CONF_TYPE: config_type,
        }
        entry.version = 4

    return True
Ejemplo n.º 22
0
async def update_listener(hass: HomeAssistant, entry: ConfigEntry):
    """Update listener."""
    entry.data = entry.options
    coordinator: LockManagerCoordinator = hass.data[DOMAIN]
    await coordinator.update_entry(entry)
Ejemplo n.º 23
0
async def async_migrate_entry(hass, entry: ConfigEntry):
    """Migrate to latest config format."""

    CONF_TYPE_AUTO = "auto"
    CONF_DISPLAY_LIGHT = "display_light"
    CONF_CHILD_LOCK = "child_lock"

    if entry.version == 1:
        # Removal of Auto detection.
        config = {**entry.data, **entry.options, "name": entry.title}
        opts = {**entry.options}
        if config[CONF_TYPE] == CONF_TYPE_AUTO:
            device = setup_device(hass, config)
            config[CONF_TYPE] = await device.async_inferred_type()
            if config[CONF_TYPE] is None:
                _LOGGER.error(
                    f"Unable to determine type for device {config[CONF_DEVICE_ID]}."
                )
                return False

        entry.data = {
            CONF_DEVICE_ID: config[CONF_DEVICE_ID],
            CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
            CONF_HOST: config[CONF_HOST],
        }
        if CONF_CHILD_LOCK in config:
            opts.pop(CONF_CHILD_LOCK, False)
            opts[CONF_LOCK] = config[CONF_CHILD_LOCK]
        if CONF_DISPLAY_LIGHT in config:
            opts.pop(CONF_DISPLAY_LIGHT, False)
            opts[CONF_LIGHT] = config[CONF_DISPLAY_LIGHT]

        entry.options = {**opts}
        entry.version = 2

    if entry.version == 2:
        # CONF_TYPE is not configurable, move it from options to the main config.
        config = {**entry.data, **entry.options, "name": entry.title}
        opts = {**entry.options}
        # Ensure type has been migrated.  Some users are reporting errors which
        # suggest it was removed completely.  But that is probably due to
        # overwriting options without CONF_TYPE.
        if config.get(CONF_TYPE, CONF_TYPE_AUTO) == CONF_TYPE_AUTO:
            device = setup_device(hass, config)
            config[CONF_TYPE] = await device.async_inferred_type()
            if config[CONF_TYPE] is None:
                _LOGGER.error(
                    f"Unable to determine type for device {config[CONF_DEVICE_ID]}."
                )
                return False
        entry.data = {
            CONF_DEVICE_ID: config[CONF_DEVICE_ID],
            CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
            CONF_HOST: config[CONF_HOST],
            CONF_TYPE: config[CONF_TYPE],
        }
        opts.pop(CONF_TYPE, None)
        entry.options = {**opts}
        entry.version = 3

    if entry.version == 3:
        # Migrate to filename based config_type, to avoid needing to
        # parse config files to find the right one.
        config = {**entry.data, **entry.options, "name": entry.title}
        config_type = get_config(config[CONF_TYPE]).config_type

        # Special case for kogan_switch.  Consider also v2.
        if config_type == "smartplugv1":
            device = setup_device(hass, config)
            config_type = await device.async_inferred_type()
            if config_type != "smartplugv2":
                config_type = "smartplugv1"

        entry.data = {
            CONF_DEVICE_ID: config[CONF_DEVICE_ID],
            CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
            CONF_HOST: config[CONF_HOST],
            CONF_TYPE: config_type,
        }
        entry.version = 4

    if entry.version == 4:
        # Migrate indexes to entity id rather than type, to allow for multiple
        # entities of the same type for a device.
        config = {**entry.data, **entry.options, "name": entry.title}
        devcfg = get_config(config[CONF_TYPE])
        opts = {**entry.options}
        newopts = {**opts}
        entry.data = {
            CONF_DEVICE_ID: config[CONF_DEVICE_ID],
            CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
            CONF_HOST: config[CONF_HOST],
            CONF_TYPE: config[CONF_TYPE],
        }
        e = devcfg.primary_entity
        if e.config_id != e.entity:
            newopts.pop(e.entity, None)
            newopts[e.config_id] = opts.get(e.entity, False)

        for e in devcfg.secondary_entities():
            if e.config_id != e.entity:
                newopts.pop(e.entity, None)
                newopts[e.config_id] = opts.get(e.entity, False)

        entry.options = {**newopts}
        entry.version = 5

    if entry.version == 5:
        # Migrate unique ids of existing entities to new format
        old_id = entry.unique_id
        conf_file = get_config(entry.data[CONF_TYPE])
        if conf_file is None:
            _LOGGER.error(f"Configuration file for {entry.data[CONF_TYPE]} not found.")
            return False

        @callback
        def update_unique_id(entity_entry):
            """Update the unique id of an entity entry."""
            e = conf_file.primary_entity
            if e.entity != entity_entry.platform:
                for e in conf_file.secondary_entities():
                    if e.entity == entity_entry.platform:
                        break
            if e.entity == entity_entry.platform:
                new_id = e.unique_id(old_id)
                if new_id != old_id:
                    _LOGGER.info(
                        f"Migrating {e.entity} unique_id {old_id} to {new_id}."
                    )
                    return {
                        "new_unique_id": entity_entry.unique_id.replace(old_id, new_id)
                    }

        await async_migrate_entries(hass, entry.entry_id, update_unique_id)
        entry.version = 6

    if entry.version == 6:
        # Migrate some entity names to make them consistent for translations
        opts = {**entry.data, **entry.options}
        newopts = {**entry.options}
        master = opts.get("switch_main_switch")
        if master is not None:
            newopts.pop("switch_main_switch", None)
            newopts["switch_master"] = master
        outlet1 = opts.get("switch_left_outlet")
        outlet2 = opts.get("switch_right_outlet")
        outlet1 = opts.get("switch_wall_switch_1") if outlet1 is None else outlet1
        outlet2 = opts.get("switch_wall_switch_2") if outlet2 is None else outlet2
        if outlet1 is not None:
            newopts.pop("switch_left_outlet", None)
            newopts.pop("switch_wall_switch_1", None)
            newopts["switch_outlet_1"] = outlet1
        if outlet2 is not None:
            newopts.pop("switch_right_outlet", None)
            newopts.pop("switch_wall_switch_2", None)
            newopts["switch_outlet_2"] = outlet2

        entry.options = {**newopts}
        entry.version = 7

    return True
Ejemplo n.º 24
0
async def async_migrate_entry(_, config_entry: ConfigEntry) -> bool:
    """Migrate old entry."""
    _LOGGER.info("Migrating %s from version %s", config_entry.title,
                 config_entry.version)
    new_data = {**config_entry.data}
    new_options = {**config_entry.options}
    removed_data: Dict[str, Any] = {}
    removed_options: Dict[str, Any] = {}
    _LOGGER.debug("new_data %s", new_data)
    _LOGGER.debug("new_options %s", new_options)
    if config_entry.version == 1:
        to_remove = [
            "offset",
            "move_country_holidays",
            "holiday_in_week_move",
            "holiday_pop_named",
            "holiday_move_offset",
            "prov",
            "state",
            "observed",
            "exclude_dates",
            "include_dates",
        ]
        for remove in to_remove:
            if remove in new_data:
                removed_data[remove] = new_data[remove]
                del new_data[remove]
            if remove in new_options:
                removed_options[remove] = new_options[remove]
                del new_options[remove]
        if new_data.get(const.CONF_FREQUENCY) in const.MONTHLY_FREQUENCY:
            if const.CONF_WEEK_ORDER_NUMBER in new_data:
                new_data[const.CONF_WEEKDAY_ORDER_NUMBER] = new_data[
                    const.CONF_WEEK_ORDER_NUMBER]
                new_data[const.CONF_FORCE_WEEK_NUMBERS] = True
                del new_data[const.CONF_WEEK_ORDER_NUMBER]
            else:
                new_data[const.CONF_FORCE_WEEK_NUMBERS] = False
            _LOGGER.info("Updated data config for week_order_number")
        if new_options.get(const.CONF_FREQUENCY) in const.MONTHLY_FREQUENCY:
            if const.CONF_WEEK_ORDER_NUMBER in new_options:
                new_options[const.CONF_WEEKDAY_ORDER_NUMBER] = new_options[
                    const.CONF_WEEK_ORDER_NUMBER]
                new_options[const.CONF_FORCE_WEEK_NUMBERS] = True
                del new_options[const.CONF_WEEK_ORDER_NUMBER]
                _LOGGER.info("Updated options config for week_order_number")
            else:
                new_options[const.CONF_FORCE_WEEK_NUMBERS] = False
    if config_entry.version <= 4:
        if const.CONF_WEEKDAY_ORDER_NUMBER in new_data:
            new_data[const.CONF_WEEKDAY_ORDER_NUMBER] = list(
                map(str, new_data[const.CONF_WEEKDAY_ORDER_NUMBER]))
        if const.CONF_WEEKDAY_ORDER_NUMBER in new_options:
            new_options[const.CONF_WEEKDAY_ORDER_NUMBER] = list(
                map(str, new_options[const.CONF_WEEKDAY_ORDER_NUMBER]))
    config_entry.version = const.VERSION
    config_entry.data = {**new_data}
    config_entry.options = {**new_options}
    if removed_data:
        _LOGGER.error(
            "Removed data config %s. "
            "Please check the documentation how to configure the functionality.",
            removed_data,
        )
    if removed_options:
        _LOGGER.error(
            "Removed options config %s. "
            "Please check the documentation how to configure the functionality.",
            removed_options,
        )
    _LOGGER.info(
        "%s migration to version %s successful",
        config_entry.title,
        config_entry.version,
    )
    return True