Remove deprecated apcupsd YAML config (#83801)

This commit is contained in:
Yuxin Wang 2022-12-13 02:52:05 -05:00 committed by GitHub
parent 9733d31cd0
commit 677f0dc335
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 6 additions and 174 deletions

View file

@ -6,13 +6,11 @@ import logging
from typing import Any, Final
from apcaccess import status
import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PORT, Platform
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType
from homeassistant.util import Throttle
_LOGGER = logging.getLogger(__name__)
@ -22,51 +20,7 @@ VALUE_ONLINE: Final = 8
PLATFORMS: Final = (Platform.BINARY_SENSOR, Platform.SENSOR)
MIN_TIME_BETWEEN_UPDATES: Final = timedelta(seconds=60)
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Optional(CONF_HOST, default="localhost"): cv.string,
vol.Optional(CONF_PORT, default=3551): cv.port,
}
)
},
extra=vol.ALLOW_EXTRA,
)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up integration from legacy YAML configurations."""
conf = config.get(DOMAIN)
if conf is None:
return True
# We only import configs from YAML if it hasn't been imported. If there is a config
# entry marked with SOURCE_IMPORT, it means the YAML config has been imported.
for entry in hass.config_entries.async_entries(DOMAIN):
if entry.source == SOURCE_IMPORT:
return True
# Since the YAML configuration for apcupsd consists of two parts:
# apcupsd:
# host: xxx
# port: xxx
# sensor:
# - platform: apcupsd
# resource:
# - resource_1
# - resource_2
# - ...
# Here at the integration set up we do not have the entire information to be
# imported to config flow yet. So we temporarily store the configuration to
# hass.data[DOMAIN] under a special entry_id SOURCE_IMPORT (which shouldn't
# conflict with other entry ids). Later when the sensor platform setup is
# called we gather the resources information and from there we start the
# actual config entry imports.
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][SOURCE_IMPORT] = conf
return True
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:

View file

@ -78,9 +78,3 @@ class ConfigFlowHandler(ConfigFlow, domain=DOMAIN):
title=title,
data=user_input,
)
async def async_step_import(self, conf: dict[str, Any]) -> FlowResult:
"""Import a configuration from yaml configuration."""
# If we are importing from YAML configuration, user_input could contain a
# CONF_RESOURCES with a list of resources (sensors) to be enabled.
return await self.async_step_user(user_input=conf)

View file

@ -4,19 +4,14 @@ from __future__ import annotations
import logging
from apcaccess.status import ALL_UNITS
import voluptuous as vol
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_HOST,
CONF_PORT,
CONF_RESOURCES,
PERCENTAGE,
UnitOfApparentPower,
UnitOfElectricCurrent,
@ -27,11 +22,8 @@ from homeassistant.const import (
UnitOfTime,
)
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN, APCUPSdData
@ -429,75 +421,6 @@ INFERRED_UNITS = {
" Percent Load Capacity": PERCENTAGE,
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_RESOURCES, default=[]): vol.All(
cv.ensure_list, [vol.In([desc.key for desc in SENSORS.values()])]
)
}
)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Import the configurations from YAML to config flows."""
# We only import configs from YAML if it hasn't been imported. If there is a config
# entry marked with SOURCE_IMPORT, it means the YAML config has been imported.
for entry in hass.config_entries.async_entries(DOMAIN):
if entry.source == SOURCE_IMPORT:
return
# This is the second step of YAML config imports, first see the comments in
# async_setup() of __init__.py to get an idea of how we import the YAML configs.
# Here we retrieve the partial YAML configs from the special entry id.
conf = hass.data[DOMAIN].get(SOURCE_IMPORT)
if conf is None:
return
_LOGGER.warning(
"Configuration of apcupsd in YAML is deprecated and will be "
"removed in Home Assistant 2022.12; Your existing configuration "
"has been imported into the UI automatically and can be safely removed "
"from your configuration.yaml file"
)
async_create_issue(
hass,
DOMAIN,
"deprecated_yaml",
breaks_in_ha_version="2022.12.0",
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key="deprecated_yaml",
)
# Remove the artificial entry since it's no longer needed.
hass.data[DOMAIN].pop(SOURCE_IMPORT)
# Our config flow supports CONF_RESOURCES and will properly import it to disable
# entities not listed in CONF_RESOURCES by default. Note that this designed to
# support YAML config import only (i.e., not shown in UI during setup).
conf[CONF_RESOURCES] = config[CONF_RESOURCES]
_LOGGER.debug(
"YAML configurations loaded with host %s, port %s and resources %s",
conf[CONF_HOST],
conf[CONF_PORT],
conf[CONF_RESOURCES],
)
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
)
)
return
async def async_setup_entry(
hass: HomeAssistant,
@ -511,28 +434,13 @@ async def async_setup_entry(
# lower cases throughout this integration.
available_resources: set[str] = {k.lower() for k, _ in data_service.status.items()}
# We use user-specified resources from imported YAML config (if available) to
# determine whether to enable the entity by default. Here, we first collect the
# specified resources
specified_resources = None
if (resources := config_entry.data.get(CONF_RESOURCES)) is not None:
assert isinstance(resources, list)
specified_resources = set(resources)
entities = []
for resource in available_resources:
if resource not in SENSORS:
_LOGGER.warning("Invalid resource from APCUPSd: %s", resource.upper())
continue
# To avoid breaking changes, we disable sensors not specified in resources.
description = SENSORS[resource]
enabled_by_default = description.entity_registry_enabled_default
if specified_resources is not None:
enabled_by_default = resource in specified_resources
entity = APCUPSdSensor(data_service, description, enabled_by_default)
entities.append(entity)
entities.append(APCUPSdSensor(data_service, SENSORS[resource]))
async_add_entities(entities, update_before_add=True)
@ -557,7 +465,6 @@ class APCUPSdSensor(SensorEntity):
self,
data_service: APCUPSdData,
description: SensorEntityDescription,
enabled_by_default: bool,
) -> None:
"""Initialize the sensor."""
# Set up unique id and device info if serial number is available.
@ -572,7 +479,6 @@ class APCUPSdSensor(SensorEntity):
)
self.entity_description = description
self._attr_entity_registry_enabled_default = enabled_by_default
self._data_service = data_service
def update(self) -> None:

View file

@ -5,8 +5,8 @@ from unittest.mock import patch
import pytest
from homeassistant.components.apcupsd import DOMAIN
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_RESOURCES, CONF_SOURCE
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SOURCE
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@ -161,25 +161,3 @@ async def test_flow_minimal_status(
assert result["data"] == CONF_DATA
assert result["title"] == expected_title
mock_setup.assert_called_once()
async def test_flow_import(hass: HomeAssistant) -> None:
"""Test successful creation of config entries via YAML import."""
with patch("apcaccess.status.parse", return_value=MOCK_STATUS), patch(
"apcaccess.status.get", return_value=b""
), _patch_setup() as mock_setup:
# Importing from YAML will create an extra field CONF_RESOURCES in the config
# entry, here we test if it is properly stored.
resources = ["MODEL"]
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=CONF_DATA | {CONF_RESOURCES: resources},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == MOCK_STATUS["MODEL"]
assert result["data"] == CONF_DATA | {CONF_RESOURCES: resources}
mock_setup.assert_called_once()