|
| 1 | +"""The file++ component.""" |
| 2 | + |
| 3 | +from copy import deepcopy |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from homeassistant.components.notify import migrate_notify_issue |
| 7 | +from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry |
| 8 | +from homeassistant.const import ( |
| 9 | + CONF_FILE_PATH, |
| 10 | + CONF_NAME, |
| 11 | + CONF_PLATFORM, |
| 12 | + CONF_SCAN_INTERVAL, |
| 13 | + Platform, |
| 14 | +) |
| 15 | +from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant |
| 16 | +from homeassistant.exceptions import ConfigEntryNotReady |
| 17 | +from homeassistant.helpers import ( |
| 18 | + config_validation as cv, |
| 19 | + discovery, |
| 20 | + issue_registry as ir, |
| 21 | +) |
| 22 | +from homeassistant.helpers.typing import ConfigType |
| 23 | + |
| 24 | +from .const import DOMAIN |
| 25 | +from .notify import PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA |
| 26 | +from .sensor import PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA |
| 27 | + |
| 28 | +IMPORT_SCHEMA = { |
| 29 | + Platform.SENSOR: SENSOR_PLATFORM_SCHEMA, |
| 30 | + Platform.NOTIFY: NOTIFY_PLATFORM_SCHEMA, |
| 31 | +} |
| 32 | + |
| 33 | +CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) |
| 34 | + |
| 35 | +PLATFORMS = [Platform.NOTIFY, Platform.SENSOR] |
| 36 | + |
| 37 | + |
| 38 | +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: |
| 39 | + """Set up the file integration.""" |
| 40 | + |
| 41 | + hass.data[DOMAIN] = config |
| 42 | + if hass.config_entries.async_entries(DOMAIN): |
| 43 | + # We skip import in case we already have config entries |
| 44 | + return True |
| 45 | + # The use of the legacy notify service was deprecated with HA Core 2024.6.0 |
| 46 | + # and will be removed with HA Core 2024.12 |
| 47 | + migrate_notify_issue(hass, DOMAIN, "File (Large)", "2024.12.0") |
| 48 | + # The YAML config was imported with HA Core 2024.6.0 and will be removed with |
| 49 | + # HA Core 2024.12 |
| 50 | + ir.async_create_issue( |
| 51 | + hass, |
| 52 | + HOMEASSISTANT_DOMAIN, |
| 53 | + f"deprecated_yaml_{DOMAIN}", |
| 54 | + breaks_in_ha_version="2024.12.0", |
| 55 | + is_fixable=False, |
| 56 | + issue_domain=DOMAIN, |
| 57 | + learn_more_url="https://www.home-assistant.io/integrations/file/", |
| 58 | + severity=ir.IssueSeverity.WARNING, |
| 59 | + translation_key="deprecated_yaml", |
| 60 | + translation_placeholders={ |
| 61 | + "domain": DOMAIN, |
| 62 | + "integration_title": "File (Large)", |
| 63 | + }, |
| 64 | + ) |
| 65 | + |
| 66 | + # Import the YAML config into separate config entries |
| 67 | + platforms_config: dict[Platform, list[ConfigType]] = { |
| 68 | + domain: config[domain] for domain in PLATFORMS if domain in config |
| 69 | + } |
| 70 | + for domain, items in platforms_config.items(): |
| 71 | + for item in items: |
| 72 | + if item[CONF_PLATFORM] == DOMAIN: |
| 73 | + file_config_item = IMPORT_SCHEMA[domain](item) |
| 74 | + file_config_item[CONF_PLATFORM] = domain |
| 75 | + if CONF_SCAN_INTERVAL in file_config_item: |
| 76 | + del file_config_item[CONF_SCAN_INTERVAL] |
| 77 | + hass.async_create_task( |
| 78 | + hass.config_entries.flow.async_init( |
| 79 | + DOMAIN, |
| 80 | + context={"source": SOURCE_IMPORT}, |
| 81 | + data=file_config_item, |
| 82 | + ) |
| 83 | + ) |
| 84 | + |
| 85 | + return True |
| 86 | + |
| 87 | + |
| 88 | +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
| 89 | + """Set up a file component entry.""" |
| 90 | + config = {**entry.data, **entry.options} |
| 91 | + filepath: str = config[CONF_FILE_PATH] |
| 92 | + if filepath and not await hass.async_add_executor_job( |
| 93 | + hass.config.is_allowed_path, filepath |
| 94 | + ): |
| 95 | + raise ConfigEntryNotReady( |
| 96 | + translation_domain=DOMAIN, |
| 97 | + translation_key="dir_not_allowed", |
| 98 | + translation_placeholders={"filename": filepath}, |
| 99 | + ) |
| 100 | + |
| 101 | + await hass.config_entries.async_forward_entry_setups( |
| 102 | + entry, [Platform(entry.data[CONF_PLATFORM])] |
| 103 | + ) |
| 104 | + entry.async_on_unload(entry.add_update_listener(update_listener)) |
| 105 | + if entry.data[CONF_PLATFORM] == Platform.NOTIFY and CONF_NAME in entry.data: |
| 106 | + # New notify entities are being setup through the config entry, |
| 107 | + # but during the deprecation period we want to keep the legacy notify platform, |
| 108 | + # so we forward the setup config through discovery. |
| 109 | + # Only the entities from yaml will still be available as legacy service. |
| 110 | + hass.async_create_task( |
| 111 | + discovery.async_load_platform( |
| 112 | + hass, |
| 113 | + Platform.NOTIFY, |
| 114 | + DOMAIN, |
| 115 | + config, |
| 116 | + hass.data[DOMAIN], |
| 117 | + ) |
| 118 | + ) |
| 119 | + |
| 120 | + return True |
| 121 | + |
| 122 | + |
| 123 | +async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
| 124 | + """Unload a config entry.""" |
| 125 | + return await hass.config_entries.async_unload_platforms( |
| 126 | + entry, [entry.data[CONF_PLATFORM]] |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: |
| 131 | + """Handle options update.""" |
| 132 | + await hass.config_entries.async_reload(entry.entry_id) |
| 133 | + |
| 134 | + |
| 135 | +async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: |
| 136 | + """Migrate config entry.""" |
| 137 | + if config_entry.version > 2: |
| 138 | + # Downgraded from future |
| 139 | + return False |
| 140 | + |
| 141 | + if config_entry.version < 2: |
| 142 | + # Move optional fields from data to options in config entry |
| 143 | + data: dict[str, Any] = deepcopy(dict(config_entry.data)) |
| 144 | + options = {} |
| 145 | + for key, value in config_entry.data.items(): |
| 146 | + if key not in (CONF_FILE_PATH, CONF_PLATFORM, CONF_NAME): |
| 147 | + data.pop(key) |
| 148 | + options[key] = value |
| 149 | + |
| 150 | + hass.config_entries.async_update_entry( |
| 151 | + config_entry, version=2, data=data, options=options |
| 152 | + ) |
| 153 | + return True |
0 commit comments