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

Only compute homekit_controller accessory_info when entity is added or config changes #102145

Merged
merged 10 commits into from
Oct 17, 2023

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Oct 17, 2023

Proposed change

These values are never overridden by child classes but they have to be calculated each time the property
is called

Noticed this while trying to come up with a solution for #99088 (comment)

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:

These values are never overridden by child classes but
they have to be calculated each time the property
is called
@home-assistant
Copy link

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

Code owner commands

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

Copy link
Member

@Jc2k Jc2k left a comment

Choose a reason for hiding this comment

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

Definitely want to avoid this yes.

So they were originally done like this for the same reason as the other dynamic props. Their "lifetime" is only until c# changes. (A new entity map is created rather than updating the existing one in place).

It does still feel weird to have old versions of these objects loaded. But I'm not sure that it will make the behaviour any worse:

  • If entities get removed from the accessory db, we suck either way
  • iids shouldn't change

I guess we wouldn't pick up any feature changes or min/max changes or any of that. I think I saw an arch issue for supporting those sorts of things now, but they should be static atm.

I guess ideally we'd have a callback from HKdevice that lets us update these attrs on a c# change. That would be where the other dynamic attributes could be set when the arch issue is resolved.

@bdraco
Copy link
Member Author

bdraco commented Oct 17, 2023

I guess ideally we'd have a callback from HKdevice that lets us update these attrs on a c# change. That would be where the other dynamic attributes could be set when the arch issue is resolved.

I added that but just for the ones here to start

Copy link
Member

@Jc2k Jc2k left a comment

Choose a reason for hiding this comment

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

That was quick! Nice!

@bdraco
Copy link
Member Author

bdraco commented Oct 17, 2023

manual testing looks good.

It looks like we already have a device_config_changed helper... I guess we could add a test for fan that gains swing mode after a config change is reflected

@bdraco
Copy link
Member Author

bdraco commented Oct 17, 2023

I'll draft this and come up with a test tomorrow. Its getting late for me

@bdraco bdraco marked this pull request as draft October 17, 2023 07:23
@bdraco bdraco changed the title Only compute homekit_controller accessory_info and service once Only compute homekit_controller accessory_info when entity is added or config changes Oct 17, 2023
@bdraco
Copy link
Member Author

bdraco commented Oct 17, 2023

I think we could build on this in a future PR to remove entities when they are removed from a bridge similar to how esphome does it

Something to think about.

Gnight

@bdraco
Copy link
Member Author

bdraco commented Oct 17, 2023

I tried to add a test for removing features at runtime but it fails on dev as well


async def test_fan_remove_feature_at_runtime(hass: HomeAssistant) -> None:
    """Test that features can be removed at runtime."""
    entity_registry = er.async_get(hass)

    # Set up a basic fan that does not support oscillation
    accessories = await setup_accessories_from_file(
        hass, "home_assistant_bridge_fan.json"
    )
    await setup_test_accessories(hass, accessories)

    fan = entity_registry.async_get("fan.living_room_fan")
    assert fan.unique_id == "00:00:00:00:00:00_1256851357_8"

    fan_state = hass.states.get("fan.living_room_fan")
    assert (
        fan_state.attributes[ATTR_SUPPORTED_FEATURES]
        is FanEntityFeature.SET_SPEED
        | FanEntityFeature.DIRECTION
        | FanEntityFeature.OSCILLATE
    )

    fan = entity_registry.async_get("fan.ceiling_fan")
    assert fan.unique_id == "00:00:00:00:00:00_766313939_8"

    fan_state = hass.states.get("fan.ceiling_fan")
    assert fan_state.attributes[ATTR_SUPPORTED_FEATURES] is FanEntityFeature.SET_SPEED

    # Now change the config to add oscillation
    accessories = await setup_accessories_from_file(
        hass, "home_assistant_bridge_basic_fan.json"
    )
    await device_config_changed(hass, accessories)

    fan_state = hass.states.get("fan.living_room_fan")
    assert (
        fan_state.attributes[ATTR_SUPPORTED_FEATURES]
        is FanEntityFeature.SET_SPEED | FanEntityFeature.DIRECTION
    )
    fan_state = hass.states.get("fan.ceiling_fan")
    assert fan_state.attributes[ATTR_SUPPORTED_FEATURES] is FanEntityFeature.SET_SPEED

@bdraco
Copy link
Member Author

bdraco commented Oct 17, 2023

If the CI passes, I'll get this merged, and work on a new PR to see if I can make removes better

@Jc2k
Copy link
Member

Jc2k commented Oct 17, 2023

Sounds good. Ping me if you need a follow up explicit +1; but extra changes all look good. 👍

@bdraco
Copy link
Member Author

bdraco commented Oct 17, 2023

I think I'll need to change aiohomekit's testing a bit for removes as the failure is there.. but it might be more than testing.py.. still digging

@bdraco bdraco marked this pull request as ready for review October 17, 2023 20:53
@bdraco bdraco merged commit d8e541a into dev Oct 17, 2023
23 checks passed
@bdraco bdraco deleted the hkc_accessory_info_once branch October 17, 2023 20:53
@github-actions github-actions bot locked and limited conversation to collaborators Oct 18, 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