Add subdiv aliases to workday (#133608)

* Add subdiv aliases to workday

* Fix

* Add lib test
This commit is contained in:
G Johansson 2025-04-07 18:24:07 +02:00 committed by GitHub
parent cd2313d2ca
commit f2e4bcea19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 70 additions and 3 deletions

View File

@ -26,6 +26,7 @@ from homeassistant.helpers.selector import (
NumberSelector,
NumberSelectorConfig,
NumberSelectorMode,
SelectOptionDict,
SelectSelector,
SelectSelectorConfig,
SelectSelectorMode,
@ -79,10 +80,19 @@ def add_province_and_language_to_schema(
}
if provinces := all_countries.get(country):
if _country.subdivisions_aliases and (
subdiv_aliases := _country.get_subdivision_aliases()
):
province_options: list[Any] = [
SelectOptionDict(value=k, label=", ".join(v))
for k, v in subdiv_aliases.items()
]
else:
province_options = provinces
province_schema = {
vol.Optional(CONF_PROVINCE): SelectSelector(
SelectSelectorConfig(
options=provinces,
options=province_options,
mode=SelectSelectorMode.DROPDOWN,
translation_key=CONF_PROVINCE,
)

View File

@ -55,7 +55,7 @@ async def test_form(hass: HomeAssistant) -> None:
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: [],
CONF_REMOVE_HOLIDAYS: [],
CONF_LANGUAGE: "de",
CONF_LANGUAGE: "en_US",
},
)
await hass.async_block_till_done()
@ -70,7 +70,48 @@ async def test_form(hass: HomeAssistant) -> None:
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"language": "de",
"language": "en_US",
}
async def test_form_province_no_alias(hass: HomeAssistant) -> None:
"""Test we get the forms."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_NAME: "Workday Sensor",
CONF_COUNTRY: "US",
},
)
await hass.async_block_till_done()
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{
CONF_EXCLUDES: DEFAULT_EXCLUDES,
CONF_OFFSET: DEFAULT_OFFSET,
CONF_WORKDAYS: DEFAULT_WORKDAYS,
CONF_ADD_HOLIDAYS: [],
CONF_REMOVE_HOLIDAYS: [],
},
)
await hass.async_block_till_done()
assert result3["type"] is FlowResultType.CREATE_ENTRY
assert result3["title"] == "Workday Sensor"
assert result3["options"] == {
"name": "Workday Sensor",
"country": "US",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
}

View File

@ -5,6 +5,7 @@ from __future__ import annotations
from datetime import datetime
from freezegun.api import FrozenDateTimeFactory
from holidays.utils import country_holidays
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
@ -50,3 +51,18 @@ async def test_update_options(
assert entry_check.state is ConfigEntryState.LOADED
state = hass.states.get("binary_sensor.workday_sensor")
assert state.state == "off"
async def test_workday_subdiv_aliases() -> None:
"""Test subdiv aliases in holidays library."""
country = country_holidays(
country="FR",
years=2025,
)
subdiv_aliases = country.get_subdivision_aliases()
assert subdiv_aliases["GES"] == [ # codespell:ignore
"Alsace",
"Champagne-Ardenne",
"Lorraine",
]