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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dedup and clarify imported konnected config flows #32138

Merged
merged 4 commits into from
Feb 25, 2020

Conversation

kit-klein
Copy link
Contributor

@kit-klein kit-klein commented Feb 24, 2020

Proposed change

This PR addresses some of the UX concerns brought up in item 1 and 2 of #32076 and should be included in the 0.106 release.

Previously importing from configuration.yaml and ssdp discovery would result in two config flows being started. Now the ssdp discovered flow is aborted and it's host info utilized by the import flow. This more accurately mimics historical konnected component behavior where host info was discovered if not included in the configuration.yaml.

This PR also includes a small change to utilize imported configuration.yaml options until the user goes thru the options flow. This has no impact on Konnected panels that are being setup completely thru the config flow but it does allow for configuration.yaml based setups to be imported and utilized without forcing the user to run thru the options flow.

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)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example entry for configuration.yaml:

# Example configuration.yaml
konnected:
  access_token: arandomstringvalue
  api_host: http://192.168.1.223:8123 #IP address of Home Assistant
  devices:
    - id: a020a61b6233  
      binary_sensors:
        - zone: 4
          type: motion
          name: 'Hallway Motion'
        - zone: 5
          type: window
          name: 'Master Bedroom Window'
        - zone: 6
          type: window
          name: 'Downstairs Windows'
      switches:
        - zone: 1
          name: 'siren'

Additional information

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
  • 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.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 馃 Silver
  • 馃 Gold
  • 馃弳 Platinum

@probot-home-assistant
Copy link

This pull request needs to be manually signed off by @balloob before it can get merged.

@probot-home-assistant
Copy link

Hey there @heythisisnate, mind taking a look at this pull request as its been labeled with a integration (konnected) you are listed as a codeowner for? Thanks!

@project-bot project-bot bot moved this from Needs review to By Code Owner in Dev Feb 24, 2020
@heythisisnate
Copy link
Contributor

LGTM

@codecov
Copy link

codecov bot commented Feb 24, 2020

Codecov Report

Merging #32138 into dev will decrease coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev   #32138      +/-   ##
==========================================
- Coverage   94.74%   94.73%   -0.01%     
==========================================
  Files         767      768       +1     
  Lines       55504    55619     +115     
==========================================
+ Hits        52587    52693     +106     
- Misses       2917     2926       +9
Impacted Files Coverage 螖
homeassistant/util/dt.py 95.36% <0%> (-4.12%) 猬囷笍
homeassistant/components/uk_transport/sensor.py 93.47% <0%> (-0.73%) 猬囷笍
homeassistant/components/buienradar/camera.py 96.34% <0%> (-0.05%) 猬囷笍
homeassistant/components/twitch/sensor.py 96.7% <0%> (-0.04%) 猬囷笍
homeassistant/components/demo/media_player.py 95.67% <0%> (-0.03%) 猬囷笍
homeassistant/components/device_tracker/legacy.py 99.14% <0%> (-0.01%) 猬囷笍
homeassistant/components/rflink/light.py 100% <0%> (酶) 猬嗭笍
homeassistant/components/darksky/sensor.py 96.81% <0%> (酶) 猬嗭笍
homeassistant/components/mqtt/fan.py 97.96% <0%> (酶) 猬嗭笍
homeassistant/components/withings/const.py 100% <0%> (酶) 猬嗭笍
... and 43 more

Continue to review full report at Codecov.

Legend - Click here to learn more
螖 = absolute <relative> (impact), 酶 = not affected, ? = missing data
Powered by Codecov. Last update df93636...9c355e4. Read the comment docs.

@springstan springstan added this to the 0.106.0 milestone Feb 24, 2020
@balloob
Copy link
Member

balloob commented Feb 24, 2020

@frenck We need to make sure we update translations on the rc branch before release to pick up the changes from this PR.

description_placeholders={"id": self.unique_id},
)

# if we have ssdp discovered applicable host info use it
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need this? If the user wants discovery help the user can use that approach instead of config yaml.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After receiving feedback on the beta build I think we do need to support this use case. The legacy konnected component automatically discovered host info and then applied the config.yaml.

The config.yaml can be extensive for this integration so we shouldn't require users to abandon what they have. This flow makes for a smooth upgrade path existing konnected users.

Copy link
Member

Choose a reason for hiding this comment

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

Ok.

if entry and (
entry.data[CONF_HOST] != self.data[CONF_HOST]
or entry.data[CONF_PORT] != self.data[CONF_PORT]
):
# update an existing config entry if host info changes
entry_data = copy.deepcopy(entry.data)
entry_data.update(self.data)
self.hass.config_entries.async_update_entry(entry, data=entry_data)
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cool - I'll swap over to this

