Make the kitchen_sink integration set up a config entry (#85680)

* Make the kitchen_sink integration set up a config entry

* Update homeassistant/components/kitchen_sink/config_flow.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Add singleton check in import step + add test

* Fix tests

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
pull/86072/head
Erik Montnemery 2023-01-16 11:03:44 +01:00 committed by GitHub
parent 3179101fbc
commit f9662e0af0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 18 deletions

View File

@ -15,9 +15,9 @@ from homeassistant.components.recorder.statistics import (
async_import_statistics,
get_last_statistics,
)
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import Platform, UnitOfEnergy, UnitOfTemperature, UnitOfVolume
from homeassistant.core import HomeAssistant
from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType
import homeassistant.util.dt as dt_util
@ -32,9 +32,20 @@ COMPONENTS_WITH_DEMO_PLATFORM = [
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the demo environment."""
# Set up demo platforms
for platform in COMPONENTS_WITH_DEMO_PLATFORM:
hass.async_create_task(async_load_platform(hass, platform, DOMAIN, {}, config))
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data={}
)
)
return True
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Set the config entry up."""
# Set up demo platforms with config entry
await hass.config_entries.async_forward_entry_setups(
config_entry, COMPONENTS_WITH_DEMO_PLATFORM
)
# Create issues
_create_issues(hass)

View File

@ -0,0 +1,22 @@
"""Config flow to configure the Kitchen Sink component."""
from __future__ import annotations
from typing import Any
from homeassistant import config_entries
from homeassistant.data_entry_flow import FlowResult
from . import DOMAIN
class KitchenSinkConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Kitchen Sink configuration flow."""
VERSION = 1
async def async_step_import(self, import_info: dict[str, Any]) -> FlowResult:
"""Set the config entry up from yaml."""
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
return self.async_create_entry(title="Kitchen Sink", data=import_info)

View File

@ -11,18 +11,17 @@ from homeassistant.const import ATTR_BATTERY_LEVEL, UnitOfPower
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
from homeassistant.helpers.typing import StateType
from . import DOMAIN
async def async_setup_platform(
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigType,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Demo sensors."""
"""Set up the Everything but the Kitchen Sink config entry."""
async_add_entities(
[
DemoSensor(
@ -56,15 +55,6 @@ async def async_setup_platform(
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Everything but the Kitchen Sink config entry."""
await async_setup_platform(hass, {}, async_add_entities)
class DemoSensor(SensorEntity):
"""Representation of a Demo sensor."""

View File

@ -0,0 +1,46 @@
"""Test the Everything but the Kitchen Sink config flow."""
from unittest.mock import patch
from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.components.kitchen_sink import DOMAIN
async def test_import(hass):
"""Test that we can import a config entry."""
with patch("homeassistant.components.kitchen_sink.async_setup_entry"):
assert await setup.async_setup_component(hass, DOMAIN, {DOMAIN: {}})
await hass.async_block_till_done()
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
entry = hass.config_entries.async_entries(DOMAIN)[0]
assert entry.data == {}
async def test_import_once(hass):
"""Test that we don't create multiple config entries."""
with patch(
"homeassistant.components.kitchen_sink.async_setup_entry"
) as mock_setup_entry:
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={},
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["title"] == "Kitchen Sink"
assert result["data"] == {}
assert result["options"] == {}
mock_setup_entry.assert_called_once()
# Test importing again doesn't create a 2nd entry
with patch(
"homeassistant.components.kitchen_sink.async_setup_entry"
) as mock_setup_entry:
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={},
)
assert result["type"] == data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "single_instance_allowed"
mock_setup_entry.assert_not_called()