Remove deprecated yaml config from aurora abb (#62317)
This commit is contained in:
parent
c3a963e12a
commit
b869b680fb
3 changed files with 2 additions and 298 deletions
|
@ -80,18 +80,6 @@ class AuroraABBConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
self._com_ports_list = None
|
||||
self._default_com_port = None
|
||||
|
||||
async def async_step_import(self, config: dict[str, Any]) -> FlowResult:
|
||||
"""Import a configuration from config.yaml."""
|
||||
if self.hass.config_entries.async_entries(DOMAIN):
|
||||
return self.async_abort(reason="already_configured")
|
||||
|
||||
conf = {}
|
||||
conf[CONF_PORT] = config["device"]
|
||||
conf[CONF_ADDRESS] = config["address"]
|
||||
# config["name"] from yaml is ignored.
|
||||
|
||||
return self.async_create_entry(title=DEFAULT_INTEGRATION_TITLE, data=conf)
|
||||
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
|
|
|
@ -6,29 +6,18 @@ import logging
|
|||
from typing import Any
|
||||
|
||||
from aurorapy.client import AuroraError, AuroraSerialClient
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_IMPORT
|
||||
from homeassistant.const import (
|
||||
CONF_ADDRESS,
|
||||
CONF_DEVICE,
|
||||
CONF_NAME,
|
||||
ENERGY_KILO_WATT_HOUR,
|
||||
POWER_WATT,
|
||||
TEMP_CELSIUS,
|
||||
)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.const import ENERGY_KILO_WATT_HOUR, POWER_WATT, TEMP_CELSIUS
|
||||
from homeassistant.helpers.entity import EntityCategory
|
||||
|
||||
from .aurora_device import AuroraEntity
|
||||
from .const import DEFAULT_ADDRESS, DOMAIN
|
||||
from .const import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -57,27 +46,6 @@ SENSOR_TYPES = [
|
|||
),
|
||||
]
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_DEVICE): cv.string,
|
||||
vol.Optional(CONF_ADDRESS, default=DEFAULT_ADDRESS): cv.positive_int,
|
||||
vol.Optional(CONF_NAME, default="Solar PV"): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up based on configuration.yaml (DEPRECATED)."""
|
||||
_LOGGER.warning(
|
||||
"Loading aurora_abb_powerone via platform config is deprecated; The configuration"
|
||||
" has been migrated to a config entry and can be safely removed from configuration.yaml"
|
||||
)
|
||||
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) -> None:
|
||||
"""Set up aurora_abb_powerone sensor based on a config entry."""
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
"""Test the Aurora ABB PowerOne Solar PV config flow."""
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from logging import INFO
|
||||
from unittest.mock import patch
|
||||
|
||||
|
@ -14,24 +12,11 @@ from homeassistant.components.aurora_abb_powerone.const import (
|
|||
ATTR_SERIAL_NUMBER,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import CONF_ADDRESS, CONF_PORT
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
TEST_DATA = {"device": "/dev/ttyUSB7", "address": 3, "name": "MyAuroraPV"}
|
||||
|
||||
|
||||
def _simulated_returns(index, global_measure=None):
|
||||
returns = {
|
||||
3: 45.678, # power
|
||||
21: 9.876, # temperature
|
||||
5: 12345, # energy
|
||||
}
|
||||
return returns[index]
|
||||
|
||||
|
||||
async def test_form(hass):
|
||||
"""Test we get the form."""
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
|
@ -164,240 +149,3 @@ async def test_form_invalid_com_ports(hass):
|
|||
)
|
||||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
assert len(mock_clientclose.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_import_invalid_com_ports(hass, caplog):
|
||||
"""Test we display correct info when the comport is invalid.."""
|
||||
|
||||
caplog.set_level(logging.ERROR)
|
||||
with patch(
|
||||
"aurorapy.client.AuroraSerialClient.connect",
|
||||
side_effect=OSError(19, "...no such device..."),
|
||||
return_value=None,
|
||||
):
|
||||
await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=TEST_DATA
|
||||
)
|
||||
configs = hass.config_entries.async_entries(DOMAIN)
|
||||
assert len(configs) == 1
|
||||
entry = configs[0]
|
||||
assert entry.state == ConfigEntryState.SETUP_ERROR
|
||||
assert "Failed to connect to inverter: " in caplog.text
|
||||
|
||||
|
||||
async def test_import_com_port_wont_open(hass):
|
||||
"""Test we display correct info when comport won't open."""
|
||||
|
||||
with patch(
|
||||
"aurorapy.client.AuroraSerialClient.connect",
|
||||
side_effect=AuroraError("..could not open port..."),
|
||||
):
|
||||
await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=TEST_DATA
|
||||
)
|
||||
configs = hass.config_entries.async_entries(DOMAIN)
|
||||
assert len(configs) == 1
|
||||
entry = configs[0]
|
||||
assert entry.state == ConfigEntryState.SETUP_ERROR
|
||||
|
||||
|
||||
async def test_import_other_oserror(hass):
|
||||
"""Test we display correct info when comport won't open."""
|
||||
|
||||
with patch(
|
||||
"aurorapy.client.AuroraSerialClient.connect",
|
||||
side_effect=OSError(18, "...another error..."),
|
||||
):
|
||||
await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=TEST_DATA
|
||||
)
|
||||
configs = hass.config_entries.async_entries(DOMAIN)
|
||||
assert len(configs) == 1
|
||||
entry = configs[0]
|
||||
assert entry.state == ConfigEntryState.SETUP_ERROR
|
||||
|
||||
|
||||
# Tests below can be deleted after deprecation period is finished.
|
||||
async def test_import_day(hass):
|
||||
"""Test .yaml import when the inverter is able to communicate."""
|
||||
|
||||
with patch("aurorapy.client.AuroraSerialClient.connect", return_value=None,), patch(
|
||||
"aurorapy.client.AuroraSerialClient.serial_number",
|
||||
return_value="9876543",
|
||||
), patch(
|
||||
"aurorapy.client.AuroraSerialClient.version",
|
||||
return_value="9.8.7.6",
|
||||
), patch(
|
||||
"aurorapy.client.AuroraSerialClient.pn",
|
||||
return_value="A.B.C",
|
||||
), patch(
|
||||
"aurorapy.client.AuroraSerialClient.firmware",
|
||||
return_value="1.234",
|
||||
) as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=TEST_DATA
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"][CONF_PORT] == "/dev/ttyUSB7"
|
||||
assert result["data"][CONF_ADDRESS] == 3
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_import_night(hass):
|
||||
"""Test .yaml import when the inverter is inaccessible (e.g. darkness)."""
|
||||
|
||||
# First time round, no response.
|
||||
with patch(
|
||||
"aurorapy.client.AuroraSerialClient.connect",
|
||||
side_effect=AuroraError("No response after"),
|
||||
) as mock_connect:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=TEST_DATA
|
||||
)
|
||||
|
||||
configs = hass.config_entries.async_entries(DOMAIN)
|
||||
assert len(configs) == 1
|
||||
entry = configs[0]
|
||||
assert not entry.unique_id
|
||||
assert entry.state == ConfigEntryState.SETUP_RETRY
|
||||
|
||||
assert len(mock_connect.mock_calls) == 1
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"][CONF_PORT] == "/dev/ttyUSB7"
|
||||
assert result["data"][CONF_ADDRESS] == 3
|
||||
|
||||
# Second time round, talking this time.
|
||||
with patch("aurorapy.client.AuroraSerialClient.connect", return_value=None,), patch(
|
||||
"aurorapy.client.AuroraSerialClient.serial_number",
|
||||
return_value="9876543",
|
||||
), patch(
|
||||
"aurorapy.client.AuroraSerialClient.version",
|
||||
return_value="9.8.7.6",
|
||||
), patch(
|
||||
"aurorapy.client.AuroraSerialClient.pn",
|
||||
return_value="A.B.C",
|
||||
), patch(
|
||||
"aurorapy.client.AuroraSerialClient.firmware",
|
||||
return_value="1.234",
|
||||
), patch(
|
||||
"aurorapy.client.AuroraSerialClient.measure",
|
||||
side_effect=_simulated_returns,
|
||||
):
|
||||
# Wait >5seconds for the config to auto retry.
|
||||
async_fire_time_changed(hass, utcnow() + timedelta(seconds=6))
|
||||
await hass.async_block_till_done()
|
||||
assert entry.state == ConfigEntryState.LOADED
|
||||
assert entry.unique_id
|
||||
|
||||
assert len(mock_connect.mock_calls) == 1
|
||||
power = hass.states.get("sensor.power_output")
|
||||
assert power
|
||||
assert power.state == "45.7"
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
|
||||
|
||||
async def test_import_night_then_user(hass):
|
||||
"""Attempt yaml import and fail (dark), but user sets up manually before auto retry."""
|
||||
|
||||
# First time round, no response.
|
||||
with patch(
|
||||
"aurorapy.client.AuroraSerialClient.connect",
|
||||
side_effect=AuroraError("No response after"),
|
||||
) as mock_connect:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=TEST_DATA
|
||||
)
|
||||
|
||||
configs = hass.config_entries.async_entries(DOMAIN)
|
||||
assert len(configs) == 1
|
||||
entry = configs[0]
|
||||
assert not entry.unique_id
|
||||
assert entry.state == ConfigEntryState.SETUP_RETRY
|
||||
|
||||
assert len(mock_connect.mock_calls) == 1
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"][CONF_PORT] == "/dev/ttyUSB7"
|
||||
assert result["data"][CONF_ADDRESS] == 3
|
||||
|
||||
# Failed once, now simulate the user initiating config flow with valid settings.
|
||||
fakecomports = []
|
||||
fakecomports.append(list_ports_common.ListPortInfo("/dev/ttyUSB7"))
|
||||
with patch(
|
||||
"serial.tools.list_ports.comports",
|
||||
return_value=fakecomports,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch("aurorapy.client.AuroraSerialClient.connect", return_value=None,), patch(
|
||||
"aurorapy.client.AuroraSerialClient.serial_number",
|
||||
return_value="9876543",
|
||||
), patch(
|
||||
"aurorapy.client.AuroraSerialClient.version",
|
||||
return_value="9.8.7.6",
|
||||
), patch(
|
||||
"aurorapy.client.AuroraSerialClient.pn",
|
||||
return_value="A.B.C",
|
||||
), patch(
|
||||
"aurorapy.client.AuroraSerialClient.firmware",
|
||||
return_value="1.234",
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_PORT: "/dev/ttyUSB7", CONF_ADDRESS: 7},
|
||||
)
|
||||
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 2
|
||||
|
||||
# Now retry yaml - it should fail with duplicate
|
||||
with patch("aurorapy.client.AuroraSerialClient.connect", return_value=None,), patch(
|
||||
"aurorapy.client.AuroraSerialClient.serial_number",
|
||||
return_value="9876543",
|
||||
), patch(
|
||||
"aurorapy.client.AuroraSerialClient.version",
|
||||
return_value="9.8.7.6",
|
||||
), patch(
|
||||
"aurorapy.client.AuroraSerialClient.pn",
|
||||
return_value="A.B.C",
|
||||
), patch(
|
||||
"aurorapy.client.AuroraSerialClient.firmware",
|
||||
return_value="1.234",
|
||||
):
|
||||
# Wait >5seconds for the config to auto retry.
|
||||
async_fire_time_changed(hass, utcnow() + timedelta(seconds=6))
|
||||
await hass.async_block_till_done()
|
||||
assert entry.state == ConfigEntryState.NOT_LOADED
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
|
||||
|
||||
async def test_import_already_existing(hass):
|
||||
"""Test configuration.yaml import when already configured."""
|
||||
TESTDATA = {"device": "/dev/ttyUSB7", "address": 7, "name": "MyAuroraPV"}
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="MyAuroraPV",
|
||||
unique_id="0123456",
|
||||
data={
|
||||
CONF_PORT: "/dev/ttyUSB7",
|
||||
CONF_ADDRESS: 7,
|
||||
ATTR_FIRMWARE: "1.234",
|
||||
ATTR_MODEL: "9.8.7.6 (A.B.C)",
|
||||
ATTR_SERIAL_NUMBER: "9876543",
|
||||
"title": "PhotoVoltaic Inverters",
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=TESTDATA
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
|
Loading…
Add table
Reference in a new issue