Skip to content

Discover new added device at runtime in AVM Fritz!Smarthome#103859

Merged
mib1185 merged 15 commits into
home-assistant:devfrom
mib1185:fritzbox/discovery-new-device-at-runtime
Nov 20, 2023
Merged

Discover new added device at runtime in AVM Fritz!Smarthome#103859
mib1185 merged 15 commits into
home-assistant:devfrom
mib1185:fritzbox/discovery-new-device-at-runtime

Conversation

@mib1185
Copy link
Copy Markdown
Member

@mib1185 mib1185 commented Nov 12, 2023

Proposed change

This will add support of creating entities for newly paired devices at runtime.

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
Copy Markdown
Contributor

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

Code owner commands

Code owners of fritzbox 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 fritzbox Removes the current integration label and assignees on the pull request, add the integration domain after the command.

Comment thread homeassistant/components/fritzbox/binary_sensor.py Outdated
Comment thread homeassistant/components/fritzbox/binary_sensor.py
Comment thread homeassistant/components/fritzbox/coordinator.py Outdated
Copy link
Copy Markdown
Member Author

@mib1185 mib1185 left a comment

Choose a reason for hiding this comment

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

I've reworked this PR, it should now be less complex.

  • the coordinator has two new properties which show the new devices and templates (during init all devices/templates are new)
  • all platforms registers to coordinator updates and add new entities if needed
  • at each platform init all entities will be added

@mib1185 mib1185 marked this pull request as ready for review November 19, 2023 14:07
Comment thread homeassistant/components/fritzbox/binary_sensor.py Outdated
Comment thread homeassistant/components/fritzbox/binary_sensor.py
@mib1185 mib1185 requested a review from elupus November 19, 2023 16:22
Comment thread homeassistant/components/fritzbox/binary_sensor.py Outdated
Comment thread homeassistant/components/fritzbox/binary_sensor.py
@elupus
Copy link
Copy Markdown
Contributor

elupus commented Nov 19, 2023

Im sorry to say i think you should partly revert to how you had ut before so something like

coordinator
init()
self.devices_last = set()

update()

devices = {
template.ain
for ain in templates
}
self.devices_new = devices - self.devices_last

platform:

standard coordinator listener callback.

just add all in devices_new.

devices =

@mib1185 mib1185 requested a review from elupus November 19, 2023 19:16
Comment thread homeassistant/components/fritzbox/binary_sensor.py Outdated
Comment thread homeassistant/components/fritzbox/coordinator.py
@mib1185 mib1185 requested a review from elupus November 19, 2023 20:10
Copy link
Copy Markdown
Contributor

@elupus elupus left a comment

Choose a reason for hiding this comment

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

Looks reasonable. There are some optimizations you could make.

  • avoid calling add_entities with empty lists (early return in function)
  • calling add_entities with a generator instead of a list so you dont have to pre allocate the list (async_add_entities(Entity(x.ain) for x.in new_devices))
  • figuring out if there are som common code. For example s utility function to get the typed coordinator object like get_coordinator(entry.id)

@elupus
Copy link
Copy Markdown
Contributor

elupus commented Nov 20, 2023

Ps. Martin may have some more thoughs. So might be good to wait a bit.

@mib1185 mib1185 marked this pull request as draft November 20, 2023 08:20
@mib1185
Copy link
Copy Markdown
Member Author

mib1185 commented Nov 20, 2023

Valid points, will work on the optimizations today evening 👍

Comment thread homeassistant/components/fritzbox/light.py Outdated
@mib1185 mib1185 marked this pull request as ready for review November 20, 2023 15:53
@mib1185 mib1185 requested a review from elupus November 20, 2023 16:07
Copy link
Copy Markdown
Contributor

@elupus elupus left a comment

Choose a reason for hiding this comment

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

Nice!

@mib1185 mib1185 merged commit 9c5e0fc into home-assistant:dev Nov 20, 2023
@mib1185 mib1185 deleted the fritzbox/discovery-new-device-at-runtime branch November 20, 2023 16:13
if not coordinator.new_devices:
return
async_add_entities(
[
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.

We can remove the brackets to make it a generator expression instead of a list comprehension.

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.

@github-actions github-actions Bot locked and limited conversation to collaborators Nov 21, 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.

4 participants