Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions homeassistant/helpers/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ def __init__(self, config: Any = None) -> None:

def serialize(self) -> Any:
"""Serialize Selector for voluptuous_serialize."""
return {"selector": {self.selector_type: self.serialize_config()}}

def serialize_config(self) -> Any:
"""Serialize config."""
return self.config
return {"selector": {self.selector_type: self.config}}


SINGLE_ENTITY_SELECTOR_CONFIG_SCHEMA = vol.Schema(
Expand Down Expand Up @@ -617,8 +613,8 @@ class NumberSelector(Selector):
vol.Coerce(float), vol.Range(min=1e-3)
),
vol.Optional(CONF_UNIT_OF_MEASUREMENT): str,
vol.Optional(CONF_MODE, default=NumberSelectorMode.SLIDER): vol.Coerce(
NumberSelectorMode
vol.Optional(CONF_MODE, default=NumberSelectorMode.SLIDER): vol.All(
vol.Coerce(NumberSelectorMode), lambda val: val.value
),
}
),
Expand All @@ -629,13 +625,6 @@ def __init__(self, config: NumberSelectorConfig | None = None) -> None:
"""Instantiate a selector."""
super().__init__(config)

def serialize_config(self) -> Any:
"""Serialize the selector config."""
return {
**self.config,
"mode": self.config["mode"].value,
}

def __call__(self, data: Any) -> float:
"""Validate the passed selection."""
value: float = vol.Coerce(float)(data)
Expand Down
2 changes: 2 additions & 0 deletions tests/components/automation/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,7 @@ async def test_blueprint_automation(hass, calls):
"input": {
"trigger_event": "blueprint_event",
"service_to_call": "test.automation",
"a_number": 5,
},
}
}
Expand All @@ -1499,6 +1500,7 @@ async def test_blueprint_automation_bad_config(hass, caplog):
"input": {
"trigger_event": "blueprint_event",
"service_to_call": {"dict": "not allowed"},
"a_number": 5,
},
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/components/blueprint/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ async def test_fetch_blueprint_from_github_url(hass, aioclient_mock, url):
assert imported_blueprint.blueprint.inputs == {
"service_to_call": None,
"trigger_event": {"selector": {"text": {}}},
"a_number": {"selector": {"number": {"mode": "box", "step": 1.0}}},
}
assert imported_blueprint.suggested_filename == "balloob/motion_light"
assert imported_blueprint.blueprint.metadata["source_url"] == url
Expand Down
4 changes: 3 additions & 1 deletion tests/components/blueprint/test_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ async def test_list_blueprints(hass, hass_ws_client):
"input": {
"service_to_call": None,
"trigger_event": {"selector": {"text": {}}},
"a_number": {"selector": {"number": {"mode": "box", "step": 1.0}}},
},
"name": "Call service based on event",
},
Expand Down Expand Up @@ -95,6 +96,7 @@ async def test_import_blueprint(hass, aioclient_mock, hass_ws_client):
"input": {
"service_to_call": None,
"trigger_event": {"selector": {"text": {}}},
"a_number": {"selector": {"number": {"mode": "box", "step": 1.0}}},
},
"name": "Call service based on event",
"source_url": "https://github.com/balloob/home-assistant-config/blob/main/blueprints/automation/motion_light.yaml",
Expand Down Expand Up @@ -129,7 +131,7 @@ async def test_save_blueprint(hass, aioclient_mock, hass_ws_client):
assert msg["success"]
assert write_mock.mock_calls
assert write_mock.call_args[0] == (
"blueprint:\n name: Call service based on event\n domain: automation\n input:\n trigger_event:\n selector:\n text: {}\n service_to_call:\n source_url: https://github.com/balloob/home-assistant-config/blob/main/blueprints/automation/motion_light.yaml\ntrigger:\n platform: event\n event_type: !input 'trigger_event'\naction:\n service: !input 'service_to_call'\n entity_id: light.kitchen\n",
"blueprint:\n name: Call service based on event\n domain: automation\n input:\n trigger_event:\n selector:\n text: {}\n service_to_call:\n a_number:\n selector:\n number:\n mode: box\n step: 1.0\n source_url: https://github.com/balloob/home-assistant-config/blob/main/blueprints/automation/motion_light.yaml\ntrigger:\n platform: event\n event_type: !input 'trigger_event'\naction:\n service: !input 'service_to_call'\n entity_id: light.kitchen\n",
)


Expand Down
1 change: 1 addition & 0 deletions tests/components/trace/test_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,7 @@ def next_id():
"input": {
"trigger_event": "blueprint_event",
"service_to_call": "test.automation",
"a_number": 5,
},
},
}
Expand Down
4 changes: 4 additions & 0 deletions tests/helpers/test_selector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test selectors."""
from enum import Enum

import pytest
import voluptuous as vol

Expand Down Expand Up @@ -52,6 +54,8 @@ def default_converter(x):
config = {selector_type: schema}
selector.validate_selector(config)
selector_instance = selector.selector(config)
# We do not allow enums in the config, as they cannot serialize
assert not any(isinstance(val, Enum) for val in selector_instance.config.values())

# Use selector in schema and validate
vol_schema = vol.Schema({"selection": selector_instance})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ blueprint:
selector:
text:
service_to_call:
a_number:
selector:
number:
mode: "box"
trigger:
platform: event
event_type: !input trigger_event
Expand Down