Fix webostv sources import from YAML (#64768)

pull/64776/head
Shay Levy 2022-01-23 19:23:29 +02:00 committed by GitHub
parent 53c39293a4
commit 94a89d520b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 21 deletions

View File

@ -221,27 +221,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True
def _async_migrate_options_from_data(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Migrate options from data."""
if entry.options:
return
config = entry.data
options = {}
# Get Preferred Sources
if sources := config.get(CONF_CUSTOMIZE, {}).get(CONF_SOURCES):
options[CONF_SOURCES] = sources
if not isinstance(sources, list):
options[CONF_SOURCES] = sources.split(",")
hass.config_entries.async_update_entry(entry, options=options)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set the config entry up."""
_async_migrate_options_from_data(hass, entry)
host = entry.data[CONF_HOST]
key = entry.data[CONF_CLIENT_SECRET]

View File

@ -10,7 +10,13 @@ import voluptuous as vol
from homeassistant import config_entries, data_entry_flow
from homeassistant.components import ssdp
from homeassistant.const import CONF_CLIENT_SECRET, CONF_HOST, CONF_NAME, CONF_UNIQUE_ID
from homeassistant.const import (
CONF_CLIENT_SECRET,
CONF_CUSTOMIZE,
CONF_HOST,
CONF_NAME,
CONF_UNIQUE_ID,
)
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import config_validation as cv
@ -61,8 +67,15 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
CONF_CLIENT_SECRET: import_info[CONF_CLIENT_SECRET],
}
self._abort_if_unique_id_configured()
options: dict[str, list[str]] | None = None
if sources := import_info.get(CONF_CUSTOMIZE, {}).get(CONF_SOURCES):
if not isinstance(sources, list):
sources = [s.strip() for s in sources.split(",")]
options = {CONF_SOURCES: sources}
_LOGGER.debug("WebOS Smart TV host %s imported from YAML config", self._host)
return self.async_create_entry(title=self._name, data=data)
return self.async_create_entry(title=self._name, data=data, options=options)
async def async_step_user(
self, user_input: dict[str, Any] | None = None

View File

@ -3,6 +3,7 @@ import dataclasses
from unittest.mock import Mock, patch
from aiowebostv import WebOsTvPairError
import pytest
from homeassistant import config_entries
from homeassistant.components import ssdp
@ -10,6 +11,7 @@ from homeassistant.components.webostv.const import CONF_SOURCES, DOMAIN
from homeassistant.config_entries import SOURCE_SSDP
from homeassistant.const import (
CONF_CLIENT_SECRET,
CONF_CUSTOMIZE,
CONF_HOST,
CONF_ICON,
CONF_NAME,
@ -71,6 +73,38 @@ async def test_import(hass, client):
assert result["reason"] == "already_configured"
@pytest.mark.parametrize(
"sources",
[
["Live TV", "Input01", "Input02"],
"Live TV, Input01 , Input02",
"Live TV,Input01 ,Input02",
],
)
async def test_import_sources(hass, client, sources):
"""Test import yaml config with sources list/csv."""
assert client
with patch("homeassistant.components.webostv.async_setup_entry", return_value=True):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={CONF_SOURCE: config_entries.SOURCE_IMPORT},
data={
**MOCK_YAML_CONFIG,
CONF_CUSTOMIZE: {
CONF_SOURCES: sources,
},
},
)
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "fake"
assert result["data"][CONF_HOST] == MOCK_YAML_CONFIG[CONF_HOST]
assert result["data"][CONF_CLIENT_SECRET] == MOCK_YAML_CONFIG[CONF_CLIENT_SECRET]
assert result["options"][CONF_SOURCES] == ["Live TV", "Input01", "Input02"]
assert result["result"].unique_id == MOCK_YAML_CONFIG[CONF_UNIQUE_ID]
async def test_form(hass, client):
"""Test we get the form."""
assert client