Skip to content

Commit 3f3604b

Browse files
committed
migrate condition
1 parent d4c0fc4 commit 3f3604b

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Fixes and improvements contributed by [@lollox80](https://github.com/lollox80)
88
- URLs in action groups allow `homeassistant://` deep links
99
- Suppress repeating `condition_variables` from archived envelopes
1010
- Expand templated values in `data` for archiving
11+
- Migrate old `condition` to `conditions`
1112
## 1.11.0
1213
### Dupe Checking
1314
- `force_resend` can be set in `data` section to override dupe handling ( contribution by [@lollox80](https://github.com/lollox80))

custom_components/supernotify/delivery.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
ATTR_NAME,
88
CONF_ACTION,
99
CONF_ALIAS,
10-
CONF_CONDITION,
1110
CONF_CONDITIONS,
1211
CONF_DEBUG,
1312
CONF_ENABLED,
@@ -77,8 +76,6 @@ def __init__(self, name: str, conf: ConfigType, transport: "Transport") -> None:
7776
self.enabled: bool = conf.get(CONF_ENABLED, self.transport.enabled)
7877
self.occupancy: str = conf.get(CONF_OCCUPANCY, OCCUPANCY_ALL)
7978
self.conditions_config: list[ConfigType] | None = conf.get(CONF_CONDITIONS)
80-
if not conf.get(CONF_CONDITIONS) and conf.get(CONF_CONDITION):
81-
self.conditions_config = conf.get(CONF_CONDITION)
8279
self.conditions: ConditionsFunc | None = None
8380
self.transport_data: dict[str, Any] = {}
8481
if self.options.get(OPTION_TARGET_SELECT):

custom_components/supernotify/scenario.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# type: ignore[attr-defined,unused-ignore]
2424
from homeassistant.components.trace import async_store_trace
2525
from homeassistant.components.trace.models import ActionTrace
26-
from homeassistant.const import ATTR_FRIENDLY_NAME, ATTR_NAME, CONF_ALIAS, CONF_CONDITION, CONF_CONDITIONS
26+
from homeassistant.const import ATTR_FRIENDLY_NAME, ATTR_NAME, CONF_ALIAS, CONF_CONDITIONS
2727
from homeassistant.core import Context, HomeAssistant
2828
from homeassistant.helpers.typing import ConfigType
2929

@@ -65,8 +65,6 @@ def __init__(
6565
self.alias: str | None = scenario_definition.get(CONF_ALIAS)
6666
self.conditions: ConditionsFunc | None = None
6767
self.conditions_config: list[ConfigType] | None = scenario_definition.get(CONF_CONDITIONS)
68-
if not scenario_definition.get(CONF_CONDITIONS) and scenario_definition.get(CONF_CONDITION):
69-
self.conditions_config = scenario_definition.get(CONF_CONDITION)
7068
self.media: dict[str, Any] | None = scenario_definition.get(CONF_MEDIA)
7169
self.action_groups: list[str] = scenario_definition.get(CONF_ACTION_GROUP_NAMES, [])
7270
self._config_delivery: dict[str, DeliveryCustomization]

custom_components/supernotify/schema.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,17 @@ def validate_scenario_names(scenarios: dict) -> dict:
293293
})
294294

295295

296+
def _migrate_condition(config: dict) -> dict:
297+
"""Migrate deprecated 'condition' key to 'conditions'."""
298+
if CONF_CONDITION in config:
299+
if CONF_CONDITIONS not in config:
300+
config = {**config, CONF_CONDITIONS: config[CONF_CONDITION]}
301+
config = {k: v for k, v in config.items() if k != CONF_CONDITION}
302+
return config
303+
304+
296305
DELIVERY_SCHEMA = vol.All(
297-
cv.deprecated(key=CONF_CONDITION),
306+
_migrate_condition,
298307
DELIVERY_CONFIG_SCHEMA.extend({
299308
vol.Required(CONF_TRANSPORT): vol.In(TRANSPORT_VALUES),
300309
vol.Optional(CONF_ALIAS): cv.string,
@@ -303,7 +312,6 @@ def validate_scenario_names(scenarios: dict) -> dict:
303312
vol.Optional(CONF_TITLE): vol.Any(None, cv.string),
304313
vol.Optional(CONF_ENABLED): cv.boolean,
305314
vol.Optional(CONF_OCCUPANCY, default=OCCUPANCY_ALL): vol.In(OCCUPANCY_VALUES),
306-
vol.Optional(CONF_CONDITION): cv.CONDITIONS_SCHEMA,
307315
vol.Optional(CONF_CONDITIONS): cv.CONDITIONS_SCHEMA,
308316
}),
309317
)
@@ -367,12 +375,11 @@ def validate_scenario_names(scenarios: dict) -> dict:
367375

368376

369377
SCENARIO_SCHEMA = vol.All(
370-
cv.deprecated(key=CONF_CONDITION),
378+
_migrate_condition,
371379
cv.deprecated(key="delivery_selection"),
372380
vol.Schema({
373381
vol.Optional(CONF_ALIAS): cv.string,
374382
vol.Optional(CONF_ENABLED, default=True): cv.boolean,
375-
vol.Optional(CONF_CONDITION): cv.CONDITIONS_SCHEMA,
376383
vol.Optional(CONF_CONDITIONS): cv.CONDITIONS_SCHEMA,
377384
vol.Optional(CONF_MEDIA): MEDIA_SCHEMA,
378385
vol.Optional(CONF_ACTION_GROUP_NAMES, default=[]): vol.All(cv.ensure_list, [cv.string]),

tests/components/supernotify/test_delivery.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest.mock import AsyncMock, Mock
22

3-
from homeassistant.const import CONF_ACTION, CONF_CONDITION
3+
from homeassistant.const import CONF_ACTION, CONF_CONDITIONS
44

55
from custom_components.supernotify.const import (
66
CONF_DELIVERY_DEFAULTS,
@@ -73,14 +73,14 @@ async def test_repair_for_bad_conditions(mock_context: Context) -> None:
7373
mock_context.hass_api.build_conditions = AsyncMock(side_effect=Exception("integrations")) # type: ignore
7474
uut = Delivery(
7575
"generic",
76-
{CONF_CONDITION: {"condition": "xor"}},
76+
{CONF_CONDITIONS: [{"condition": "xor"}]},
7777
GenericTransport(mock_context, {CONF_DELIVERY_DEFAULTS: {CONF_ACTION: "notify.notify"}}),
7878
)
7979
assert await uut.initialize(mock_context) is False
8080
mock_context.hass_api.raise_issue.assert_called_with( # type: ignore
8181
"delivery_generic_invalid_condition",
8282
issue_key="delivery_invalid_condition",
83-
issue_map={"delivery": "generic", "condition": "{'condition': 'xor'}", "exception": "integrations"},
83+
issue_map={"delivery": "generic", "condition": "[{'condition': 'xor'}]", "exception": "integrations"},
8484
learn_more_url="https://supernotify.rhizomatics.org.uk/deliveries",
8585
)
8686

0 commit comments

Comments
 (0)