Remove deprecated yaml config from Steam (#74805)

This commit is contained in:
Robert Hillis 2022-07-09 10:43:50 -04:00 committed by GitHub
parent 825e696d26
commit e17db1fd0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 118 deletions

View File

@ -12,29 +12,16 @@ from homeassistant.const import CONF_API_KEY, Platform
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import config_validation as cv, entity_registry as er from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers.typing import ConfigType
from .const import ( from .const import CONF_ACCOUNT, CONF_ACCOUNTS, DOMAIN, LOGGER, PLACEHOLDERS
CONF_ACCOUNT,
CONF_ACCOUNTS,
DEFAULT_NAME,
DOMAIN,
LOGGER,
PLACEHOLDERS,
)
def validate_input( def validate_input(user_input: dict[str, str]) -> dict[str, str | int]:
user_input: dict[str, str | int], multi: bool = False
) -> list[dict[str, str | int]]:
"""Handle common flow input validation.""" """Handle common flow input validation."""
steam.api.key.set(user_input[CONF_API_KEY]) steam.api.key.set(user_input[CONF_API_KEY])
interface = steam.api.interface("ISteamUser") interface = steam.api.interface("ISteamUser")
if multi: names = interface.GetPlayerSummaries(steamids=user_input[CONF_ACCOUNT])
names = interface.GetPlayerSummaries(steamids=user_input[CONF_ACCOUNTS]) return names["response"]["players"]["player"][0]
else:
names = interface.GetPlayerSummaries(steamids=user_input[CONF_ACCOUNT])
return names["response"]["players"]["player"]
class SteamFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): class SteamFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
@ -62,8 +49,8 @@ class SteamFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
elif user_input is not None: elif user_input is not None:
try: try:
res = await self.hass.async_add_executor_job(validate_input, user_input) res = await self.hass.async_add_executor_job(validate_input, user_input)
if res[0] is not None: if res is not None:
name = str(res[0]["personaname"]) name = str(res["personaname"])
else: else:
errors["base"] = "invalid_account" errors["base"] = "invalid_account"
except (steam.api.HTTPError, steam.api.HTTPTimeoutError) as ex: except (steam.api.HTTPError, steam.api.HTTPTimeoutError) as ex:
@ -80,22 +67,10 @@ class SteamFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
await self.hass.config_entries.async_reload(entry.entry_id) await self.hass.config_entries.async_reload(entry.entry_id)
return self.async_abort(reason="reauth_successful") return self.async_abort(reason="reauth_successful")
self._abort_if_unique_id_configured() self._abort_if_unique_id_configured()
if self.source == config_entries.SOURCE_IMPORT:
res = await self.hass.async_add_executor_job(
validate_input, user_input, True
)
accounts_data = {
CONF_ACCOUNTS: {
acc["steamid"]: acc["personaname"] for acc in res
}
}
user_input.pop(CONF_ACCOUNTS)
else:
accounts_data = {CONF_ACCOUNTS: {user_input[CONF_ACCOUNT]: name}}
return self.async_create_entry( return self.async_create_entry(
title=name or DEFAULT_NAME, title=name,
data=user_input, data=user_input,
options=accounts_data, options={CONF_ACCOUNTS: {user_input[CONF_ACCOUNT]: name}},
) )
user_input = user_input or {} user_input = user_input or {}
return self.async_show_form( return self.async_show_form(
@ -114,18 +89,6 @@ class SteamFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
description_placeholders=PLACEHOLDERS, description_placeholders=PLACEHOLDERS,
) )
async def async_step_import(self, import_config: ConfigType) -> FlowResult:
"""Import a config entry from configuration.yaml."""
for entry in self._async_current_entries():
if entry.data[CONF_API_KEY] == import_config[CONF_API_KEY]:
return self.async_abort(reason="already_configured")
LOGGER.warning(
"Steam yaml config is now deprecated and has been imported. "
"Please remove it from your config"
)
import_config[CONF_ACCOUNT] = import_config[CONF_ACCOUNTS][0]
return await self.async_step_user(import_config)
async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult: async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
"""Handle a reauthorization flow request.""" """Handle a reauthorization flow request."""
self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"]) self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])

View File

@ -4,19 +4,11 @@ from __future__ import annotations
from datetime import datetime from datetime import datetime
from time import localtime, mktime from time import localtime, mktime
import voluptuous as vol from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_API_KEY
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType from homeassistant.helpers.typing import StateType
from homeassistant.util.dt import utc_from_timestamp from homeassistant.util.dt import utc_from_timestamp
from . import SteamEntity from . import SteamEntity
@ -31,31 +23,9 @@ from .const import (
) )
from .coordinator import SteamDataUpdateCoordinator from .coordinator import SteamDataUpdateCoordinator
# Deprecated in Home Assistant 2022.5
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_ACCOUNTS, default=[]): vol.All(cv.ensure_list, [cv.string]),
}
)
PARALLEL_UPDATES = 1 PARALLEL_UPDATES = 1
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Twitch sensor from yaml."""
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
)
)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: ConfigEntry,

View File

@ -30,15 +30,6 @@ CONF_OPTIONS_2 = {
} }
} }
CONF_IMPORT_OPTIONS = {
CONF_ACCOUNTS: {
ACCOUNT_1: ACCOUNT_NAME_1,
ACCOUNT_2: ACCOUNT_NAME_2,
}
}
CONF_IMPORT_DATA = {CONF_API_KEY: API_KEY, CONF_ACCOUNTS: [ACCOUNT_1, ACCOUNT_2]}
def create_entry(hass: HomeAssistant) -> MockConfigEntry: def create_entry(hass: HomeAssistant) -> MockConfigEntry:
"""Add config entry in Home Assistant.""" """Add config entry in Home Assistant."""

View File

@ -5,7 +5,7 @@ import steam
from homeassistant import data_entry_flow from homeassistant import data_entry_flow
from homeassistant.components.steam_online.const import CONF_ACCOUNTS, DOMAIN from homeassistant.components.steam_online.const import CONF_ACCOUNTS, DOMAIN
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_REAUTH, SOURCE_USER from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
from homeassistant.const import CONF_API_KEY, CONF_SOURCE from homeassistant.const import CONF_API_KEY, CONF_SOURCE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
@ -15,8 +15,6 @@ from . import (
ACCOUNT_2, ACCOUNT_2,
ACCOUNT_NAME_1, ACCOUNT_NAME_1,
CONF_DATA, CONF_DATA,
CONF_IMPORT_DATA,
CONF_IMPORT_OPTIONS,
CONF_OPTIONS, CONF_OPTIONS,
CONF_OPTIONS_2, CONF_OPTIONS_2,
create_entry, create_entry,
@ -137,34 +135,6 @@ async def test_flow_reauth(hass: HomeAssistant) -> None:
assert entry.data == new_conf assert entry.data == new_conf
async def test_flow_import(hass: HomeAssistant) -> None:
"""Test import step."""
with patch_interface():
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=CONF_IMPORT_DATA,
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["title"] == ACCOUNT_NAME_1
assert result["data"] == CONF_DATA
assert result["options"] == CONF_IMPORT_OPTIONS
assert result["result"].unique_id == ACCOUNT_1
async def test_flow_import_already_configured(hass: HomeAssistant) -> None:
"""Test import step already configured."""
create_entry(hass)
with patch_interface():
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=CONF_IMPORT_DATA,
)
assert result["type"] == data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "already_configured"
async def test_options_flow(hass: HomeAssistant) -> None: async def test_options_flow(hass: HomeAssistant) -> None:
"""Test updating options.""" """Test updating options."""
entry = create_entry(hass) entry = create_entry(hass)