Remove YAML support from nut (#45276)

pull/45282/head
J. Nick Koston 2021-01-18 08:16:42 -06:00 committed by GitHub
parent 8e0addd216
commit c621c0fa5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 110 deletions

View File

@ -136,22 +136,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
}
return await self.async_step_user()
async def async_step_import(self, user_input=None):
"""Handle the import."""
errors = {}
if user_input is not None:
if self._host_port_alias_already_configured(user_input):
return self.async_abort(reason="already_configured")
_, errors = await self._async_validate_or_error(user_input)
if not errors:
title = _format_host_port_alias(user_input)
return self.async_create_entry(title=title, data=user_input)
return self.async_show_form(
step_id="user", data_schema=_base_schema({}), errors=errors
)
async def async_step_user(self, user_input=None):
"""Handle the user input."""
errors = {}

View File

@ -1,29 +1,11 @@
"""Provides a sensor to track various status aspects of a UPS."""
import logging
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.const import (
ATTR_STATE,
CONF_ALIAS,
CONF_HOST,
CONF_NAME,
CONF_PASSWORD,
CONF_PORT,
CONF_RESOURCES,
CONF_USERNAME,
STATE_UNKNOWN,
)
import homeassistant.helpers.config_validation as cv
from homeassistant.const import ATTR_STATE, CONF_RESOURCES, STATE_UNKNOWN
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
COORDINATOR,
DEFAULT_HOST,
DEFAULT_NAME,
DEFAULT_PORT,
DOMAIN,
KEY_STATUS,
KEY_STATUS_DISPLAY,
@ -44,29 +26,6 @@ from .const import (
_LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_ALIAS): cv.string,
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_PASSWORD): cv.string,
vol.Required(CONF_RESOURCES): vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
}
)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Import the platform into a config entry."""
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
)
)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the NUT sensors."""

View File

@ -212,15 +212,41 @@ async def test_form_user_multiple_ups(hass):
assert len(mock_setup_entry.mock_calls) == 2
async def test_form_import(hass):
"""Test we get the form with import source."""
async def test_form_user_one_ups_with_ignored_entry(hass):
"""Test we can setup a new one when there is an ignored one."""
ignored_entry = MockConfigEntry(
domain=DOMAIN, data={}, source=config_entries.SOURCE_IGNORE
)
ignored_entry.add_to_hass(hass)
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["errors"] == {}
mock_pynut = _get_mock_pynutclient(
list_vars={"battery.voltage": "serial"},
list_ups={"ups1": "UPS 1", "ups2": "UPS2"},
list_vars={"battery.voltage": "voltage", "ups.status": "OL"}, list_ups=["ups1"]
)
with patch(
"homeassistant.components.nut.PyNUTClient",
return_value=mock_pynut,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.1.1.1",
"username": "test-username",
"password": "test-password",
"port": 2222,
},
)
assert result2["step_id"] == "resources"
assert result2["type"] == "form"
with patch(
"homeassistant.components.nut.PyNUTClient",
return_value=mock_pynut,
@ -230,62 +256,25 @@ async def test_form_import(hass):
"homeassistant.components.nut.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={
"host": "localhost",
"port": 123,
"name": "name",
"resources": ["battery.charge"],
},
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{"resources": ["battery.voltage", "ups.status", "ups.status.display"]},
)
await hass.async_block_till_done()
assert result["type"] == "create_entry"
assert result["title"] == "localhost:123"
assert result["data"] == {
"host": "localhost",
"port": 123,
"name": "name",
"resources": ["battery.charge"],
assert result3["type"] == "create_entry"
assert result3["title"] == "1.1.1.1:2222"
assert result3["data"] == {
"host": "1.1.1.1",
"password": "test-password",
"port": 2222,
"resources": ["battery.voltage", "ups.status", "ups.status.display"],
"username": "test-username",
}
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_import_dupe(hass):
"""Test we get abort on duplicate import."""
await setup.async_setup_component(hass, "persistent_notification", {})
entry = MockConfigEntry(domain=DOMAIN, data=VALID_CONFIG)
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=VALID_CONFIG
)
assert result["type"] == "abort"
assert result["reason"] == "already_configured"
async def test_form_import_with_ignored_entry(hass):
"""Test we get abort on duplicate import when there is an ignored one."""
await setup.async_setup_component(hass, "persistent_notification", {})
entry = MockConfigEntry(domain=DOMAIN, data=VALID_CONFIG)
entry.add_to_hass(hass)
ignored_entry = MockConfigEntry(
domain=DOMAIN, data={}, source=config_entries.SOURCE_IGNORE
)
ignored_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=VALID_CONFIG
)
assert result["type"] == "abort"
assert result["reason"] == "already_configured"
async def test_form_cannot_connect(hass):
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(