options={},
)
entry.add_to_hass(hass)
hass.data[panel.DOMAIN] = {
Copy link
Member

Choose a reason for hiding this comment

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

It's better to set up the integration using the mock entry than to mock integration code. Whenever we mock integration code in tests of the integration, the tests become less robust.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand. The panel module is well isolated from the rest of the integration and HA architecture - it just needs to parse and act upon passed in config entry data (which the integration also defines).

Bypassing the integration setup made it really clean to access, control, and assert behaviors based on the panel class methods. I think these tests might be an exception where directly mocking some workings of the integration shouldn't add maintenance overhead.

Copy link
Member

Choose a reason for hiding this comment

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

That we need to mock this is a sign that it's not isolated.

We don't really care about what the panel is doing. We just care about what the result in the core is upon different input that the panel forwards. It's better to set up the integration and assert core state.

Copy link
Member

Choose a reason for hiding this comment

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

Or if the info is flowing the other way, we assert that the library is called correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok - will fix

@MartinHjelmare MartinHjelmare changed the title 0.106 Beta - dedup and clarify imported konnected config flows Dedup and clarify imported konnected config flows Feb 25, 2020
}

# confirm the device settings are saved in hass.data
assert hass.data[panel.DOMAIN][panel.CONF_DEVICES] == {
Copy link
Member

Choose a reason for hiding this comment

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

Saving the data like this is just a detail which could change in the future. This isn't something we should assert.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you referring to asserting that konnected integration data resides in hass.data?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. 馃憤

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it - updating

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in latest commits. There is still one reference to hass.data in these tests to get the instance of the AlarmPanel created when the integration is setup. This is essential to confirm that the data store is formatted correctly - a check I want to keep to ensure that no future code changes in this module break that data structure since it is utilized when setting up the platforms.

@kit-klein
Copy link
Contributor Author

The last commit prior to this comment (9c355e4) was a minor change to reflect a key change used to identify the soon to be released pro board. It has no impact on existing equipment.

Copy link
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.

If we do these three types of changes, we'll no longer interact with the panel instance directly in the tests, making them more robust.


# confirm panel instance was created and configured
# hass.data is the only mechanism to get a reference to the created panel instance
device = hass.data[panel.DOMAIN][panel.CONF_DEVICES]["112233445566"]["panel"]
await device.update_switch("1", 0)
Copy link
Member

Choose a reason for hiding this comment

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

update_switch should be tested in konnected switch platform tests.

"state": None,
"type": "door",
},
assert device.stored_configuration == {
Copy link
Member

Choose a reason for hiding this comment

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

Instead we should assert that mock_panel.put_settings is called with the correct configuration during device sync.

@@ -92,9 +93,6 @@ def mock_constructor(host, port, websession):
options=device_options,
Copy link
Member

Choose a reason for hiding this comment

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

Instead of creating the data and options first and using that to create a mock entry, just pass the configuration to async_setup_component. It will set up the integration and use the config yaml schemas of the integration before importing the config into a config entry.

@kit-klein
Copy link
Contributor Author

If we do these three types of changes, we'll no longer interact with the panel instance directly in the tests, making them more robust.

If we had a single test_konnected.py module that tested the integration as a black box these suggestions would be the ideal strategy. In the current implementation they defeat the purpose of the attempted limited scope unit testing that is occurring here.

Directly accessing hass.data as part of a testing strategy is common throughout the codebase - including in the platinum integration examples like hue. It is a global data store but the content within each DOMAIN is defined/managed by the integration - ensuring robustness within the scope of the integration. If abstracting accessors existed for the data store it would be heinous to access it directly but as implemented it's not unreasonable to utilize it as a mechanism for testing.

Are these changes FYI/suggestions or requirements for PR approval? If they are required I will make them but I would like to understand why these items are blocking this PR but identical test code was approved without issue in the recent konnected PR. That would have been a much more ideal time to overhaul testing architecture vs now when attempting to fix beta release bugs.

@MartinHjelmare
Copy link
Member

It's ok to improve the tests in a future PR. We shouldn't add any new tests that are not robust though.

As we said many times before, trying to get a huge PR like last PR merged is never a good idea since there will always be things that are missed. This is just confirmation.

Dev automation moved this from By Code Owner to Reviewer approved Feb 25, 2020
@MartinHjelmare MartinHjelmare merged commit 5488389 into home-assistant:dev Feb 25, 2020
Dev automation moved this from Reviewer approved to Done Feb 25, 2020
@kit-klein
Copy link
Contributor Author

@MartinHjelmare Understood. I greatly appreciate the high standards you and the team are implementing. Thank you for all the help.

balloob pushed a commit that referenced this pull request Feb 25, 2020
* dedup config flows

* use default (imported) options until user goes thru options flow

* address pr feedback

* correct key used to distinguish pro model
@lock lock bot locked and limited conversation to collaborators Feb 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Dev
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

6 participants