Skip to content
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

Rework MQTT config merging and adding defaults #90529

Merged
merged 7 commits into from
Apr 4, 2023

Conversation

jbouwh
Copy link
Contributor

@jbouwh jbouwh commented Mar 30, 2023

Proposed change

Rework the MQTT config processing and remove not needed check since we do not import broker settings from configuration.yaml anymore.
Further changes:

  • The CONFIG_SCHEMA_ENTRY now takes care of adding the defaults as they are missing and validates the entry data at entry startup.
  • Invalid config entry settings will throw a ConfigEntryError, this will set the config entry in error allowing the user to correct the entry settings.
  • The old CONFIG_SCHEMA_BASE schema used for yaml based settings was a left over and is removed now.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

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.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @emontnemery, mind taking a look at this pull request as it has been labeled with an integration (mqtt) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of mqtt can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign mqtt Removes the current integration label and assignees on the pull request, add the integration domain after the command.

@jbouwh jbouwh marked this pull request as draft March 30, 2023 16:17
@jbouwh jbouwh marked this pull request as ready for review March 30, 2023 21:54
entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
entry.data = {"broker": "test-broker", "port": 1234}
await mqtt_mock_entry_no_yaml_config()
await hass.async_block_till_done()

# Config entry data should now be updated
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't update the entry data anymore to mix missing items. Defaults are added to the loaded entry date using the schema. For an invalid config entry the user can correct the settings in the UI.

@@ -183,15 +183,14 @@ async def test_user_connection_works(
assert result["type"] == "form"

result = await hass.config_entries.flow.async_configure(
result["flow_id"], {"broker": "127.0.0.1", "advanced_options": False}
Copy link
Contributor Author

@jbouwh jbouwh Mar 30, 2023

Choose a reason for hiding this comment

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

"advanced_options" was filtered out, that was why tests pass, it should not be a setting in the entry, and in real live this setting was never added to the MQTT config entry data.

)

assert result["type"] == "create_entry"
assert result["result"].data == {
"broker": "127.0.0.1",
"port": 1883,
"discovery": True,
"discovery_prefix": "homeassistant",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Default settings are not added to the entry by default, they are added during entry load by applying the schema.

homeassistant/components/mqtt/__init__.py Outdated Show resolved Hide resolved
@@ -169,9 +158,11 @@
CONFIG_SCHEMA_ENTRY = vol.Schema(
Copy link
Contributor

Choose a reason for hiding this comment

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

In a follow-up, I think we can drop the schema? There's no reason to validate config entry options / data?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The schema adds default values. If we want to drop the schema we need to change the config flow to save basic broker settings with broker CONFIG_SCHEMA_ENTRY. So I don't see an option to drop it yet.

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand, but there's no need to add default values via a schema, just use dict.get instead and pass the wanted default value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So let's implement this in a follow up PR

homeassistant/components/mqtt/models.py Outdated Show resolved Hide resolved
"""Return typed MqttData from hass.data[DATA_MQTT]."""
mqtt_data: MqttData
if ensure_exists:
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of just removing the check, maybe raise instead:

if DATA_MQTT not in hass.data:
    raise HomeAssistantError

Or maybe the KeyError is good enough?

Copy link
Contributor Author

@jbouwh jbouwh Apr 4, 2023

Choose a reason for hiding this comment

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

KeyError should be enough I think

homeassistant/components/mqtt/client.py Outdated Show resolved Hide resolved
homeassistant/components/mqtt/__init__.py Outdated Show resolved Hide resolved
@home-assistant
Copy link

home-assistant bot commented Apr 4, 2023

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft April 4, 2023 11:47
@jbouwh jbouwh marked this pull request as ready for review April 4, 2023 13:28
@home-assistant home-assistant bot requested a review from emontnemery April 4, 2023 13:28
@jbouwh jbouwh merged commit 4a0d3e8 into home-assistant:dev Apr 4, 2023
@jbouwh jbouwh deleted the mqtt-cleanup-config-processing branch April 4, 2023 16:12
@github-actions github-actions bot locked and limited conversation to collaborators Apr 5, 2023
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.

2 participants