-
-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
Use voluptuous for input_slider, input_boolean, input_select #3256
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7936db6
Use voluptuous for input slider
kellerza 10f7471
floats
kellerza 3795aec
_setup_component
kellerza d194286
Imperative mood
kellerza 83aa08a
CONFIG_SCHEMA
kellerza 1284768
None returns empty ensure_list
kellerza e95a35f
allow_extra
kellerza 7902823
bool
kellerza 0e90bfc
restore ensure_list behaviour
kellerza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
_setup_component
- Loading branch information
commit 3795aec283692f5eb2c79c5746b2cff981ac366d
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
"""Helper methods for components within Home Assistant.""" | ||
import re | ||
|
||
from typing import Any, Iterable, Tuple, List, Dict | ||
from typing import Any, Iterable, Tuple, Sequence, Dict | ||
|
||
from homeassistant.const import CONF_PLATFORM | ||
|
||
# Typing Imports and TypeAlias | ||
# pylint: disable=using-constant-test,unused-import | ||
# pylint: disable=using-constant-test,unused-import,wrong-import-order | ||
if False: | ||
from logging import Logger # NOQA | ||
|
||
|
@@ -34,7 +34,19 @@ def config_per_platform(config: ConfigType, | |
yield platform, item | ||
|
||
|
||
def extract_domain_configs(config: ConfigType, domain: str) -> List[str]: | ||
def extract_domain_configs(config: ConfigType, domain: str) -> Sequence[str]: | ||
"""Extract keys from config for given domain name.""" | ||
pattern = re.compile(r'^{}(| .+)$'.format(domain)) | ||
return [key for key in config.keys() if pattern.match(key)] | ||
|
||
|
||
def dict_items_from_list(list_of_dicts: Sequence[dict]) \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should no longer need this method. |
||
-> Iterable[Tuple[Any, Any]]: | ||
"""Iterate over all dicts' items in a list.""" | ||
if not isinstance(list_of_dicts, list): | ||
list_of_dicts = [list_of_dicts] | ||
seen = {} | ||
for a_dict in list_of_dicts: | ||
for key, val in a_dict.items(): | ||
yield key, val, seen.get(key) | ||
seen[key] = val |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,14 @@ | |
import unittest | ||
|
||
import voluptuous as vol | ||
from tests.common import get_test_home_assistant | ||
|
||
from homeassistant.components import input_boolean | ||
from homeassistant.bootstrap import _setup_component | ||
from homeassistant.components.input_boolean import ( | ||
DOMAIN, PLATFORM_SCHEMA, is_on, turn_off, turn_on) | ||
from homeassistant.const import ( | ||
STATE_ON, STATE_OFF, ATTR_ICON, ATTR_FRIENDLY_NAME) | ||
|
||
from tests.common import get_test_home_assistant | ||
|
||
|
||
class TestInputBoolean(unittest.TestCase): | ||
"""Test the input boolean module.""" | ||
|
@@ -24,38 +25,41 @@ def tearDown(self): # pylint: disable=invalid-name | |
|
||
def test_config(self): | ||
"""Test config.""" | ||
with self.assertRaises(vol.Invalid): | ||
input_boolean.PLATFORM_SCHEMA(None) | ||
with self.assertRaises(vol.Invalid): | ||
input_boolean.PLATFORM_SCHEMA({}) | ||
with self.assertRaises(vol.Invalid): | ||
input_boolean.PLATFORM_SCHEMA({'name with space': None}) | ||
invalid_configs = [ | ||
None, | ||
{}, | ||
{'name with space': None}, | ||
] | ||
|
||
for cfg in invalid_configs: | ||
with self.assertRaises(vol.Invalid): | ||
PLATFORM_SCHEMA(cfg) | ||
self.assertFalse( | ||
_setup_component(self.hass, DOMAIN, {DOMAIN: cfg})) | ||
|
||
def test_methods(self): | ||
"""Test is_on, turn_on, turn_off methods.""" | ||
self.assertTrue(input_boolean.setup(self.hass, { | ||
'input_boolean': { | ||
'test_1': None, | ||
} | ||
})) | ||
self.assertTrue(_setup_component(self.hass, DOMAIN, {DOMAIN: { | ||
'test_1': None, | ||
}})) | ||
entity_id = 'input_boolean.test_1' | ||
|
||
self.assertFalse( | ||
input_boolean.is_on(self.hass, entity_id)) | ||
is_on(self.hass, entity_id)) | ||
|
||
input_boolean.turn_on(self.hass, entity_id) | ||
turn_on(self.hass, entity_id) | ||
|
||
self.hass.block_till_done() | ||
|
||
self.assertTrue( | ||
input_boolean.is_on(self.hass, entity_id)) | ||
is_on(self.hass, entity_id)) | ||
|
||
input_boolean.turn_off(self.hass, entity_id) | ||
turn_off(self.hass, entity_id) | ||
|
||
self.hass.block_till_done() | ||
|
||
self.assertFalse( | ||
input_boolean.is_on(self.hass, entity_id)) | ||
is_on(self.hass, entity_id)) | ||
|
||
input_boolean.toggle(self.hass, entity_id) | ||
|
||
|
@@ -68,16 +72,14 @@ def test_config_options(self): | |
"""Test configuration options.""" | ||
count_start = len(self.hass.states.entity_ids()) | ||
|
||
self.assertTrue(input_boolean.setup(self.hass, { | ||
'input_boolean': { | ||
'test_1': None, | ||
'test_2': { | ||
'name': 'Hello World', | ||
'icon': 'work', | ||
'initial': True, | ||
}, | ||
self.assertTrue(_setup_component(self.hass, DOMAIN, {DOMAIN: { | ||
'test_1': None, | ||
'test_2': { | ||
'name': 'Hello World', | ||
'icon': 'mdi:work', | ||
'initial': True, | ||
}, | ||
})) | ||
}})) | ||
|
||
self.assertEqual(count_start + 2, len(self.hass.states.entity_ids())) | ||
|
||
|
@@ -94,4 +96,32 @@ def test_config_options(self): | |
self.assertEqual(STATE_ON, state_2.state) | ||
self.assertEqual('Hello World', | ||
state_2.attributes.get(ATTR_FRIENDLY_NAME)) | ||
self.assertEqual('work', state_2.attributes.get(ATTR_ICON)) | ||
self.assertEqual('mdi:work', state_2.attributes.get(ATTR_ICON)) | ||
|
||
def test_multiple_platform_style(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not supported once you're back to be just using |
||
"""Load from multiple sub components.""" | ||
count_start = len(self.hass.states.entity_ids()) | ||
|
||
self.assertTrue(_setup_component(self.hass, DOMAIN, { | ||
DOMAIN: { | ||
'test_1': None, | ||
'test_2': { | ||
'name': 'Hello World', | ||
'icon': 'mdi:work', | ||
'initial': True, | ||
}, | ||
}, | ||
DOMAIN+" 2": { | ||
'test_3': None, | ||
} | ||
})) | ||
|
||
self.assertEqual(count_start + 3, len(self.hass.states.entity_ids())) | ||
|
||
state_1 = self.hass.states.get('input_boolean.test_1') | ||
state_2 = self.hass.states.get('input_boolean.test_2') | ||
state_3 = self.hass.states.get('input_boolean.test_3') | ||
|
||
self.assertIsNotNone(state_1) | ||
self.assertIsNotNone(state_2) | ||
self.assertIsNotNone(state_3) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For all other checks we raise if the value is invalid, but here we actually change the value. Let's raise here too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be a change in current behaviour, but in the end more consistent. Will do the change