Remove deprecated yaml config from AsusWRT (#65904)

This commit is contained in:
ollo69 2022-02-06 16:03:04 +01:00 committed by GitHub
parent a0895f1bf2
commit 13699baa4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 178 deletions

View File

@ -1,123 +1,18 @@
"""Support for ASUSWRT devices.""" """Support for ASUSWRT devices."""
import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import EVENT_HOMEASSISTANT_STOP, Platform
CONF_HOST,
CONF_MODE,
CONF_PASSWORD,
CONF_PORT,
CONF_PROTOCOL,
CONF_SENSORS,
CONF_USERNAME,
EVENT_HOMEASSISTANT_STOP,
Platform,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.typing import ConfigType
from .const import ( from .const import DATA_ASUSWRT, DOMAIN
CONF_DNSMASQ,
CONF_INTERFACE,
CONF_REQUIRE_IP,
CONF_SSH_KEY,
DATA_ASUSWRT,
DEFAULT_DNSMASQ,
DEFAULT_INTERFACE,
DEFAULT_SSH_PORT,
DOMAIN,
MODE_AP,
MODE_ROUTER,
PROTOCOL_SSH,
PROTOCOL_TELNET,
)
from .router import AsusWrtRouter from .router import AsusWrtRouter
PLATFORMS = [Platform.DEVICE_TRACKER, Platform.SENSOR] PLATFORMS = [Platform.DEVICE_TRACKER, Platform.SENSOR]
CONF_PUB_KEY = "pub_key"
SECRET_GROUP = "Password or SSH Key"
SENSOR_TYPES = ["devices", "upload_speed", "download_speed", "download", "upload"]
CONFIG_SCHEMA = vol.Schema(
vol.All(
cv.deprecated(DOMAIN),
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Optional(CONF_PROTOCOL, default=PROTOCOL_SSH): vol.In(
[PROTOCOL_SSH, PROTOCOL_TELNET]
),
vol.Optional(CONF_MODE, default=MODE_ROUTER): vol.In(
[MODE_ROUTER, MODE_AP]
),
vol.Optional(CONF_PORT, default=DEFAULT_SSH_PORT): cv.port,
vol.Optional(CONF_REQUIRE_IP, default=True): cv.boolean,
vol.Exclusive(CONF_PASSWORD, SECRET_GROUP): cv.string,
vol.Exclusive(CONF_SSH_KEY, SECRET_GROUP): cv.isfile,
vol.Exclusive(CONF_PUB_KEY, SECRET_GROUP): cv.isfile,
vol.Optional(CONF_SENSORS): vol.All(
cv.ensure_list, [vol.In(SENSOR_TYPES)]
),
vol.Optional(CONF_INTERFACE, default=DEFAULT_INTERFACE): cv.string,
vol.Optional(CONF_DNSMASQ, default=DEFAULT_DNSMASQ): cv.string,
}
)
},
),
extra=vol.ALLOW_EXTRA,
)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the AsusWrt integration."""
if (conf := config.get(DOMAIN)) is None:
return True
# save the options from config yaml
options = {}
mode = conf.get(CONF_MODE, MODE_ROUTER)
for name, value in conf.items():
if name in [CONF_DNSMASQ, CONF_INTERFACE, CONF_REQUIRE_IP]:
if name == CONF_REQUIRE_IP and mode != MODE_AP:
continue
options[name] = value
hass.data[DOMAIN] = {"yaml_options": options}
# check if already configured
domains_list = hass.config_entries.async_domains()
if DOMAIN in domains_list:
return True
# remove not required config keys
if pub_key := conf.pop(CONF_PUB_KEY, ""):
conf[CONF_SSH_KEY] = pub_key
conf.pop(CONF_REQUIRE_IP, True)
conf.pop(CONF_SENSORS, {})
conf.pop(CONF_INTERFACE, "")
conf.pop(CONF_DNSMASQ, "")
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
)
)
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up AsusWrt platform.""" """Set up AsusWrt platform."""
# import options from yaml if empty
yaml_options = hass.data.get(DOMAIN, {}).pop("yaml_options", {})
if not entry.options and yaml_options:
hass.config_entries.async_update_entry(entry, options=yaml_options)
router = AsusWrtRouter(hass, entry) router = AsusWrtRouter(hass, entry)
await router.setup() await router.setup()

View File

@ -165,10 +165,6 @@ class AsusWrtFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
data=user_input, data=user_input,
) )
async def async_step_import(self, user_input=None):
"""Import a config entry."""
return await self.async_step_user(user_input)
@staticmethod @staticmethod
@callback @callback
def async_get_options_flow(config_entry): def async_get_options_flow(config_entry):

View File

@ -14,7 +14,7 @@ from homeassistant.components.asuswrt.const import (
DOMAIN, DOMAIN,
) )
from homeassistant.components.device_tracker.const import CONF_CONSIDER_HOME from homeassistant.components.device_tracker.const import CONF_CONSIDER_HOME
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import ( from homeassistant.const import (
CONF_HOST, CONF_HOST,
CONF_MODE, CONF_MODE,
@ -80,62 +80,6 @@ async def test_user(hass, connect):
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
async def test_import(hass, connect):
"""Test import step."""
with patch(
"homeassistant.components.asuswrt.async_setup_entry",
return_value=True,
) as mock_setup_entry, patch(
"homeassistant.components.asuswrt.config_flow.socket.gethostbyname",
return_value=IP_ADDRESS,
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=CONFIG_DATA,
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == HOST
assert result["data"] == CONFIG_DATA
assert len(mock_setup_entry.mock_calls) == 1
async def test_import_ssh(hass, connect):
"""Test import step with ssh file."""
config_data = CONFIG_DATA.copy()
config_data.pop(CONF_PASSWORD)
config_data[CONF_SSH_KEY] = SSH_KEY
with patch(
"homeassistant.components.asuswrt.async_setup_entry",
return_value=True,
) as mock_setup_entry, patch(
"homeassistant.components.asuswrt.config_flow.socket.gethostbyname",
return_value=IP_ADDRESS,
), patch(
"homeassistant.components.asuswrt.config_flow.os.path.isfile",
return_value=True,
), patch(
"homeassistant.components.asuswrt.config_flow.os.access",
return_value=True,
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=config_data,
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == HOST
assert result["data"] == config_data
assert len(mock_setup_entry.mock_calls) == 1
async def test_error_no_password_ssh(hass): async def test_error_no_password_ssh(hass):
"""Test we abort if component is already setup.""" """Test we abort if component is already setup."""
config_data = CONFIG_DATA.copy() config_data = CONFIG_DATA.copy()
@ -215,15 +159,6 @@ async def test_abort_if_already_setup(hass):
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "single_instance_allowed" assert result["reason"] == "single_instance_allowed"
# Should fail, same HOST (import)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=CONFIG_DATA,
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "single_instance_allowed"
async def test_on_connect_failed(hass): async def test_on_connect_failed(hass):
"""Test when we have errors connecting the router.""" """Test when we have errors connecting the router."""