Fix LiteJet import config issue (#97679)

This commit is contained in:
G Johansson 2023-08-21 12:20:48 +02:00 committed by GitHub
parent 82b3ced4f1
commit fb56dd0615
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 23 deletions

View file

@ -6,10 +6,9 @@ import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_PORT, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, Event, HomeAssistant
from homeassistant.core import Event, HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType
from .const import CONF_EXCLUDE_NAMES, CONF_INCLUDE_SWITCHES, DOMAIN, PLATFORMS
@ -37,27 +36,13 @@ CONFIG_SCHEMA = vol.Schema(
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the LiteJet component."""
if DOMAIN in config and not hass.config_entries.async_entries(DOMAIN):
# No config entry exists and configuration.yaml config exists, trigger the import flow.
if DOMAIN in config:
# Configuration.yaml config exists, trigger the import flow.
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=config[DOMAIN]
)
)
async_create_issue(
hass,
HOMEASSISTANT_DOMAIN,
f"deprecated_yaml_{DOMAIN}",
breaks_in_ha_version="2024.2.0",
is_fixable=False,
issue_domain=DOMAIN,
severity=IssueSeverity.WARNING,
translation_key="deprecated_yaml",
translation_placeholders={
"domain": DOMAIN,
"integration_title": "LiteJet",
},
)
return True

View file

@ -9,9 +9,10 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_PORT
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, callback
from homeassistant.data_entry_flow import FlowResult, FlowResultType
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from .const import CONF_DEFAULT_TRANSITION, DOMAIN
@ -53,6 +54,21 @@ class LiteJetConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
) -> FlowResult:
"""Create a LiteJet config entry based upon user input."""
if self._async_current_entries():
if self.context["source"] == config_entries.SOURCE_IMPORT:
async_create_issue(
self.hass,
HOMEASSISTANT_DOMAIN,
f"deprecated_yaml_{DOMAIN}",
breaks_in_ha_version="2024.2.0",
is_fixable=False,
issue_domain=DOMAIN,
severity=IssueSeverity.WARNING,
translation_key="deprecated_yaml",
translation_placeholders={
"domain": DOMAIN,
"integration_title": "LiteJet",
},
)
return self.async_abort(reason="single_instance_allowed")
errors = {}
@ -62,6 +78,20 @@ class LiteJetConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
try:
system = await pylitejet.open(port)
except SerialException:
if self.context["source"] == config_entries.SOURCE_IMPORT:
async_create_issue(
self.hass,
DOMAIN,
"deprecated_yaml_serial_exception",
breaks_in_ha_version="2024.2.0",
is_fixable=False,
issue_domain=DOMAIN,
severity=IssueSeverity.ERROR,
translation_key="deprecated_yaml_serial_exception",
translation_placeholders={
"url": "/config/integrations/dashboard/add?domain=litejet"
},
)
errors[CONF_PORT] = "open_failed"
else:
await system.close()
@ -78,7 +108,24 @@ class LiteJetConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_import(self, import_data: dict[str, Any]) -> FlowResult:
"""Import litejet config from configuration.yaml."""
return self.async_create_entry(title=import_data[CONF_PORT], data=import_data)
new_data = {CONF_PORT: import_data[CONF_PORT]}
result = await self.async_step_user(new_data)
if result["type"] == FlowResultType.CREATE_ENTRY:
async_create_issue(
self.hass,
HOMEASSISTANT_DOMAIN,
f"deprecated_yaml_{DOMAIN}",
breaks_in_ha_version="2024.2.0",
is_fixable=False,
issue_domain=DOMAIN,
severity=IssueSeverity.WARNING,
translation_key="deprecated_yaml",
translation_placeholders={
"domain": DOMAIN,
"integration_title": "LiteJet",
},
)
return result
@staticmethod
@callback

View file

@ -25,5 +25,11 @@
}
}
}
},
"issues": {
"deprecated_yaml_serial_exception": {
"title": "The LiteJet YAML configuration import failed",
"description": "Configuring LiteJet using YAML is being removed but there was an error opening the serial port when importing your YAML configuration.\n\nCorrect the YAML configuration and restart Home Assistant to try again or manually continue to [set up the integration]({url})."
}
}
}

View file

@ -6,7 +6,8 @@ from serial import SerialException
from homeassistant import config_entries, data_entry_flow
from homeassistant.components.litejet.const import CONF_DEFAULT_TRANSITION, DOMAIN
from homeassistant.const import CONF_PORT
from homeassistant.core import HomeAssistant
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
import homeassistant.helpers.issue_registry as ir
from tests.common import MockConfigEntry
@ -67,7 +68,7 @@ async def test_flow_open_failed(hass: HomeAssistant) -> None:
assert result["errors"][CONF_PORT] == "open_failed"
async def test_import_step(hass: HomeAssistant) -> None:
async def test_import_step(hass: HomeAssistant, mock_litejet) -> None:
"""Test initializing via import step."""
test_data = {CONF_PORT: "/dev/imported"}
result = await hass.config_entries.flow.async_init(
@ -78,6 +79,51 @@ async def test_import_step(hass: HomeAssistant) -> None:
assert result["title"] == test_data[CONF_PORT]
assert result["data"] == test_data
issue_registry = ir.async_get(hass)
issue = issue_registry.async_get_issue(
HOMEASSISTANT_DOMAIN, "deprecated_yaml_litejet"
)
assert issue.translation_key == "deprecated_yaml"
async def test_import_step_fails(hass: HomeAssistant) -> None:
"""Test initializing via import step fails due to can't open port."""
test_data = {CONF_PORT: "/dev/test"}
with patch("pylitejet.LiteJet") as mock_pylitejet:
mock_pylitejet.side_effect = SerialException
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=test_data
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] == {"port": "open_failed"}
issue_registry = ir.async_get(hass)
assert issue_registry.async_get_issue(DOMAIN, "deprecated_yaml_serial_exception")
async def test_import_step_already_exist(hass: HomeAssistant) -> None:
"""Test initializing via import step when entry already exist."""
first_entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_PORT: "/dev/imported"},
)
first_entry.add_to_hass(hass)
test_data = {CONF_PORT: "/dev/imported"}
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=test_data
)
assert result["type"] == data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "single_instance_allowed"
issue_registry = ir.async_get(hass)
issue = issue_registry.async_get_issue(
HOMEASSISTANT_DOMAIN, "deprecated_yaml_litejet"
)
assert issue.translation_key == "deprecated_yaml"
async def test_options(hass: HomeAssistant) -> None:
"""Test updating options."""