-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Discover new added device at runtime in AVM Fritz!Smarthome #103859
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
Merged
mib1185
merged 15 commits into
home-assistant:dev
from
mib1185:fritzbox/discovery-new-device-at-runtime
Nov 20, 2023
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8793200
add handling for newly discovered devices
mib1185 44ffcb0
use `entry.async_on_unload`
mib1185 2d36e3e
rework
mib1185 707487f
remove commented code
mib1185 e6a7915
keep added_devices platform local
mib1185 27c225c
add tests
mib1185 f607945
add callback annotation
mib1185 d733660
uset set instead of list
mib1185 e511040
use dispatcher signal for new devices
mib1185 9ebfb53
coordinator provides new devices
mib1185 d74f33a
apply suggestion
mib1185 0096a0e
stop early when no new devices
mib1185 1eb6151
use generator when calling async_add_entities
mib1185 f0e9c69
prepare light entity inline
mib1185 0cfccf8
move getting coordintor into common function
mib1185 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,12 +14,11 @@ | |
| ) | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import EntityCategory | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.core import HomeAssistant, callback | ||
| from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
|
||
| from . import FritzBoxDeviceEntity | ||
| from .const import CONF_COORDINATOR, DOMAIN as FRITZBOX_DOMAIN | ||
| from .coordinator import FritzboxDataUpdateCoordinator | ||
| from .common import get_coordinator | ||
| from .model import FritzEntityDescriptionMixinBase | ||
|
|
||
|
|
||
|
|
@@ -68,18 +67,25 @@ async def async_setup_entry( | |
| hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback | ||
| ) -> None: | ||
| """Set up the FRITZ!SmartHome binary sensor from ConfigEntry.""" | ||
| coordinator: FritzboxDataUpdateCoordinator = hass.data[FRITZBOX_DOMAIN][ | ||
| entry.entry_id | ||
| ][CONF_COORDINATOR] | ||
|
|
||
| async_add_entities( | ||
| [ | ||
| FritzboxBinarySensor(coordinator, ain, description) | ||
| for ain, device in coordinator.data.devices.items() | ||
| for description in BINARY_SENSOR_TYPES | ||
| if description.suitable(device) | ||
| ] | ||
| ) | ||
| coordinator = get_coordinator(hass, entry.entry_id) | ||
|
|
||
| @callback | ||
| def _add_entities() -> None: | ||
| """Add devices.""" | ||
| if not coordinator.new_devices: | ||
| return | ||
| async_add_entities( | ||
| [ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| FritzboxBinarySensor(coordinator, ain, description) | ||
| for ain in coordinator.new_devices | ||
| for description in BINARY_SENSOR_TYPES | ||
| if description.suitable(coordinator.data.devices[ain]) | ||
| ] | ||
|
mib1185 marked this conversation as resolved.
|
||
| ) | ||
|
|
||
| entry.async_on_unload(coordinator.async_add_listener(_add_entities)) | ||
|
mib1185 marked this conversation as resolved.
|
||
|
|
||
| _add_entities() | ||
|
|
||
|
|
||
| class FritzboxBinarySensor(FritzBoxDeviceEntity, BinarySensorEntity): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| """Common functions for fritzbox integration.""" | ||
|
|
||
| from homeassistant.core import HomeAssistant | ||
|
|
||
| from .const import CONF_COORDINATOR, DOMAIN | ||
| from .coordinator import FritzboxDataUpdateCoordinator | ||
|
|
||
|
|
||
| def get_coordinator( | ||
| hass: HomeAssistant, config_entry_id: str | ||
| ) -> FritzboxDataUpdateCoordinator: | ||
| """Get coordinator for given config entry id.""" | ||
| coordinator: FritzboxDataUpdateCoordinator = hass.data[DOMAIN][config_entry_id][ | ||
| CONF_COORDINATOR | ||
| ] | ||
| return coordinator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.