Skip to content

Commit

Permalink
Test GASelector
Browse files Browse the repository at this point in the history
  • Loading branch information
farmio committed Jun 20, 2024
1 parent 465b9eb commit f08ca44
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
2 changes: 1 addition & 1 deletion homeassistant/components/knx/storage/knx_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ def _add_passive(self, schema: dict[vol.Marker, Any]) -> None:
def _add_dpt(self, schema: dict[vol.Marker, Any]) -> None:
"""Add DPT validator to the schema."""
if self.dpt is not None:
schema[vol.Required("dpt")] = vol.In(self.dpt)
schema[vol.Required("dpt")] = vol.In(item.value for item in self.dpt)
else:
schema[vol.Remove("dpt")] = object
122 changes: 122 additions & 0 deletions tests/components/knx/test_knx_selectors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
"""Test KNX selectors."""

import pytest
import voluptuous as vol

from homeassistant.components.knx.const import ColorTempModes
from homeassistant.components.knx.storage.knx_selector import GASelector

INVALID = "invalid"


@pytest.mark.parametrize(
("selector_config", "data", "expected"),
[
(
{},
{},
{"write": None, "state": None, "passive": []},
),
(
{},
{"write": "1/2/3"},
{"write": "1/2/3", "state": None, "passive": []},
),
(
{},
{"state": "1/2/3"},
{"write": None, "state": "1/2/3", "passive": []},
),
(
{},
{"passive": ["1/2/3"]},
{"write": None, "state": None, "passive": ["1/2/3"]},
),
(
{},
{"write": "1", "state": 2, "passive": ["1/2/3"]},
{"write": "1", "state": 2, "passive": ["1/2/3"]},
),
(
{"write": False},
{"write": "1/2/3"},
{"state": None, "passive": []},
),
(
{"write": False},
{"state": "1/2/3"},
{"state": "1/2/3", "passive": []},
),
(
{"write": False},
{"passive": ["1/2/3"]},
{"state": None, "passive": ["1/2/3"]},
),
(
{"passive": False},
{"passive": ["1/2/3"]},
{"write": None, "state": None},
),
(
{"passive": False},
{"write": "1/2/3"},
{"write": "1/2/3", "state": None},
),
# required keys
(
{"write_required": True},
{},
INVALID,
),
(
{"state_required": True},
{},
INVALID,
),
(
{"write_required": True},
{"write": "1/2/3"},
{"write": "1/2/3", "state": None, "passive": []},
),
(
{"state_required": True},
{"state": "1/2/3"},
{"write": None, "state": "1/2/3", "passive": []},
),
(
{"write_required": True},
{"state": "1/2/3"},
INVALID,
),
(
{"state_required": True},
{"write": "1/2/3"},
INVALID,
),
# dpt key
(
{"dpt": ColorTempModes},
{"write": "1/2/3"},
INVALID,
),
(
{"dpt": ColorTempModes},
{"write": "1/2/3", "dpt": "7.600"},
{"write": "1/2/3", "state": None, "passive": [], "dpt": "7.600"},
),
(
{"dpt": ColorTempModes},
{"write": "1/2/3", "state": None, "passive": [], "dpt": "invalid"},
INVALID,
),
],
)
def test_ga_selector(selector_config, data, expected):
"""Test GASelector."""
selector = GASelector(**selector_config)
if expected == INVALID:
with pytest.raises(vol.Invalid):
selector(data)
else:
result = selector(data)
assert result == expected

0 comments on commit f08ca44

Please sign in to comment.