Skip to content

Fix KeyError in decora setup#28279

Merged
MartinHjelmare merged 3 commits into
home-assistant:devfrom
springstan:fixingDecoraSetupError
Oct 30, 2019
Merged

Fix KeyError in decora setup#28279
MartinHjelmare merged 3 commits into
home-assistant:devfrom
springstan:fixingDecoraSetupError

Conversation

@springstan
Copy link
Copy Markdown
Member

@springstan springstan commented Oct 27, 2019

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):

name does not need to be specified, see documentation example

light:
  - platform: decora
    devices:
      00:21:4D:00:00:01:
        api_key: 003026

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly. Update and include derived files by running python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

@MartinHjelmare MartinHjelmare changed the title Fixed Error in decora setup KeyError: 'name' Fix KeyError in decora setup Oct 27, 2019
try:
device["name"] = device_config[CONF_NAME]
except KeyError:
device["name"] = util.slugify(address)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I have to create a complete new schema or can I just extend the light platform schema and add this custom config validator?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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? :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this:

PLATFORM_SCHEMA = vol.Schema(
    vol.All(
        PLATFORM_SCHEMA.extend({...}),
        _name_validator
    )
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you are completely right, did not think of that. My bad 😁 solved with 994ec3e

Comment thread homeassistant/components/decora/light.py Outdated
Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

@MartinHjelmare MartinHjelmare merged commit f8efc2a into home-assistant:dev Oct 30, 2019
@MartinHjelmare MartinHjelmare added this to the 0.101.0 milestone Oct 30, 2019
pvizeli pushed a commit that referenced this pull request Oct 30, 2019
* 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
@pvizeli pvizeli mentioned this pull request Oct 30, 2019
@lock lock Bot locked and limited conversation to collaborators Oct 31, 2019
@springstan springstan deleted the fixingDecoraSetupError branch December 16, 2019 12:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error in decora setup KeyError: 'name'

4 participants