Fix KeyError in decora setup#28279
Conversation
| try: | ||
| device["name"] = device_config[CONF_NAME] | ||
| except KeyError: | ||
| device["name"] = util.slugify(address) |
There was a problem hiding this comment.
Add a custom voluptuous config validator instead that sets the name item if it's missing in the device config. See eg:
https://github.com/home-assistant/home-assistant/blob/6ac7796fb7599565cf44e90fad1efefa56c43d5a/homeassistant/components/manual/alarm_control_panel.py#L63-L75
There was a problem hiding this comment.
Do I have to create a complete new schema or can I just extend the light platform schema and add this custom config validator?
There was a problem hiding this comment.
I have tried to extend the light platfrom schema, but without any luck so far.
Building a new one works though:
DEVICE_SCHEMA = vol.Schema(
{vol.Optional(CONF_NAME): cv.string, vol.Required(CONF_API_KEY): cv.string}
)
PLATFORM_SCHEMA = vol.Schema(
vol.All(
{
vol.Required(CONF_PLATFORM): "decora",
vol.Optional(CONF_DEVICES, default={}): {cv.string: DEVICE_SCHEMA},
},
_name_validator,
)
)@MartinHjelmare am I missing something? :)
There was a problem hiding this comment.
Something like this:
PLATFORM_SCHEMA = vol.Schema(
vol.All(
PLATFORM_SCHEMA.extend({...}),
_name_validator
)
)There was a problem hiding this comment.
Yeah you are completely right, did not think of that. My bad 😁 solved with 994ec3e
* Imported homeassistant.util and slugified address if no name is specified * Added a custom validator function in case name is not set in config * Removed logger.debug line only used for testing
Breaking Change: None
Description:
Fix KeyError: 'name' in decora setup.
Related issue (if applicable): fixes #27643
Pull request with documentation for home-assistant.io (if applicable): home-assistant/home-assistant.io#<home-assistant.io PR number goes here>
Example entry for
configuration.yaml(if applicable):namedoes not need to be specified, see documentation exampleChecklist:
tox. Your PR cannot be merged unless tests passIf user exposed functionality or configuration variables are added/changed:
see Fixed and Updated decora switch documentation home-assistant.io#11028
If the code communicates with devices, web services, or third-party tools:
python3 -m script.hassfest.requirements_all.txtby runningpython3 -m script.gen_requirements_all..coveragerc.If the code does not interact with devices: