Replace 'validate_config' with voluptuous

This commit is contained in:
Fabian Affolter 2016-12-27 20:31:50 +01:00
parent ba3fdbb8e7
commit ec4a528e9a
No known key found for this signature in database
GPG Key ID: DDF3D6F44AAB1336

View File

@ -99,22 +99,20 @@ hello_state:
text: 'Hello, World!' text: 'Hello, World!'
``` ```
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. 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.
```python ```python
from homeassistant.helpers import validate_config import voluptuous as vol
import homeassistant.helpers.config_validation as cv
[...] [...]
if not validate_config(config, {DOMAIN: [CONF_TEXT]}, _LOGGER): PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
return False vol.Required(CONF_TEXT): cv.string,
})
``` ```
If `text:` is missing, there will be a warning in the log file. If `text:` is missing, there will be a warning in the log file.
```bash
16-03-12 14:37:37 ERROR (MainThread) [custom_components.hello_state] Missing required configuration items in hello_state: text
16-03-12 14:37:37 ERROR (MainThread) [homeassistant.bootstrap] component hello_state failed to initialize
```
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. 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.
<p class='img'> <p class='img'>