Move Workday failures to __init__ (#98651)

Workday failures in init
This commit is contained in:
G Johansson 2023-08-20 22:30:43 +02:00 committed by GitHub
parent 53b596101b
commit a29e4a5f02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 9 deletions

View File

@ -1,15 +1,28 @@
"""Sensor to indicate whether the current day is a workday."""
from __future__ import annotations
from holidays import list_supported_countries
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryError
from .const import PLATFORMS
from .const import CONF_COUNTRY, CONF_PROVINCE, LOGGER, PLATFORMS
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Workday from a config entry."""
country: str = entry.options[CONF_COUNTRY]
province: str | None = entry.options.get(CONF_PROVINCE)
if country and country not in list_supported_countries():
LOGGER.error("There is no country %s", country)
raise ConfigEntryError("Selected country is not valid")
if province and province not in list_supported_countries()[country]:
LOGGER.error("There is no subdivision %s in country %s", province, country)
raise ConfigEntryError("Selected province is not valid")
entry.async_on_unload(entry.add_update_listener(async_update_listener))
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

View File

@ -129,14 +129,6 @@ async def async_setup_entry(
workdays: list[str] = entry.options[CONF_WORKDAYS]
year: int = (dt_util.now() + timedelta(days=days_offset)).year
if country and country not in list_supported_countries():
LOGGER.error("There is no country %s", country)
return
if province and province not in list_supported_countries()[country]:
LOGGER.error("There is no subdivision %s in country %s", province, country)
return
obj_holidays: HolidayBase = country_holidays(country, subdiv=province, years=year)
# Add custom holidays