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

Use separate data coordinators for AccuWeather observation and forecast #115628

Merged
merged 11 commits into from
Apr 16, 2024

Conversation

bieniu
Copy link
Member

@bieniu bieniu commented Apr 15, 2024

Breaking change

Proposed change

This PR:

  • adds separate data coordinator for observation and forecast
  • moves the data coordinator class to the coordinator module
  • removes options flow because we no longer need the option to enable forecast

The observation (current conditions) data update interval remains unchanged (every 40 minutes). Forecast data is updated every 6 hours. With this configuration, the integration performs 40 requests per day. This gives us 10 requests left for HA restarts or reload config entry (free AccuWeather accounts allow you to make 50 requests per day).

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.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

_LOGGER = logging.getLogger(__name__)


class AccuWeatherDataUpdateCoordinator(TimestampDataUpdateCoordinator[Any]):
Copy link
Member

Choose a reason for hiding this comment

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

What's a TimestampDataUpdateCoordinator?

Copy link
Member Author

@bieniu bieniu Apr 15, 2024

Choose a reason for hiding this comment

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

This is a DataUpdateCoordinator which keeps track of the last successful update:

class TimestampDataUpdateCoordinator(DataUpdateCoordinator[_DataT]):
"""DataUpdateCoordinator which keeps track of the last successful update."""
last_update_success_time: datetime | None = None
@callback
def _async_refresh_finished(self) -> None:
"""Handle when a refresh has finished."""
if self.last_update_success:
self.last_update_success_time = utcnow()

WeaterEntity uses it:

_DailyForecastUpdateCoordinatorT = TypeVar(
"_DailyForecastUpdateCoordinatorT", bound="TimestampDataUpdateCoordinator[Any]"
)
_HourlyForecastUpdateCoordinatorT = TypeVar(
"_HourlyForecastUpdateCoordinatorT", bound="TimestampDataUpdateCoordinator[Any]"
)
_TwiceDailyForecastUpdateCoordinatorT = TypeVar(
"_TwiceDailyForecastUpdateCoordinatorT", bound="TimestampDataUpdateCoordinator[Any]"
)

_LOGGER = logging.getLogger(__name__)


class AccuWeatherDataUpdateCoordinator(TimestampDataUpdateCoordinator[Any]):
Copy link
Member

Choose a reason for hiding this comment

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

Can we make this a generic so we add the return type annotation?

Comment on lines 474 to 479
for description in FORECAST_SENSOR_TYPES:
# Some air quality/allergy sensors are only available for certain
# locations.
if description.key not in forecast_daily_coordinator.data[description.day]:
continue
sensors.append(AccuWeatherSensor(forecast_daily_coordinator, description))
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 this can be a generator expression

class AccuWeatherEntity(
SingleCoordinatorWeatherEntity[AccuWeatherDataUpdateCoordinator]
):
class AccuWeatherEntity(CoordinatorWeatherEntity):
Copy link
Member

Choose a reason for hiding this comment

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

We can retain this coordinator type

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't understand, why do you want to use SingleCoordinatorWeatherEntity?

Copy link
Member

Choose a reason for hiding this comment

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

I assumed the CoordinatorWeatherEntity also takes in a coordinator in the type CoordinatorWeatherEntity[AccuWeatherDataUpdateCoordinator] (but then the new coordinator)

@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 April 15, 2024 19:22
@bieniu bieniu force-pushed the accuweather-split-coordinator branch from ba30bac to a9721b5 Compare April 15, 2024 22:28
@bieniu bieniu marked this pull request as ready for review April 15, 2024 22:40
@home-assistant home-assistant bot requested a review from joostlek April 15, 2024 22:40
@@ -42,12 +49,12 @@
)


async def test_sensor_without_forecast(
async def test_sensor(
Copy link
Member

Choose a reason for hiding this comment

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

Snapshots would be a nice improvment for this test

Copy link
Member Author

Choose a reason for hiding this comment

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

As you wish 😄

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.

Oh I mean that as a separate PR, but this also works

@bieniu bieniu merged commit e7076ac into home-assistant:dev Apr 16, 2024
24 checks passed
@bieniu bieniu deleted the accuweather-split-coordinator branch April 16, 2024 22:00
@bieniu
Copy link
Member Author

bieniu commented Apr 16, 2024

Thanks @joostlek

@github-actions github-actions bot locked and limited conversation to collaborators Apr 17, 2024
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.

None yet

2 participants