mirror of
https://github.com/home-assistant/core.git
synced 2026-04-02 00:20:30 +01:00
Cleanup deprecated YAML import from vera (#165659)
This commit is contained in:
@@ -8,9 +8,7 @@ import logging
|
||||
|
||||
import pyvera as veraApi
|
||||
from requests.exceptions import RequestException
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONF_EXCLUDE,
|
||||
@@ -21,7 +19,6 @@ from homeassistant.const import (
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .common import (
|
||||
ControllerData,
|
||||
@@ -35,41 +32,7 @@ from .const import CONF_CONTROLLER, DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
VERA_ID_LIST_SCHEMA = vol.Schema([int])
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
vol.All(
|
||||
cv.deprecated(DOMAIN),
|
||||
{
|
||||
DOMAIN: vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_CONTROLLER): cv.url,
|
||||
vol.Optional(CONF_EXCLUDE, default=[]): VERA_ID_LIST_SCHEMA,
|
||||
vol.Optional(CONF_LIGHTS, default=[]): VERA_ID_LIST_SCHEMA,
|
||||
}
|
||||
)
|
||||
},
|
||||
),
|
||||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, base_config: ConfigType) -> bool:
|
||||
"""Set up for Vera controllers."""
|
||||
hass.data[DOMAIN] = {}
|
||||
|
||||
if not (config := base_config.get(DOMAIN)):
|
||||
return True
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data=config,
|
||||
)
|
||||
)
|
||||
|
||||
return True
|
||||
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
@@ -46,7 +46,7 @@ def set_controller_data(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry, data: ControllerData
|
||||
) -> None:
|
||||
"""Set controller data in hass data."""
|
||||
hass.data[DOMAIN][config_entry.entry_id] = data
|
||||
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = data
|
||||
|
||||
|
||||
class SubscriptionRegistry(pv.AbstractSubscriptionRegistry):
|
||||
|
||||
@@ -12,7 +12,6 @@ from requests.exceptions import RequestException
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import (
|
||||
SOURCE_IMPORT,
|
||||
SOURCE_USER,
|
||||
ConfigEntry,
|
||||
ConfigFlow,
|
||||
@@ -21,7 +20,6 @@ from homeassistant.config_entries import (
|
||||
)
|
||||
from homeassistant.const import CONF_EXCLUDE, CONF_LIGHTS, CONF_SOURCE
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.typing import VolDictType
|
||||
|
||||
from .const import CONF_CONTROLLER, CONF_LEGACY_UNIQUE_ID, DOMAIN
|
||||
@@ -131,31 +129,6 @@ class VeraFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
},
|
||||
)
|
||||
|
||||
async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
|
||||
"""Handle a flow initialized by import."""
|
||||
|
||||
# If there are entities with the legacy unique_id, then this imported config
|
||||
# should also use the legacy unique_id for entity creation.
|
||||
entity_registry = er.async_get(self.hass)
|
||||
use_legacy_unique_id = (
|
||||
len(
|
||||
[
|
||||
entry
|
||||
for entry in entity_registry.entities.values()
|
||||
if entry.platform == DOMAIN and entry.unique_id.isdigit()
|
||||
]
|
||||
)
|
||||
> 0
|
||||
)
|
||||
|
||||
return await self.async_step_finish(
|
||||
{
|
||||
**import_data,
|
||||
CONF_SOURCE: SOURCE_IMPORT,
|
||||
CONF_LEGACY_UNIQUE_ID: use_legacy_unique_id,
|
||||
}
|
||||
)
|
||||
|
||||
async def async_step_finish(self, config: dict[str, Any]) -> ConfigFlowResult:
|
||||
"""Validate and create config entry."""
|
||||
base_url = config[CONF_CONTROLLER] = config[CONF_CONTROLLER].rstrip("/")
|
||||
|
||||
@@ -16,7 +16,6 @@ from homeassistant.components.vera.const import (
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
@@ -39,7 +38,6 @@ class ComponentData(NamedTuple):
|
||||
class ConfigSource(Enum):
|
||||
"""Source of configuration."""
|
||||
|
||||
FILE = "file"
|
||||
CONFIG_FLOW = "config_flow"
|
||||
CONFIG_ENTRY = "config_entry"
|
||||
|
||||
@@ -142,16 +140,6 @@ class ComponentFactory:
|
||||
|
||||
self.vera_controller_class_mock.return_value = controller
|
||||
|
||||
hass_config = {}
|
||||
|
||||
# Setup component through config file import.
|
||||
if controller_config.config_source == ConfigSource.FILE:
|
||||
hass_config[DOMAIN] = component_config
|
||||
|
||||
# Setup Home Assistant.
|
||||
assert await async_setup_component(hass, DOMAIN, hass_config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Setup component through config flow.
|
||||
if controller_config.config_source == ConfigSource.CONFIG_FLOW:
|
||||
await hass.config_entries.flow.async_init(
|
||||
|
||||
@@ -15,7 +15,6 @@ from homeassistant.components.vera.const import (
|
||||
from homeassistant.const import CONF_EXCLUDE, CONF_LIGHTS, CONF_SOURCE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
@@ -66,60 +65,6 @@ async def test_async_step_user_success(hass: HomeAssistant) -> None:
|
||||
assert entries
|
||||
|
||||
|
||||
async def test_async_step_import_success(hass: HomeAssistant) -> None:
|
||||
"""Test import step success."""
|
||||
with patch("pyvera.VeraController") as vera_controller_class_mock:
|
||||
controller = MagicMock()
|
||||
controller.refresh_data = MagicMock()
|
||||
controller.serial_number = "serial_number_1"
|
||||
vera_controller_class_mock.return_value = controller
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={CONF_CONTROLLER: "http://127.0.0.1:123/"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "http://127.0.0.1:123"
|
||||
assert result["data"] == {
|
||||
CONF_CONTROLLER: "http://127.0.0.1:123",
|
||||
CONF_SOURCE: config_entries.SOURCE_IMPORT,
|
||||
CONF_LEGACY_UNIQUE_ID: False,
|
||||
}
|
||||
assert result["result"].unique_id == controller.serial_number
|
||||
|
||||
|
||||
async def test_async_step_import_success_with_legacy_unique_id(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
) -> None:
|
||||
"""Test import step success with legacy unique id."""
|
||||
entity_registry.async_get_or_create(
|
||||
domain="switch", platform=DOMAIN, unique_id="12"
|
||||
)
|
||||
|
||||
with patch("pyvera.VeraController") as vera_controller_class_mock:
|
||||
controller = MagicMock()
|
||||
controller.refresh_data = MagicMock()
|
||||
controller.serial_number = "serial_number_1"
|
||||
vera_controller_class_mock.return_value = controller
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={CONF_CONTROLLER: "http://127.0.0.1:123/"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "http://127.0.0.1:123"
|
||||
assert result["data"] == {
|
||||
CONF_CONTROLLER: "http://127.0.0.1:123",
|
||||
CONF_SOURCE: config_entries.SOURCE_IMPORT,
|
||||
CONF_LEGACY_UNIQUE_ID: True,
|
||||
}
|
||||
assert result["result"].unique_id == controller.serial_number
|
||||
|
||||
|
||||
async def test_async_step_finish_error(hass: HomeAssistant) -> None:
|
||||
"""Test finish step with error."""
|
||||
with patch("pyvera.VeraController") as vera_controller_class_mock:
|
||||
@@ -129,7 +74,7 @@ async def test_async_step_finish_error(hass: HomeAssistant) -> None:
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
data={CONF_CONTROLLER: "http://127.0.0.1:123/"},
|
||||
)
|
||||
|
||||
|
||||
@@ -49,40 +49,12 @@ async def test_init(
|
||||
assert entry1.unique_id == "vera_first_serial_1"
|
||||
|
||||
|
||||
async def test_init_from_file(
|
||||
async def test_multiple_controllers(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
vera_component_factory: ComponentFactory,
|
||||
) -> None:
|
||||
"""Test function."""
|
||||
vera_device1: pv.VeraBinarySensor = MagicMock(spec=pv.VeraBinarySensor)
|
||||
vera_device1.device_id = 1
|
||||
vera_device1.vera_device_id = vera_device1.device_id
|
||||
vera_device1.name = "first_dev"
|
||||
vera_device1.is_tripped = False
|
||||
entity1_id = "binary_sensor.first_dev_1"
|
||||
|
||||
await vera_component_factory.configure_component(
|
||||
hass=hass,
|
||||
controller_config=new_simple_controller_config(
|
||||
config={CONF_CONTROLLER: "http://127.0.0.1:111"},
|
||||
config_source=ConfigSource.FILE,
|
||||
serial_number="first_serial",
|
||||
devices=(vera_device1,),
|
||||
),
|
||||
)
|
||||
|
||||
entry1 = entity_registry.async_get(entity1_id)
|
||||
assert entry1
|
||||
assert entry1.unique_id == "vera_first_serial_1"
|
||||
|
||||
|
||||
async def test_multiple_controllers_with_legacy_one(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
vera_component_factory: ComponentFactory,
|
||||
) -> None:
|
||||
"""Test multiple controllers with one legacy controller."""
|
||||
"""Test multiple controllers."""
|
||||
vera_device1: pv.VeraBinarySensor = MagicMock(spec=pv.VeraBinarySensor)
|
||||
vera_device1.device_id = 1
|
||||
vera_device1.vera_device_id = vera_device1.device_id
|
||||
@@ -106,7 +78,7 @@ async def test_multiple_controllers_with_legacy_one(
|
||||
hass=hass,
|
||||
controller_config=new_simple_controller_config(
|
||||
config={CONF_CONTROLLER: "http://127.0.0.1:111"},
|
||||
config_source=ConfigSource.FILE,
|
||||
config_source=ConfigSource.CONFIG_FLOW,
|
||||
serial_number="first_serial",
|
||||
devices=(vera_device1,),
|
||||
),
|
||||
@@ -124,7 +96,7 @@ async def test_multiple_controllers_with_legacy_one(
|
||||
|
||||
entry1 = entity_registry.async_get(entity1_id)
|
||||
assert entry1
|
||||
assert entry1.unique_id == "1"
|
||||
assert entry1.unique_id == "vera_first_serial_1"
|
||||
|
||||
entry2 = entity_registry.async_get(entity2_id)
|
||||
assert entry2
|
||||
|
||||
Reference in New Issue
Block a user