Skip to content

Commit ec4a528

Browse files
committed
Replace 'validate_config' with voluptuous
1 parent ba3fdbb commit ec4a528

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

source/_cookbook/python_component_basic_state.markdown

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,20 @@ hello_state:
9999
text: 'Hello, World!'
100100
```
101101
102-
Thanks to `DEFAULT_TEXT` variable the component will launch even if no `text:` field is used in the `configuration.yaml` file. Quite often there are variables which are required. It's important to check if all mandatory configuration variables are provided. If not, the setup should fail. We will use the `validate_config` function as a helper to achive this. The next listing shows the essential parts.
102+
Thanks to `DEFAULT_TEXT` variable the component will launch even if no `text:` field is used in the `configuration.yaml` file. Quite often there are variables which are required. It's important to check if all mandatory configuration variables are provided. If not, the setup should fail. We will use `voluptuous` as a helper to achive this. The next listing shows the essential parts.
103103

104104
```python
105-
from homeassistant.helpers import validate_config
105+
import voluptuous as vol
106+
107+
import homeassistant.helpers.config_validation as cv
106108
[...]
107-
if not validate_config(config, {DOMAIN: [CONF_TEXT]}, _LOGGER):
108-
return False
109+
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
110+
vol.Required(CONF_TEXT): cv.string,
111+
})
109112
```
110113

111114
If `text:` is missing, there will be a warning in the log file.
112115

113-
```bash
114-
16-03-12 14:37:37 ERROR (MainThread) [custom_components.hello_state] Missing required configuration items in hello_state: text
115-
16-03-12 14:37:37 ERROR (MainThread) [homeassistant.bootstrap] component hello_state failed to initialize
116-
```
117-
118116
After a start or a restart of Home Assistant the component will be visible in the frontend if the `configuration.yaml` file is up-to-date.
119117

120118
<p class='img'>

0 commit comments

Comments
 (0)