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

Adjust HomeWizard to use updated python-homewizard-energy library #135046

Merged
merged 19 commits into from
Jan 10, 2025

Conversation

DCSBL
Copy link
Contributor

@DCSBL DCSBL commented Jan 7, 2025

Proposed change

This is a big one, and I am fully open to get tips to reduce the TL;DR level.

This PR implements the updated python-homewizard-energy, which makes it easier to move to the v2 API (docs). The only goal of this PR is to get the component work with the libary as if nothing has been changed. New features and actual v2 support will be added in future PR's.

The previous PR related to this was #131366, which renamed HomeWizardEnergy to HomeWizardEnergyV1. I then started to implement HomeWizardEnergyV2 but everything in this component went from easy to understand code to many "if v1 then this else that" trees.

Therefore the library has been adjusted to do all this instead HA. After initialisation everything should work the same.

import HomeWizardEnergy

api: HomeWizardEnergy

if HomeWizardEnergy.has_v2_api("ip_address"):
    api = HomeWizardEnergyV2("ip_address", token="<auth_token>")
else:
    api = HomeWizardEnergyV1("ip_address")
    
combined = await api.combined()
print(combined) # Same for both versions, content depends on feature set of the device

Models and naming conventions are moved to v2, as all HomeWizard devices will be moved to v2 eventually.


The update itself:

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:

@DCSBL DCSBL changed the title Adjust HomeWizard to use updated python-homewizard-energy Adjust HomeWizard to use updated python-homewizard-energy library Jan 7, 2025
Comment on lines +16 to +21
"gas_unique_id",
"id",
"serial",
"wifi_ssid",
"unique_meter_id",
"unique_id",
"gas_unique_id",
"unique_meter_id",
"wifi_ssid",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added 'id' and ordered alphabetically

self.config_entry.title,
ex,
)
data = await self.api.combined()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

"Supported checks" are now handled by the library

@@ -6,7 +6,6 @@
from homeassistant.const import PERCENTAGE, EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.color import brightness_to_value, value_to_brightness
Copy link
Contributor Author

Choose a reason for hiding this comment

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

0...255 mapping to percentage is now handled by library

available_fn=lambda data: data.state is not None and not data.state.switch_lock,
is_on_fn=lambda data: data.state.power_on if data.state else None,
set_fn=lambda api, active: api.state_set(power_on=active),
create_fn=lambda x: x.device.supports_state(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Library now handles 'supports_*' checks

tests/components/homewizard/snapshots/test_sensor.ambr Outdated Show resolved Hide resolved
tests/components/homewizard/snapshots/test_sensor.ambr Outdated Show resolved Hide resolved
tests/components/homewizard/snapshots/test_sensor.ambr Outdated Show resolved Hide resolved
tests/components/homewizard/snapshots/test_sensor.ambr Outdated Show resolved Hide resolved

data.system = await self.api.system()

except UnsupportedError as ex:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Supported/unsupported mismatches are handled by the library

@DCSBL DCSBL marked this pull request as ready for review January 7, 2025 23:02
requirements_all.txt Outdated Show resolved Hide resolved
homeassistant/components/homewizard/entity.py Outdated Show resolved Hide resolved
homeassistant/components/homewizard/sensor.py Outdated Show resolved Hide resolved
homeassistant/components/homewizard/sensor.py Outdated Show resolved Hide resolved
tests/components/homewizard/conftest.py Outdated Show resolved Hide resolved
tests/components/homewizard/conftest.py Outdated Show resolved Hide resolved
@home-assistant
Copy link

home-assistant bot commented Jan 8, 2025

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 January 8, 2025 21:45
@DCSBL DCSBL marked this pull request as ready for review January 9, 2025 10:23
@home-assistant home-assistant bot requested a review from joostlek January 9, 2025 10:23
@joostlek joostlek merged commit f31f6d7 into home-assistant:dev Jan 10, 2025
65 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 11, 2025
@DCSBL DCSBL deleted the bumb-homewizard-energy-8.0.0 branch January 16, 2025 15:01
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