Remove YAML import from Workday (#101223)

This commit is contained in:
G Johansson 2023-10-02 09:01:03 +02:00 committed by GitHub
parent b56d25121f
commit f2cf87b0b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 3 additions and 301 deletions

View file

@ -2,28 +2,19 @@
from __future__ import annotations
from datetime import date, timedelta
from typing import Any
from holidays import (
HolidayBase,
__version__ as python_holidays_version,
country_holidays,
list_supported_countries,
)
import voluptuous as vol
from homeassistant.components.binary_sensor import (
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
BinarySensorEntity,
)
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import dt as dt_util
from .const import (
@ -35,10 +26,6 @@ from .const import (
CONF_PROVINCE,
CONF_REMOVE_HOLIDAYS,
CONF_WORKDAYS,
DEFAULT_EXCLUDES,
DEFAULT_NAME,
DEFAULT_OFFSET,
DEFAULT_WORKDAYS,
DOMAIN,
LOGGER,
)
@ -64,76 +51,6 @@ def validate_dates(holiday_list: list[str]) -> list[str]:
return calc_holidays
def valid_country(value: Any) -> str:
"""Validate that the given country is supported."""
value = cv.string(value)
try:
raw_value = value.encode("utf-8")
except UnicodeError as err:
raise vol.Invalid(
"The country name or the abbreviation must be a valid UTF-8 string."
) from err
if not raw_value:
raise vol.Invalid("Country name or the abbreviation must not be empty.")
if value not in list_supported_countries():
raise vol.Invalid("Country is not supported.")
return value
PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_COUNTRY): valid_country,
vol.Optional(CONF_EXCLUDES, default=DEFAULT_EXCLUDES): vol.All(
cv.ensure_list, [vol.In(ALLOWED_DAYS)]
),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_OFFSET, default=DEFAULT_OFFSET): vol.Coerce(int),
vol.Optional(CONF_PROVINCE): cv.string,
vol.Optional(CONF_WORKDAYS, default=DEFAULT_WORKDAYS): vol.All(
cv.ensure_list, [vol.In(ALLOWED_DAYS)]
),
vol.Optional(CONF_ADD_HOLIDAYS, default=[]): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(CONF_REMOVE_HOLIDAYS, default=[]): vol.All(
cv.ensure_list, [cv.string]
),
}
)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Workday sensor."""
async_create_issue(
hass,
HOMEASSISTANT_DOMAIN,
f"deprecated_yaml_{DOMAIN}",
breaks_in_ha_version="2023.11.0",
is_fixable=False,
issue_domain=DOMAIN,
severity=IssueSeverity.WARNING,
translation_key="deprecated_yaml",
translation_placeholders={
"domain": DOMAIN,
"integration_title": "Workday",
},
)
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=config,
)
)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:

View file

@ -179,33 +179,6 @@ class WorkdayConfigFlow(ConfigFlow, domain=DOMAIN):
"""Get the options flow for this handler."""
return WorkdayOptionsFlowHandler(config_entry)
async def async_step_import(self, config: dict[str, Any]) -> FlowResult:
"""Import a configuration from config.yaml."""
abort_match = {
CONF_COUNTRY: config[CONF_COUNTRY],
CONF_EXCLUDES: config[CONF_EXCLUDES],
CONF_OFFSET: config[CONF_OFFSET],
CONF_WORKDAYS: config[CONF_WORKDAYS],
CONF_ADD_HOLIDAYS: config[CONF_ADD_HOLIDAYS],
CONF_REMOVE_HOLIDAYS: config[CONF_REMOVE_HOLIDAYS],
CONF_PROVINCE: config.get(CONF_PROVINCE),
}
new_config = config.copy()
new_config[CONF_PROVINCE] = config.get(CONF_PROVINCE)
LOGGER.debug("Importing with %s", new_config)
self._async_abort_entries_match(abort_match)
self.data[CONF_NAME] = config.get(CONF_NAME, DEFAULT_NAME)
self.data[CONF_COUNTRY] = config[CONF_COUNTRY]
LOGGER.debug(
"No duplicate, next step with name %s for country %s",
self.data[CONF_NAME],
self.data[CONF_COUNTRY],
)
return await self.async_step_options(user_input=new_config)
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
@ -246,8 +219,6 @@ class WorkdayConfigFlow(ConfigFlow, domain=DOMAIN):
errors["remove_holidays"] = "remove_holiday_error"
except RemoveDateRangeError:
errors["remove_holidays"] = "remove_holiday_range_error"
except NotImplementedError:
self.async_abort(reason="incorrect_province")
abort_match = {
CONF_COUNTRY: combined_input[CONF_COUNTRY],

View file

@ -2,7 +2,6 @@
"title": "Workday",
"config": {
"abort": {
"incorrect_province": "Incorrect subdivision from yaml import",
"already_configured": "Workday has already been setup with chosen configuration"
},
"step": {

View file

@ -4,9 +4,7 @@ from typing import Any
from freezegun.api import FrozenDateTimeFactory
import pytest
import voluptuous as vol
from homeassistant.components.workday import binary_sensor
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import UTC
@ -38,21 +36,6 @@ from . import (
)
async def test_valid_country_yaml() -> None:
"""Test valid country from yaml."""
# Invalid UTF-8, must not contain U+D800 to U+DFFF
with pytest.raises(vol.Invalid):
binary_sensor.valid_country("\ud800")
with pytest.raises(vol.Invalid):
binary_sensor.valid_country("\udfff")
# Country MUST NOT be empty
with pytest.raises(vol.Invalid):
binary_sensor.valid_country("")
# Country must be supported by holidays
with pytest.raises(vol.Invalid):
binary_sensor.valid_country("HomeAssistantLand")
@pytest.mark.parametrize(
("config", "expected_state"),
[
@ -89,35 +72,6 @@ async def test_setup(
}
async def test_setup_from_import(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test setup from various configs."""
freezer.move_to(datetime(2022, 4, 15, 12, tzinfo=UTC)) # Monday
await async_setup_component(
hass,
"binary_sensor",
{
"binary_sensor": {
"platform": "workday",
"country": "DE",
}
},
)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.workday_sensor")
assert state is not None
assert state.state == "off"
assert state.attributes == {
"friendly_name": "Workday Sensor",
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
}
async def test_setup_with_invalid_province_from_yaml(hass: HomeAssistant) -> None:
"""Test setup invalid province with import."""
await async_setup_component(

View file

@ -13,7 +13,6 @@ from homeassistant.components.workday.const import (
CONF_REMOVE_HOLIDAYS,
CONF_WORKDAYS,
DEFAULT_EXCLUDES,
DEFAULT_NAME,
DEFAULT_OFFSET,
DEFAULT_WORKDAYS,
DOMAIN,
@ -24,8 +23,6 @@ from homeassistant.data_entry_flow import FlowResultType
from . import init_integration
from tests.common import MockConfigEntry
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
@ -156,142 +153,6 @@ async def test_form_no_subdivision(hass: HomeAssistant) -> None:
}
async def test_import_flow_success(hass: HomeAssistant) -> None:
"""Test a successful import of yaml."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={
CONF_NAME: DEFAULT_NAME,
CONF_COUNTRY: "DE",
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 result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "Workday Sensor"
assert result["options"] == {
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"province": None,
}
result2 = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={
CONF_NAME: "Workday Sensor 2",
CONF_COUNTRY: "DE",
CONF_PROVINCE: "BW",
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 result2["type"] == FlowResultType.CREATE_ENTRY
assert result2["title"] == "Workday Sensor 2"
assert result2["options"] == {
"name": "Workday Sensor 2",
"country": "DE",
"province": "BW",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
}
async def test_import_flow_already_exist(hass: HomeAssistant) -> None:
"""Test import of yaml already exist."""
entry = MockConfigEntry(
domain=DOMAIN,
data={},
options={
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
"province": None,
},
)
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={
CONF_NAME: "Workday sensor 2",
CONF_COUNTRY: "DE",
CONF_EXCLUDES: ["sat", "sun", "holiday"],
CONF_OFFSET: 0,
CONF_WORKDAYS: ["mon", "tue", "wed", "thu", "fri"],
CONF_ADD_HOLIDAYS: [],
CONF_REMOVE_HOLIDAYS: [],
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "already_configured"
async def test_import_flow_province_no_conflict(hass: HomeAssistant) -> None:
"""Test import of yaml with province."""
entry = MockConfigEntry(
domain=DOMAIN,
data={},
options={
"name": "Workday Sensor",
"country": "DE",
"excludes": ["sat", "sun", "holiday"],
"days_offset": 0,
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"add_holidays": [],
"remove_holidays": [],
},
)
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={
CONF_NAME: "Workday sensor 2",
CONF_COUNTRY: "DE",
CONF_PROVINCE: "BW",
CONF_EXCLUDES: ["sat", "sun", "holiday"],
CONF_OFFSET: 0,
CONF_WORKDAYS: ["mon", "tue", "wed", "thu", "fri"],
CONF_ADD_HOLIDAYS: [],
CONF_REMOVE_HOLIDAYS: [],
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY
async def test_options_form(hass: HomeAssistant) -> None:
"""Test we get the form in options."""