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

Update Vesync light state after on / off commands #135958

Draft
wants to merge 8 commits into
base: dev
Choose a base branch
from

Conversation

cdnninja
Copy link
Contributor

@cdnninja cdnninja commented Jan 19, 2025

Proposed change

Commands for on and off were slow to show in the interface. This was do to no refresh occurring after it was sent. This also throws exceptions now if it fails vs nothing.

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 Ruff (ruff format 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.

To help with the load of incoming pull requests:

@home-assistant
Copy link

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

Code owner commands

Code owners of vesync 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 vesync Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

Copy link
Member

@joostlek joostlek left a comment

Choose a reason for hiding this comment

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

Does the device directly show the right state?

@cdnninja
Copy link
Contributor Author

cdnninja commented Jan 19, 2025

In the native app? Yes. Without this code Home Asssitant reverts back for a bit until next scan.

@cdnninja cdnninja mentioned this pull request Feb 6, 2025
19 tasks
@cdnninja cdnninja requested a review from joostlek February 7, 2025 01:24
@MartinHjelmare MartinHjelmare changed the title Vesync Update state after on / off commands Update Vesync state after on / off commands Feb 23, 2025
@cdnninja
Copy link
Contributor Author

Adjusted this to just focus on light.py, added tests as well. This aligns it with humidifer.py. This same work is in my fan refactor so this reduces overlap.

@cdnninja cdnninja changed the title Update Vesync state after on / off commands Update Vesync light state after on / off commands Feb 23, 2025
Comment on lines +61 to +120
@pytest.mark.parametrize(
("api_response", "expectation"),
[(False, pytest.raises(HomeAssistantError)), (True, NoException)],
)
async def test_turn_on(
hass: HomeAssistant,
light_config_entry: MockConfigEntry,
api_response: bool,
expectation,
) -> None:
"""Test turn_on method."""

# turn_on returns False indicating failure in which case light.turn_on
# raises HomeAssistantError.
with (
expectation,
patch(
"pyvesync.vesyncswitch.VeSyncDimmerSwitch.turn_on",
return_value=api_response,
) as method_mock,
):
with patch(
"homeassistant.components.vesync.light.VeSyncBaseLightHA.schedule_update_ha_state"
) as update_mock:
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: ENTITY_LIGHT},
blocking=True,
)

await hass.async_block_till_done()
method_mock.assert_called_once()
update_mock.assert_called_once()


@pytest.mark.parametrize(
("api_response", "expectation"),
[(False, pytest.raises(HomeAssistantError)), (True, NoException)],
)
async def test_turn_off(
hass: HomeAssistant,
light_config_entry: MockConfigEntry,
api_response: bool,
expectation,
) -> None:
"""Test turn_off method."""

# turn_off returns False indicating failure in which case light.turn_off
# raises HomeAssistantError.
with (
expectation,
patch(
"pyvesync.vesyncswitch.VeSyncDimmerSwitch.turn_off",
return_value=api_response,
) as method_mock,
):
with patch(
"homeassistant.components.vesync.light.VeSyncBaseLightHA.schedule_update_ha_state"
) as update_mock:
Copy link
Member

Choose a reason for hiding this comment

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

Same comment as with the switch entity

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Planning to make the change here and then update this one it is merged. #137493

@home-assistant
Copy link

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 February 25, 2025 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants