Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix repairs for remove dates in Workday #109626

Merged
merged 1 commit into from Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 42 additions & 20 deletions homeassistant/components/workday/binary_sensor.py
@@ -1,4 +1,5 @@
"""Sensor to indicate whether the current day is a workday."""

from __future__ import annotations

from datetime import date, timedelta
Expand Down Expand Up @@ -53,7 +54,7 @@ def validate_dates(holiday_list: list[str]) -> list[str]:
continue
_range: timedelta = d2 - d1
for i in range(_range.days + 1):
day = d1 + timedelta(days=i)
day: date = d1 + timedelta(days=i)
calc_holidays.append(day.strftime("%Y-%m-%d"))
continue
calc_holidays.append(add_date)
Expand Down Expand Up @@ -123,25 +124,46 @@ async def async_setup_entry(
LOGGER.debug("Removed %s by name '%s'", holiday, remove_holiday)
except KeyError as unmatched:
LOGGER.warning("No holiday found matching %s", unmatched)
async_create_issue(
hass,
DOMAIN,
f"bad_named_holiday-{entry.entry_id}-{slugify(remove_holiday)}",
is_fixable=True,
is_persistent=False,
severity=IssueSeverity.WARNING,
translation_key="bad_named_holiday",
translation_placeholders={
CONF_COUNTRY: country if country else "-",
"title": entry.title,
CONF_REMOVE_HOLIDAYS: remove_holiday,
},
data={
"entry_id": entry.entry_id,
"country": country,
"named_holiday": remove_holiday,
},
)
if dt_util.parse_date(remove_holiday):
async_create_issue(
hass,
DOMAIN,
f"bad_date_holiday-{entry.entry_id}-{slugify(remove_holiday)}",
is_fixable=True,
is_persistent=False,
severity=IssueSeverity.WARNING,
translation_key="bad_date_holiday",
translation_placeholders={
CONF_COUNTRY: country if country else "-",
"title": entry.title,
CONF_REMOVE_HOLIDAYS: remove_holiday,
},
data={
"entry_id": entry.entry_id,
"country": country,
"named_holiday": remove_holiday,
},
)
else:
async_create_issue(
hass,
DOMAIN,
f"bad_named_holiday-{entry.entry_id}-{slugify(remove_holiday)}",
is_fixable=True,
is_persistent=False,
severity=IssueSeverity.WARNING,
translation_key="bad_named_holiday",
translation_placeholders={
CONF_COUNTRY: country if country else "-",
"title": entry.title,
CONF_REMOVE_HOLIDAYS: remove_holiday,
},
data={
"entry_id": entry.entry_id,
"country": country,
"named_holiday": remove_holiday,
},
)

LOGGER.debug("Found the following holidays for your configuration:")
for holiday_date, name in sorted(obj_holidays.items()):
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/workday/repairs.py
Expand Up @@ -125,9 +125,9 @@ async def async_step_init(
self, user_input: dict[str, str] | None = None
) -> data_entry_flow.FlowResult:
"""Handle the first step of a fix flow."""
return await self.async_step_named_holiday()
return await self.async_step_fix_remove_holiday()

async def async_step_named_holiday(
async def async_step_fix_remove_holiday(
self, user_input: dict[str, Any] | None = None
) -> data_entry_flow.FlowResult:
"""Handle the options step of a fix flow."""
Expand Down Expand Up @@ -168,7 +168,7 @@ async def async_step_named_holiday(
{CONF_REMOVE_HOLIDAYS: removed_named_holiday},
)
return self.async_show_form(
step_id="named_holiday",
step_id="fix_remove_holiday",
data_schema=new_schema,
description_placeholders={
CONF_COUNTRY: self.country if self.country else "-",
Expand Down
22 changes: 21 additions & 1 deletion homeassistant/components/workday/strings.json
Expand Up @@ -137,7 +137,7 @@
"title": "Configured named holiday {remove_holidays} for {title} does not exist",
"fix_flow": {
"step": {
"named_holiday": {
"fix_remove_holiday": {
"title": "[%key:component::workday::issues::bad_named_holiday::title%]",
"description": "Remove named holiday `{remove_holidays}` as it can't be found in country {country}.",
"data": {
Expand All @@ -152,6 +152,26 @@
"remove_holiday_error": "[%key:component::workday::config::error::remove_holiday_error%]"
}
}
},
"bad_date_holiday": {
"title": "Configured holiday date {remove_holidays} for {title} does not exist",
"fix_flow": {
"step": {
"fix_remove_holiday": {
"title": "[%key:component::workday::issues::bad_date_holiday::title%]",
"description": "Remove holiday date `{remove_holidays}` as it can't be found in country {country}.",
"data": {
"remove_holidays": "[%key:component::workday::config::step::options::data::remove_holidays%]"
},
"data_description": {
"remove_holidays": "[%key:component::workday::config::step::options::data_description::remove_holidays%]"
}
}
},
"error": {
"remove_holiday_error": "[%key:component::workday::config::error::remove_holiday_error%]"
}
}
}
},
"entity": {
Expand Down
11 changes: 11 additions & 0 deletions tests/components/workday/__init__.py
@@ -1,4 +1,5 @@
"""Tests the Home Assistant workday binary sensor."""

from __future__ import annotations

from typing import Any
Expand Down Expand Up @@ -181,6 +182,16 @@ async def init_integration(
"remove_holidays": ["Not a Holiday", "Christmas", "Thanksgiving"],
"language": "en_US",
}
TEST_CONFIG_REMOVE_DATE = {
"name": DEFAULT_NAME,
"country": "US",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": ["2024-02-05", "2024-02-06"],
"language": "en_US",
}
TEST_CONFIG_TOMORROW = {
"name": DEFAULT_NAME,
"country": "DE",
Expand Down
89 changes: 86 additions & 3 deletions tests/components/workday/test_repairs.py
@@ -1,4 +1,5 @@
"""Test repairs for unifiprotect."""

from __future__ import annotations

from http import HTTPStatus
Expand All @@ -10,12 +11,13 @@
from homeassistant.components.workday.const import CONF_REMOVE_HOLIDAYS, DOMAIN
from homeassistant.const import CONF_COUNTRY
from homeassistant.core import HomeAssistant
from homeassistant.helpers.issue_registry import async_create_issue
from homeassistant.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component

from . import (
TEST_CONFIG_INCORRECT_COUNTRY,
TEST_CONFIG_INCORRECT_PROVINCE,
TEST_CONFIG_REMOVE_DATE,
TEST_CONFIG_REMOVE_NAMED,
init_integration,
)
Expand Down Expand Up @@ -329,6 +331,7 @@ async def test_bad_named_holiday(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
issue_registry: ir.IssueRegistry,
) -> None:
"""Test fixing bad province selecting none."""
assert await async_setup_component(hass, "repairs", {})
Expand All @@ -337,6 +340,11 @@ async def test_bad_named_holiday(
state = hass.states.get("binary_sensor.workday_sensor")
assert state

issues = issue_registry.issues.keys()
for issue in issues:
if issue[0] == DOMAIN:
assert issue[1].startswith("bad_named")

ws_client = await hass_ws_client(hass)
client = await hass_client()

Expand Down Expand Up @@ -365,7 +373,7 @@ async def test_bad_named_holiday(
CONF_REMOVE_HOLIDAYS: "Not a Holiday",
"title": entry.title,
}
assert data["step_id"] == "named_holiday"
assert data["step_id"] == "fix_remove_holiday"

url = RepairsFlowResourceView.url.format(flow_id=flow_id)
resp = await client.post(
Expand Down Expand Up @@ -402,6 +410,81 @@ async def test_bad_named_holiday(
assert not issue


async def test_bad_date_holiday(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
hass_ws_client: WebSocketGenerator,
issue_registry: ir.IssueRegistry,
) -> None:
"""Test fixing bad province selecting none."""
assert await async_setup_component(hass, "repairs", {})
entry = await init_integration(hass, TEST_CONFIG_REMOVE_DATE)

state = hass.states.get("binary_sensor.workday_sensor")
assert state

issues = issue_registry.issues.keys()
for issue in issues:
if issue[0] == DOMAIN:
assert issue[1].startswith("bad_date")

ws_client = await hass_ws_client(hass)
client = await hass_client()

await ws_client.send_json({"id": 1, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()

assert msg["success"]
assert len(msg["result"]["issues"]) > 0
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_date_holiday-1-2024_02_05":
issue = i
assert issue is not None

url = RepairsFlowIndexView.url
resp = await client.post(
url,
json={"handler": DOMAIN, "issue_id": "bad_date_holiday-1-2024_02_05"},
)
assert resp.status == HTTPStatus.OK
data = await resp.json()

flow_id = data["flow_id"]
assert data["description_placeholders"] == {
CONF_COUNTRY: "US",
CONF_REMOVE_HOLIDAYS: "2024-02-05",
"title": entry.title,
}
assert data["step_id"] == "fix_remove_holiday"

url = RepairsFlowResourceView.url.format(flow_id=flow_id)
resp = await client.post(url, json={"remove_holidays": ["2024-02-06"]})
assert resp.status == HTTPStatus.OK
data = await resp.json()

assert data["type"] == "create_entry"
await hass.async_block_till_done()

state = hass.states.get("binary_sensor.workday_sensor")
assert state

await ws_client.send_json({"id": 2, "type": "repairs/list_issues"})
msg = await ws_client.receive_json()

assert msg["success"]
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_date_holiday-1-2024_02_05":
issue = i
assert not issue
issue = None
for i in msg["result"]["issues"]:
if i["issue_id"] == "bad_date_holiday-1-2024_02_06":
issue = i
assert issue


async def test_other_fixable_issues(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
Expand All @@ -428,7 +511,7 @@ async def test_other_fixable_issues(
"severity": "error",
"translation_key": "issue_1",
}
async_create_issue(
ir.async_create_issue(
hass,
issue["domain"],
issue["issue_id"],
Expand Down