Add config flow to Moon (#67444)

This commit is contained in:
Franck Nijhof
2022-03-04 00:12:33 +01:00
committed by GitHub
parent 81a509e69e
commit 02391663c1
15 changed files with 304 additions and 42 deletions

View File

@@ -0,0 +1,27 @@
"""Fixtures for Moon integration tests."""
from __future__ import annotations
from collections.abc import Generator
from unittest.mock import patch
import pytest
from homeassistant.components.moon.const import DOMAIN
from tests.common import MockConfigEntry
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Return the default mocked config entry."""
return MockConfigEntry(
title="Moon",
domain=DOMAIN,
)
@pytest.fixture
def mock_setup_entry() -> Generator[None, None, None]:
"""Mock setting up a config entry."""
with patch("homeassistant.components.moon.async_setup_entry", return_value=True):
yield

View File

@@ -0,0 +1,72 @@
"""Tests for the Moon config flow."""
from unittest.mock import MagicMock
import pytest
from homeassistant.components.moon.const import DOMAIN
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import (
RESULT_TYPE_ABORT,
RESULT_TYPE_CREATE_ENTRY,
RESULT_TYPE_FORM,
)
from tests.common import MockConfigEntry
async def test_full_user_flow(
hass: HomeAssistant,
mock_setup_entry: MagicMock,
) -> None:
"""Test the full user configuration flow."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result.get("type") == RESULT_TYPE_FORM
assert result.get("step_id") == SOURCE_USER
assert "flow_id" in result
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={},
)
assert result2.get("type") == RESULT_TYPE_CREATE_ENTRY
assert result2.get("title") == "Moon"
assert result2.get("data") == {}
@pytest.mark.parametrize("source", [SOURCE_USER, SOURCE_IMPORT])
async def test_single_instance_allowed(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
source: str,
) -> None:
"""Test we abort if already setup."""
mock_config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": source}
)
assert result.get("type") == RESULT_TYPE_ABORT
assert result.get("reason") == "single_instance_allowed"
async def test_import_flow(
hass: HomeAssistant,
mock_setup_entry: MagicMock,
) -> None:
"""Test the import configuration flow."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={CONF_NAME: "My Moon"},
)
assert result.get("type") == RESULT_TYPE_CREATE_ENTRY
assert result.get("title") == "My Moon"
assert result.get("data") == {}

View File

@@ -0,0 +1,55 @@
"""Tests for the Moon integration."""
from unittest.mock import AsyncMock
from homeassistant.components.moon.const import DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
async def test_load_unload_config_entry(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the Moon configuration entry loading/unloading."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED
await hass.config_entries.async_unload(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert not hass.data.get(DOMAIN)
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
async def test_import_config(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
) -> None:
"""Test Moon being set up from config via import."""
assert await async_setup_component(
hass,
SENSOR_DOMAIN,
{
SENSOR_DOMAIN: {
"platform": DOMAIN,
CONF_NAME: "My Moon",
}
},
)
await hass.async_block_till_done()
config_entries = hass.config_entries.async_entries(DOMAIN)
assert len(config_entries) == 1
entry = config_entries[0]
assert entry.title == "My Moon"
assert entry.unique_id is None
assert entry.data == {}

View File

@@ -5,10 +5,6 @@ from unittest.mock import patch
import pytest
from homeassistant.components.homeassistant import (
DOMAIN as HA_DOMAIN,
SERVICE_UPDATE_ENTITY,
)
from homeassistant.components.moon.sensor import (
MOON_ICONS,
STATE_FIRST_QUARTER,
@@ -20,9 +16,11 @@ from homeassistant.components.moon.sensor import (
STATE_WAXING_CRESCENT,
STATE_WAXING_GIBBOUS,
)
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.const import ATTR_ICON
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry
@pytest.mark.parametrize(
@@ -39,33 +37,27 @@ from homeassistant.setup import async_setup_component
],
)
async def test_moon_day(
hass: HomeAssistant, moon_value: float, native_value: str, icon: str
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
moon_value: float,
native_value: str,
icon: str,
) -> None:
"""Test the Moon sensor."""
config = {"sensor": {"platform": "moon"}}
await async_setup_component(hass, HA_DOMAIN, {})
assert await async_setup_component(hass, "sensor", config)
await hass.async_block_till_done()
assert hass.states.get("sensor.moon")
mock_config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.moon.sensor.moon.phase", return_value=moon_value
):
await async_update_entity(hass, "sensor.moon")
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("sensor.moon")
assert state
assert state.state == native_value
assert state.attributes["icon"] == icon
assert state.attributes[ATTR_ICON] == icon
async def async_update_entity(hass: HomeAssistant, entity_id: str) -> None:
"""Run an update action for an entity."""
await hass.services.async_call(
HA_DOMAIN,
SERVICE_UPDATE_ENTITY,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
await hass.async_block_till_done()
entity_registry = er.async_get(hass)
entry = entity_registry.async_get("sensor.moon")
assert entry
assert entry.unique_id == mock_config_entry.entry_id