240 Commits

Author SHA1 Message Date
Joost Lekkerkerker
b4f4b06f29
Enable RUF021 (#135832) 2025-01-17 10:20:45 +01:00
Marc Mueller
5c6e4ad191
Use PEP 695 TypeVar syntax (#133049) 2024-12-12 16:01:57 +01:00
epenet
c7485b94d5
Use breaks_in_ha_version in report_usage (#131137)
* Use breaks_in_ha_version in report_usage

* Revert behavior change
2024-11-23 18:58:24 +01:00
epenet
600f83ddab
Finish migration from report to report_usage (#130412) 2024-11-15 12:35:50 +01:00
epenet
7045b776b6
Use report_usage in helpers (#130365) 2024-11-12 09:25:13 +01:00
G Johansson
d727f8ff50
Clarify event tracking in docstrings for track_state_change/report (#129338)
* Clarify event tracking in docstrings for track_state_change/report

* Fixes

* Update homeassistant/helpers/event.py

* Update homeassistant/helpers/event.py

Co-authored-by: J. Nick Koston <nick@koston.org>

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
2024-10-28 23:05:06 +01:00
Erik Montnemery
cf10549df4
Restore unnecessary assignment of Template.hass in event helper (#125143) 2024-09-03 15:25:35 +02:00
Erik Montnemery
78f7b3340d
Remove unnecessary assignment of Template.hass from event helper (#123777) 2024-08-13 12:09:30 +02:00
J. Nick Koston
1879db9f8f
Revert to using call_soon for event triggers and state changed event trackers (#122735) 2024-07-29 11:45:39 +02:00
Erik Montnemery
8de771de96
Rename async_track_state_reported_event to async_track_state_report_event (#120637)
* Rename async_track_state_reported_event to async_track_state_report_event

* Update tests
2024-06-27 13:45:15 +02:00
Erik Montnemery
a165064e9d
Improve typing of state event helpers (#120639) 2024-06-27 06:08:19 -05:00
J. Nick Koston
a5a631148e
Add async_track_state_reported_event to fix integration performance regression (#120622)
split from https://github.com/home-assistant/core/pull/120621
2024-06-26 23:04:27 -04:00
J. Nick Koston
a8fba691ee
Add types to event tracker data (#118010)
* Add types to event tracker data

* fixes

* do not test event internals in other tests

* fixes

* Update homeassistant/helpers/event.py

* cleanup

* cleanup
2024-05-24 10:09:39 -04:00
Marc Mueller
dd22ee3dac
Improve annotation styling (#118032) 2024-05-24 15:05:53 +02:00
J. Nick Koston
b7a18e9a8f
Avoid calling split_entity_id in event add/remove filters (#118015) 2024-05-24 10:01:33 +02:00
J. Nick Koston
6c6a5f496a
Simplify async_track_time_interval implementation (#117956) 2024-05-22 21:56:49 -10:00
Peter
eb76386c68
Prevent time pattern reschedule if cancelled during job execution (#117879)
Co-authored-by: J. Nick Koston <nick@koston.org>
2024-05-22 10:36:03 -10:00
Marc Mueller
25d1ca747b
Use PEP 695 for decorator typing (3) (#117640) 2024-05-17 16:27:32 +02:00
Marc Mueller
87bb7ced79
Use PEP 695 for simple type aliases (#117633) 2024-05-17 14:42:21 +02:00
Marc Mueller
2db64c7e6d
Use HassKey for helpers (1) (#117012) 2024-05-07 11:25:16 -05:00
Sid
2cc916db6d
Replace pylint broad-except with Ruff BLE001 (#116250) 2024-05-07 14:00:27 +02:00
J. Nick Koston
0380116ef6
Improve logging of _TrackPointUTCTime objects (#116711) 2024-05-04 17:35:44 -05:00
J. Nick Koston
b520efb87a
Small speed up to async_track_event (#116083) 2024-04-24 09:56:59 +02:00
J. Nick Koston
d48bd9b016
Deprecate async_track_state_change in favor of async_track_state_change_event (#115558) 2024-04-18 10:45:14 -05:00
J. Nick Koston
8da7de1fea
Remove attr usage in event helper (#115554) 2024-04-13 19:46:23 -05:00
J. Nick Koston
11af7d91ff
Optimize _async_track_event for the single key common case (#115242) 2024-04-09 07:04:50 -10:00
J. Nick Koston
ca5ed274cb
Deprecate calling async_listen and async_listen_once with run_immediately (#115169) 2024-04-08 10:07:54 -10:00
J. Nick Koston
9ef28f83ea
Switch async_track_state_change to use run_immediately (#115164) 2024-04-07 16:38:25 -10:00
Marc Mueller
a0e6fd6ec5
Add improved typing for event fire and listen methods (#114906)
* Add EventType implementation

* Update integrations for EventType

* Change state_changed to EventType

* Fix tests

* Remove runtime impact

* Add tests

* Move to stub file

* Apply pre-commit to stub files

* Fix ruff PYI checks

---------

Co-authored-by: J. Nick Koston <nick@koston.org>
2024-04-07 13:28:24 -10:00
J. Nick Koston
fb98a6f026
Make run_immediately the default for core EventBus listeners (#113752)
* DNM: Make run_immediately the default for listeners

This is a test to see how much progress we have made twords this goal

https://github.com/home-assistant/core/pull/113727#issuecomment-2004587947

* fix shutdown

* Revert "fix shutdown"

This reverts commit a8969d7db9fed10040cb8b7b25459dc9d812eb9c.

* set false since it break utility meter tests

* one more

* fix rfxtrx test

* test needs to be explict now

* fix matrix

* fail sooner
2024-04-05 22:14:20 -04:00
J. Nick Koston
d321906342
Always run keyed event trackers immediately (#114709) 2024-04-04 15:30:01 -10:00
Marc Mueller
56ef9500f7
Use EventStateChangedData type when firing state changed event (#114740) 2024-04-04 09:27:44 -10:00
J. Nick Koston
b574220247
Refactor rate limit helper to track time in seconds (#113898)
* Refactor rate limit helper to track time in seconds

Currently we created datetime and timedelta objects to enforce the
rate limit. When the rate limit was being hit hard, this got expensive.

We now use floats everywhere instead as they are much cheaper which
is important when we are running up against a rate limit, which is
by definition a hot path

The rate limit helper is currently only used for templates and
we do not have any code in the code base that directly passes
in a rate limit so the impact to custom components is expected
to be negligible if any

* misesd two
2024-03-20 19:49:37 -04:00
Erik Montnemery
d31124d5d4
Avoid creating unneeded Context and Event objects when firing events (#113798)
* Avoid creating unneeded Context and Event objects when firing events

* Add test

---------

Co-authored-by: J. Nick Koston <nick@koston.org>
2024-03-20 09:40:06 +01:00
J. Nick Koston
620433a79d
Run coroutines as eager tasks in async_run_hass_job (#111683)
* Run coroutines as eager tasks in async_run_hass_job

Note that this does not change async_add_hass_job

Do not merge this. For test run only

* Phase out periodic tasks

* false by default or some tests will block forever, will need to fix each one manually

* kwarg works

* kwarg works

* kwarg works

* fixes

* fix more tests

* fix more tests

* fix lifx

* opensky

* pvpc_hourly_pricing

* adjust more

* adjust more

* smarttub

* adjust more

* adjust more

* adjust more

* adjust more

* adjust

* no eager executor

* zha

* qnap_qsw

* fix more

* fix fix

* docs

* its a wrapper now

* add more coverage

* coverage

* cover all combos

* more fixes

* more fixes

* more fixes

* remaining issues are legit bugs in tests

* make tplink test more predictable

* more fixes

* feedreader

* grind out some more

* make test race safe

* limit first scope to triggers

* one more

* Start tasks eagerly in for async_at_start(ed)

A few of these can avoid being scheduled on the loop
during startup

* fix cloud

* Revert "fix cloud"

This reverts commit 5eb3ce695da788bcae649f82c9527c0f9307139c.

* fix test to do what start does

* flip flag

* flip flag

* Fix here_travel_time creating many refresh requests at startup

- Each entity would try to refresh the coordinator which
  created many tasks. Move the refresh to a single
  async_at_started

- The tests fired the EVENT_HOMEASSISTANT_START event
  but the code used async_at_started which only worked
  because the tests did not set CoreState to not_running

* fix azure

* remove kw

* remove kw

* rip

* cover

* more rips

* more rips

* more rips
2024-03-11 20:05:08 -04:00
J. Nick Koston
23ebd80285
Schedule coroutines with eager_task from async_track_state_change_event (#112807) 2024-03-09 11:24:54 -10:00
J. Nick Koston
65358c129a
Replace periodic tasks with background tasks (#112726)
* Phase out periodic tasks

* false by default or some tests will block forever, will need to fix each one manually

* kwarg works

* kwarg works

* kwarg works

* fixes

* fix more tests

* fix more tests

* fix lifx

* opensky

* pvpc_hourly_pricing

* adjust more

* adjust more

* smarttub

* adjust more

* adjust more

* adjust more

* adjust more

* adjust

* no eager executor

* zha

* qnap_qsw

* fix more

* fix fix

* docs

* its a wrapper now

* add more coverage

* coverage

* cover all combos

* more fixes

* more fixes

* more fixes

* remaining issues are legit bugs in tests

* make tplink test more predictable

* more fixes

* feedreader

* grind out some more

* make test race safe

* one more
2024-03-08 21:45:10 -05:00
Marc Mueller
de886d8c49
Update EventBus listen type signatures (#112760) 2024-03-08 21:57:23 +01:00
Marc Mueller
b026b5d589
Replace EventType with Event [helpers] (#112743) 2024-03-08 19:41:50 +01:00
Marc Mueller
19ab3d6daf
Add empty line after module docstring [helpers + other] (#112707) 2024-03-08 10:36:11 -05:00
J. Nick Koston
a6b17dbe68
Schedule polling as periodic tasks (#112640)
* Schedule periodic coordinator updates as background tasks.

Currently, the coordinator's periodic refreshes delay startup because they are not scheduled as background tasks. We will wait if the startup takes long enough for the first planned refresh. Another coordinator's scheduled refresh will be fired on busy systems, further delaying the startup. This chain of events results in the startup taking a long time and hitting the safety timeout because too many coordinators are refreshing.

This case can also happen with scheduled entity refreshes, but it's less common. A future PR will address that case.

* periodic_tasks

* periodic_tasks

* periodic_tasks

* merge

* merge

* merge

* merge

* merge

* fix test that call the sync api from async

* one more place

* cannot chain

* async_run_periodic_hass_job

* sun and pattern time changes from automations also block startup

* Revert "sun and pattern time changes from automations also block startup"

This reverts commit 6de2defa0586165c2918ce006cf57aa3acaae730.

* make sure polling is cancelled when config entry is unloaded

* Revert "Revert "sun and pattern time changes from automations also block startup""

This reverts commit e8f12aad5579a1e5e3930957a46ea6f3dd9beecf.

* remove DisabledError from homewizard test as it relies on a race

* fix race

* direct coverage
2024-03-07 23:32:26 -05:00
J. Nick Koston
a7b4cd3512
Pass job type to event listeners when creating entities (#112551) 2024-03-06 16:10:07 -10:00
J. Nick Koston
b5a2df1951
Refactor keyed event trackers to reduce future refactoring risk (#111150)
* Refactor keyed event trackers to avoid refactoring risk

Follow to https://github.com/home-assistant/core/pull/110978#discussion_r1496771106

I had to do some type ignores because of the EventType vs Event
which is hopefully not going to be needed after the next mypy

* delete constants only used one in other const

* no field

* fixes

* less refactoring later

* less refactoring later

* keep const
2024-02-23 13:55:02 -05:00
J. Nick Koston
9c145b5faa
Fix race in removing entities from the registry (#110978) 2024-02-20 20:48:31 -06:00
J. Nick Koston
664285b9d4
Small performance improvement in tracking template results (#110622)
- Avoid inner function creation each refresh
- remove extra unneeded checks from ratelimit
2024-02-17 18:08:24 +01:00
J. Nick Koston
f73431ac06
Switch utc_to_timestamp to .timestamp() where possible (#109729)
* Switch utc_to_timestamp to .timestamp()

.timestamp() is faster now in newer cpython

```
from homeassistant.util.dt import utc_to_timestamp, utcnow
import timeit
now = utcnow()
print(timeit.timeit('utc_to_timestamp(now)',globals={"now":now,"utc_to_timestamp":utc_to_timestamp}))
print(timeit.timeit('now.timestamp()',globals={"now":now}))
```

utc_to_timestamp = 0.18721245788037777
timestamp = 0.11421508435159922

* compat

* revert

* revert

* revert

* revert

* revert
2024-02-05 23:04:52 +01:00
J. Nick Koston
6f28d79651
Copy callbacks instead of slice for event dispatch (#109711)
We established copy is faster in
https://github.com/home-assistant/core/pull/108428#discussion_r1466932262
2024-02-05 17:07:21 +01:00
J. Nick Koston
99b6c7d25f
Refactor async_track_utc_time_change to avoid using nonlocal (#108007) 2024-01-14 10:57:54 +01:00
J. Nick Koston
659ee51914
Refactor event time trackers to avoid using nonlocal (#107997) 2024-01-13 17:17:55 -10:00
Ville Skyttä
24b1e01d71
Update Ruff to 0.1.8, avoid linter/formatter conflicts (#106080)
* Disable Ruff rules that may conflict with the formatter

* Upgrade Ruff to 0.1.8

- https://github.com/astral-sh/ruff/releases/tag/v0.1.7
- https://github.com/astral-sh/ruff/releases/tag/v0.1.8

* Format with Ruff 0.1.8
2023-12-20 23:55:09 +01:00