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

Vesync Display Switch Feature #137493

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

Conversation

cdnninja
Copy link
Contributor

@cdnninja cdnninja commented Feb 6, 2025

Proposed change

Adds the screen switch for both fan and humidifier device. Tested on my physical humidifier and air purifier.

This also consolidates on/off testing so it can run parameter based tests.

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

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

home-assistant bot commented Feb 6, 2025

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.

@cdnninja cdnninja changed the title Vesync Screen Switch Feature Vesync Display Switch Feature Feb 6, 2025
cdnninja added a commit to cdnninja/home-assistant.io that referenced this pull request Feb 6, 2025
@cdnninja cdnninja requested a review from iprak February 13, 2025 16:51
@iprak
Copy link
Contributor

iprak commented Feb 13, 2025

Can we can use display_state property which other types of devices implement as well?

I was originally working on this (dev...iprak:core-fork:auto-stop-display-switches-for-humidifier) when I noticed it to be partially implemented. I got that fixed (webdjoe/pyvesync#293) but it hasn't been released.

@cdnninja
Copy link
Contributor Author

I have updated - I will put to draft until that release is done and merged.

@cdnninja cdnninja marked this pull request as draft February 13, 2025 17:17
@patricknelson
Copy link

patricknelson commented Feb 17, 2025

Looking forward to this one! I’d love to automate switching off that bright display at night.

Edit: Feature request in the community forums: https://community.home-assistant.io/t/vesync-set-the-night-light-on-levoit-air-purifiers/367463

@cdnninja cdnninja marked this pull request as ready for review February 17, 2025 20:33
@cdnninja
Copy link
Contributor Author

cdnninja commented Feb 17, 2025

This is ready for review. Two notes:

  1. It works on my humidifier, when in sleep mode the API rejects it. Only works in Auto or Normal. I updated the turn on off logic to throw an exception when it falls. Otherwise it fails silently. Since we don't have debug support no one would know.
  2. My 131S doesn't work to toggle, it shows state fine. This is an API library issue though. I thought this worked last week. Server is responding with:
2025-02-17 13:31:26,884 - DEBUG - API response: 

  {
  "code": 4001007,
  "msg": "request parameter invalid",
  "traceId": "1739824287128",
  "result": null
}

I will dig into this. I personally don't think this is a deal breaker as other device models work. I have created an issue within the library and will spend some future time on that.

@cdnninja
Copy link
Contributor Author

Solves 131 in webdjoe/pyvesync#305.

Comment on lines 61 to 130
@pytest.mark.parametrize(
("api_response", "expectation"),
[(False, pytest.raises(HomeAssistantError)), (True, NoException)],
)
async def test_turn_on_display(
hass: HomeAssistant,
humidifier_config_entry: MockConfigEntry,
api_response: bool,
expectation,
) -> None:
"""Test turn_on method."""

# turn_on_display returns False indicating failure in which case switch.turn_on_display
# raises HomeAssistantError.
with (
expectation,
patch(
"pyvesync.vesyncfan.VeSyncHumid200300S.turn_on_display",
return_value=api_response,
) as method_mock,
):
with patch(
"homeassistant.components.vesync.switch.VeSyncSwitchEntity.schedule_update_ha_state"
) as update_mock:
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: ENTITY_SWITCH_DISPLAY},
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_display(
hass: HomeAssistant,
humidifier_config_entry: MockConfigEntry,
api_response: bool,
expectation,
) -> None:
"""Test turn_off method."""

# turn_off_display returns False indicating failure in which case switch.turn_off_display
# raises HomeAssistantError.
with (
expectation,
patch(
"pyvesync.vesyncfan.VeSyncHumid200300S.turn_off_display",
return_value=api_response,
) as method_mock,
):
with patch(
"homeassistant.components.vesync.switch.VeSyncSwitchEntity.schedule_update_ha_state"
) as update_mock:
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: ENTITY_SWITCH_DISPLAY},
blocking=True,
)

await hass.async_block_till_done()
method_mock.assert_called_once()
update_mock.assert_called_once()
Copy link
Member

