8000 Use RestoreSensor in Ecowitt by Thomas55555 · Pull Request #134442 · home-assistant/core · GitHub
[go: up one dir, main page]

Skip to content

Use RestoreSensor in Ecowitt #134442

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

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

Conversation

Thomas55555
Copy link
Contributor

Breaking change

Proposed change

Use a RestoreSensor in EcoWitt. This assures that there is sensor data available until the first data from the webhook arrives.
Entities are built out of the entity registry. To manage this, we need to store some data in the config entry. A migration of the config entry in in my opinion not required.

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
home-assistant bot commented Jan 2, 2025

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

Code owner commands

Code owners of ecowitt 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 ecowitt 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.

@Thomas55555 Thomas55555 marked this pull request as ready for review January 2, 2025 11:53
@Thomas55555 Thomas55555 requested a review from pvizeli as a code owner January 2, 2025 11:53
Copy link
Member
@edenhaus edenhaus left a comment

Choose a reason for hiding this comment

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

Can you please add tests for your changes

@Thomas55555
Copy link
Contributor Author
Thomas55555 commented Jan 4, 2025

Can you please add tests for your changes

There are no tests for this component, yet. Except of the config flow. So that would mean I have to setup all the basic tests?

@edenhaus
Copy link
Member

There are no tests for this component, yet. Except of the config flow. So that would mean I have to setup all the basic tests?

Nope, then it's fine. But tests are always welcome :)

@home-assistant home-assistant bot marked this pull request as draft January 20, 2025 07:47
@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.

Thomas55555 and others added 3 commits January 20, 2025 20:49
@Thomas55555 Thomas55555 marked this pull request as ready for review February 7, 2025 11:29
@home-assistant home-assistant bot requested a review from edenhaus February 7, 2025 11:29
@@ -33,6 +33,15 @@ async def handle_webhook(
hass, DOMAIN, entry.title, entry.data[CONF_WEBHOOK_ID], handle_webhook
)

def async_update_device_config(sensor: EcoWittSensor) -> None:
"""Update device config."""
hass.config_entries.async_update_entry(
Copy link
Member

Choose a reason for hiding this comment

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

We should not update on any sensor the config entry if the station is the same

"""Update device config."""
hass.config_entries.async_update_entry(
entry,
data={**entry.data, "station": sensor.station},
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to have multiple devices on the same webhook? If yes this solution will not work

self.restored_last_reported = sensor_last_state.last_reported

if (sensor_data := await self.async_get_last_sensor_data()) is not None:
self._attr_native_value = sensor_data.native_value
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self._attr_native_value = sensor_data.native_value

Will not be used as you override the base native_value property

Comment on lines +314 to +324
station = EcoWittStation(
**entry.data["station"],
)
sensor = ecowitt.sensors[f"{config_entry_unique_id}.{sensor_key_name}"] = (
EcoWittSensor(
name=config_entry.original_name,
key=config_entry_key,
stype=sensor_key_name, # type: ignore[arg-type]
station=station,
)
)
Copy link
Member

Choose a reason for hiding this comment

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

This should be done in the library to reduce errors, such as when you forget to add the sensor to the EcoWittListener.
In your PR the sensor will be created EcoWittSensor.
Creating them here is hacky


if (sensor_data := await self.async_get_last_sensor_data()) is F438 not None:
self._attr_native_value = sensor_data.native_value
self._attr_available = True
Copy link
Member

Choose a reason for hiding this comment

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

Same for this one

Suggested change
self._attr_available = True

async def check_availability(self) -> None:
"""Return whether the state is based on actual reading from device."""
if not self.restored_last_reported:
self._attr_available = False
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self._attr_available = False

await asyncio.sleep(MAX_AGE - age.seconds)
new_age = dt_util.utcnow() - self.restored_last_reported
if new_age.seconds >= MAX_AGE:
self._attr_available = False
Copy link
Member

Choose a reason for hiding this comment

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

This will not work as you have overwritten the available property

@home-assistant home-assistant bot marked this pull request as draft April 2, 2025 14:22
Copy link
github-actions bot commented Jun 1, 2025

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

@github-actions github-actions bot added the stale label Jun 1, 2025
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.

3 participants
0