mirror of
https://github.com/home-assistant/core.git
synced 2025-08-07 04:28:19 +00:00
Fix migration
This commit is contained in:
parent
ec8ca241c8
commit
de5145f5a6
@ -6,19 +6,28 @@ import asyncio
|
||||
from collections.abc import Coroutine
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from types import MappingProxyType
|
||||
from typing import Any
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.rest import RESOURCE_SCHEMA, create_rest_data_from_config
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.components.sensor import CONF_STATE_CLASS, DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntry, ConfigSubentry
|
||||
from homeassistant.const import (
|
||||
CONF_ATTRIBUTE,
|
||||
CONF_AUTHENTICATION,
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_HEADERS,
|
||||
CONF_NAME,
|
||||
CONF_PASSWORD,
|
||||
CONF_SCAN_INTERVAL,
|
||||
CONF_TIMEOUT,
|
||||
CONF_UNIQUE_ID,
|
||||
CONF_UNIT_OF_MEASUREMENT,
|
||||
CONF_USERNAME,
|
||||
CONF_VALUE_TEMPLATE,
|
||||
CONF_VERIFY_SSL,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
@ -35,7 +44,14 @@ from homeassistant.helpers.trigger_template_entity import (
|
||||
)
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .const import CONF_INDEX, CONF_SELECT, DEFAULT_SCAN_INTERVAL, DOMAIN, PLATFORMS
|
||||
from .const import (
|
||||
CONF_ENCODING,
|
||||
CONF_INDEX,
|
||||
CONF_SELECT,
|
||||
DEFAULT_SCAN_INTERVAL,
|
||||
DOMAIN,
|
||||
PLATFORMS,
|
||||
)
|
||||
from .coordinator import ScrapeCoordinator
|
||||
|
||||
type ScrapeConfigEntry = ConfigEntry[ScrapeCoordinator]
|
||||
@ -142,8 +158,32 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ScrapeConfigEntry) ->
|
||||
sensor_config = dict(sensor)
|
||||
title = sensor_config.pop(CONF_NAME)
|
||||
old_unique_id = sensor_config.pop(CONF_UNIQUE_ID)
|
||||
|
||||
sensor_config["advanced"] = {}
|
||||
for sensor_advanced_key in (
|
||||
CONF_ATTRIBUTE,
|
||||
CONF_VALUE_TEMPLATE,
|
||||
CONF_AVAILABILITY,
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_STATE_CLASS,
|
||||
CONF_UNIT_OF_MEASUREMENT,
|
||||
):
|
||||
if sensor_advanced_key in sensor_config:
|
||||
sensor_config["advanced"][sensor_advanced_key] = sensor_config.pop(
|
||||
sensor_advanced_key
|
||||
)
|
||||
|
||||
_LOGGER.debug(
|
||||
"Migrating sensor %s with unique id %s to sub config entry data %s",
|
||||
title,
|
||||
old_unique_id,
|
||||
sensor_config,
|
||||
)
|
||||
new_sub_entry = ConfigSubentry(
|
||||
data=sensor, subentry_type="entity", title=title, unique_id=None
|
||||
data=MappingProxyType(sensor_config),
|
||||
subentry_type="entity",
|
||||
title=title,
|
||||
unique_id=None,
|
||||
)
|
||||
old_to_new_sensor_id[old_unique_id] = new_sub_entry.subentry_id
|
||||
hass.config_entries.async_add_subentry(entry, new_sub_entry)
|
||||
@ -152,9 +192,19 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ScrapeConfigEntry) ->
|
||||
entity_reg = er.async_get(hass)
|
||||
entities = er.async_entries_for_config_entry(entity_reg, entry.entry_id)
|
||||
for entity in entities:
|
||||
_LOGGER.debug(
|
||||
(
|
||||
"Migrating entity %s with unique id %s to new unique id %s",
|
||||
entity.entity_id,
|
||||
entity.unique_id,
|
||||
old_to_new_sensor_id.get(entity.unique_id),
|
||||
)
|
||||
)
|
||||
if entity.unique_id in old_to_new_sensor_id:
|
||||
entity_reg.async_update_entity(
|
||||
entity.entity_id,
|
||||
config_entry_id=entry.entry_id,
|
||||
config_subentry_id=old_to_new_sensor_id[entity.unique_id],
|
||||
new_unique_id=old_to_new_sensor_id[entity.unique_id],
|
||||
)
|
||||
|
||||
@ -163,10 +213,17 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ScrapeConfigEntry) ->
|
||||
devices = dr.async_entries_for_config_entry(device_reg, entry.entry_id)
|
||||
for device in devices:
|
||||
for identifier in device.identifiers:
|
||||
_LOGGER.debug(
|
||||
"Migrating device %s with identifiers %s to new unique id %s",
|
||||
device.id,
|
||||
device.identifiers,
|
||||
old_to_new_sensor_id.get(identifier[1]),
|
||||
)
|
||||
device_unique_id = identifier[1]
|
||||
if device_unique_id in old_to_new_sensor_id:
|
||||
device_reg.async_update_device(
|
||||
device.id,
|
||||
add_config_entry_id=entry.entry_id,
|
||||
add_config_subentry_id=old_to_new_sensor_id[device_unique_id],
|
||||
new_identifiers={
|
||||
(DOMAIN, old_to_new_sensor_id[device_unique_id])
|
||||
@ -177,6 +234,30 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ScrapeConfigEntry) ->
|
||||
new_config_entry_data = dict(entry.options)
|
||||
new_config_entry_data.pop(SENSOR_DOMAIN)
|
||||
|
||||
# Update the resource config
|
||||
new_config_entry_data["auth"] = {}
|
||||
new_config_entry_data["advanced"] = {}
|
||||
for resource_advanced_key in (
|
||||
CONF_HEADERS,
|
||||
CONF_VERIFY_SSL,
|
||||
CONF_TIMEOUT,
|
||||
CONF_ENCODING,
|
||||
):
|
||||
if resource_advanced_key in new_config_entry_data:
|
||||
new_config_entry_data["advanced"][resource_advanced_key] = (
|
||||
new_config_entry_data.pop(resource_advanced_key)
|
||||
)
|
||||
for resource_auth_key in (CONF_AUTHENTICATION, CONF_USERNAME, CONF_PASSWORD):
|
||||
if resource_auth_key in new_config_entry_data:
|
||||
new_config_entry_data["auth"][resource_auth_key] = (
|
||||
new_config_entry_data.pop(resource_auth_key)
|
||||
)
|
||||
|
||||
_LOGGER.debug(
|
||||
"Migrating config entry %s from version 1 to version 2 with data %s",
|
||||
entry.entry_id,
|
||||
new_config_entry_data,
|
||||
)
|
||||
hass.config_entries.async_update_entry(
|
||||
entry, version=2, options=new_config_entry_data
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user