Choose a reason for hiding this comment

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

I think it's more useful and logical to switch the way we parametrize. Have 1 test for all the failing service calls, and have 1 test for all the working service calls, since the only thing you have to change is the function called and the service name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To confirm are you thinking a consolidated test across all platforms so also parametrize a few other values or just within the platform?

@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 12:52
@cdnninja cdnninja marked this pull request as ready for review February 27, 2025 19:03
@home-assistant home-assistant bot requested review from iprak and joostlek February 27, 2025 19:03
@cdnninja
Copy link
Contributor Author

I have updated the tests to focus on parameters. A single fixture now can setup any device. The on_off tests in turn can run on multiple platforms.

This PR is required before my other ones with this issue as it holds the core test structure. Others will now just adjust the dictionary to add more tests.

Let me know if off/on should be two tests or if okay within the same test like it is right now.

@t0mc1k
Copy link

t0mc1k commented Mar 14, 2025

Hello. Light control on CORE200S does not work.

"select/select_option. 'VeSyncAirBypass' object has no attribute 'set_night_light_brightness'

@iprak
Copy link
Contributor

iprak commented Mar 15, 2025

Hello. Light control on CORE200S does not work.

"select/select_option. 'VeSyncAirBypass' object has no attribute 'set_night_light_brightness'

This issue is logged here - #140236

@cdnninja
Copy link
Contributor Author

@joostlek tests have been updated. Please let me know if this matches the goal. If so I will update the tests on the other PRs.

@jcefoli
Copy link

jcefoli commented Mar 25, 2025

Not sure if this is the proper place for this, but with all the recent refactors, I found an issue with an older Voltson ESW01outlet, which is of device_type "wifi-switch-1.3" with firmware version 2.125. Home Assistant can no longer retrieve the power state of this plug, nor toggle it... (My identical looking plugs that are working fine report in as "ESW03-USA" (firmware 1.1.30).

I'm not a python dev, so I have no idea how to provide more info or help, but I did some basic tests with the newest released pyvesync library and was able to toggle that plug state and had no issue controlling all of my plugs directly via a simplified test case.

The only differences I can see are when I dumped both plug objects.

For device_type: ESW03-USA, the cid is a random string.
For device_type: wifi-switch-1.3, the cid is the same as the UUID

Let me know how I can provide more info or what the process is if this needs to be created as a separate issue

@patricknelson
Copy link

@jcefoli I don't think so. See top of the issue:

Adds the screen switch for both fan and humidifier device. Tested on my physical humidifier and air purifier.

In this case, I think it's just specific to humidifers and purifiers (e.g. like the Levoit 131S, see code). I think the "switch" in this case is in reference to the software switch presented by HA, not a physical wifi switch device.

@cdnninja
Copy link
Contributor Author

Not sure if this is the proper place for this, but with all the recent refactors, I found an issue with an older Voltson ESW01outlet, which is of device_type "wifi-switch-1.3" with firmware version 2.125. Home Assistant can no longer retrieve the power state of this plug, nor toggle it... (My identical looking plugs that are working fine report in as "ESW03-USA" (firmware 1.1.30).

I'm not a python dev, so I have no idea how to provide more info or help, but I did some basic tests with the newest released pyvesync library and was able to toggle that plug state and had no issue controlling all of my plugs directly via a simplified test case.

The only differences I can see are when I dumped both plug objects.

For device_type: ESW03-USA, the cid is a random string. For device_type: wifi-switch-1.3, the cid is the same as the UUID

Let me know how I can provide more info or what the process is if this needs to be created as a separate issue

This is a new feature that hasn't been merged yet. Please open an issue under the issues tab and we can take a look.

'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unavailable',
Copy link
Member

Choose a reason for hiding this comment

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

Why is this one unavailable? It might sound like there's a bug here?

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 think you are right. I adjusted this so it fails while we figure this out. I think a test has an issue as I own a 131 and it works. I updated some of the json files as they seemed incorrect without luck. @iprak thoughts?

@home-assistant home-assistant bot marked this pull request as draft March 25, 2025 09:39
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.

6 participants