core/tests/components/holiday/test_config_flow.py

221 lines
6.2 KiB
Python
Raw Normal View History

Add Holiday integration (#103795) * Add Holiday integration * Localize holiday names * Changes based on review feedback * Add tests * Add device info * Bump holidays to 0.36 * Default to Home Assistant country setting * Update homeassistant/components/holiday/calendar.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Update homeassistant/components/holiday/calendar.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Update homeassistant/components/holiday/config_flow.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * black * Move time * Stop creating duplicate holiday calendars * Set default language using python-holiday * Use common translation * Set _attr_name to None to fix friendly name * Fix location * Update homeassistant/components/holiday/__init__.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Update homeassistant/components/holiday/calendar.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Update homeassistant/components/holiday/calendar.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Update tests/components/holiday/test_init.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * cleanup * Set up the integration and test the state * Test that configuring more than one instance is rejected * Set default_language to user's language, fallback to country's default language * Improve tests * Update homeassistant/components/holiday/calendar.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Cleanup * Add next year so we don't run out * Update tests/components/holiday/test_init.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Cleanup * Set default language in `__init__` * Add strict typing * Change default language: HA's language `en` is `en_US` in holidays, apart from Canada * CONF_PROVINCE can be None * Fix test * Fix default_language * Refactor tests * Province can be None * Add test for translated title * Address feedback * Address feedback * Change test to use service call * Address feedback * Apply suggestions from code review Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Changes based on review feedback * Update homeassistant/components/holiday/calendar.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Update homeassistant/components/holiday/calendar.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Add a test if next event is missing * Rebase * Set device to service * Remove not needed translation key --------- Co-authored-by: G Johansson <goran.johansson@shiftit.se>
2023-12-03 15:28:53 +00:00
"""Test the Holiday config flow."""
from unittest.mock import AsyncMock
import pytest
from homeassistant import config_entries
from homeassistant.components.holiday.const import CONF_PROVINCE, DOMAIN
from homeassistant.const import CONF_COUNTRY
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.FORM
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_COUNTRY: "DE",
},
)
await hass.async_block_till_done()
assert result2["type"] == FlowResultType.FORM
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{
CONF_PROVINCE: "BW",
},
)
await hass.async_block_till_done()
assert result3["type"] == FlowResultType.CREATE_ENTRY
assert result3["title"] == "Germany, BW"
assert result3["data"] == {
"country": "DE",
"province": "BW",
}
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_no_subdivision(hass: HomeAssistant) -> None:
"""Test we get the forms correctly without subdivision."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.FORM
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_COUNTRY: "SE",
},
)
await hass.async_block_till_done()
assert result2["type"] == FlowResultType.CREATE_ENTRY
assert result2["title"] == "Sweden"
assert result2["data"] == {
"country": "SE",
}
async def test_form_translated_title(hass: HomeAssistant) -> None:
"""Test the title gets translated."""
hass.config.language = "de"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_COUNTRY: "SE",
},
)
await hass.async_block_till_done()
assert result2["title"] == "Schweden"
async def test_single_combination_country_province(hass: HomeAssistant) -> None:
"""Test that configuring more than one instance is rejected."""
data_de = {
CONF_COUNTRY: "DE",
CONF_PROVINCE: "BW",
}
data_se = {
CONF_COUNTRY: "SE",
}
MockConfigEntry(domain=DOMAIN, data=data_de).add_to_hass(hass)
MockConfigEntry(domain=DOMAIN, data=data_se).add_to_hass(hass)
# Test for country without subdivisions
result_se = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
data=data_se,
)
assert result_se["type"] == FlowResultType.ABORT
assert result_se["reason"] == "already_configured"
# Test for country with subdivisions
result_de_step1 = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
data=data_de,
)
assert result_de_step1["type"] == FlowResultType.FORM
result_de_step2 = await hass.config_entries.flow.async_configure(
result_de_step1["flow_id"],
{
CONF_PROVINCE: data_de[CONF_PROVINCE],
},
)
assert result_de_step2["type"] == FlowResultType.ABORT
assert result_de_step2["reason"] == "already_configured"
async def test_form_babel_unresolved_language(hass: HomeAssistant) -> None:
"""Test the config flow if using not babel supported language."""
hass.config.language = "en-XX"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_COUNTRY: "SE",
},
)
await hass.async_block_till_done()
assert result["title"] == "Sweden"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_COUNTRY: "DE",
},
)
await hass.async_block_till_done()
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_PROVINCE: "BW",
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "Germany, BW"
assert result["data"] == {
"country": "DE",
"province": "BW",
}
async def test_form_babel_replace_dash_with_underscore(hass: HomeAssistant) -> None:
"""Test the config flow if using language with dash."""
hass.config.language = "en-GB"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_COUNTRY: "SE",
},
)
await hass.async_block_till_done()
assert result["title"] == "Sweden"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_COUNTRY: "DE",
},
)
await hass.async_block_till_done()
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_PROVINCE: "BW",
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "Germany, BW"
assert result["data"] == {
"country": "DE",
"province": "BW",
}