Commit Graph

516 Commits (6d22dd073c9ceb0abf92e66fbe9236eafe7e8afa)

Author SHA1 Message Date
J. Nick Koston c4d719a7cf
Use eager_start for background service call tasks (#111620) 2024-02-27 12:06:32 -10:00
J. Nick Koston 67e356904b
Add support for eager tasks (#111425)
* Add support for eager tasks

python 3.12 supports eager tasks

reading:
https://docs.python.org/3/library/asyncio-task.html#eager-task-factory
https://github.com/python/cpython/issues/97696

There are lots of places were we are unlikely to suspend, but we might
suspend so creating a task makes sense

* reduce

* revert entity

* revert

* coverage

* coverage

* coverage

* coverage

* fix test
2024-02-26 11:36:46 -05:00
Franck Nijhof 1c55ba0cb2
Use covariant for fire event data type (#110843) 2024-02-18 13:21:55 +01:00
J. Nick Koston b5afdf34f4
Improve ability to debug one time listeners blocking the event loop (#110064) 2024-02-09 08:44:14 +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
Marc Mueller cd0ee98dba
Use builtin TimeoutError [core + helpers] (#109684) 2024-02-05 12:09:54 +01:00
J. Nick Koston 7e3001f843
Remove default values from calls to async_fire (#109613)
There were a few places were we passing unnecessary default values. Since
this code tends to get copied, remove them
2024-02-04 14:49:42 -05:00
J. Nick Koston e35c7fde89
Avoid many string lowers in the state machine (#109607) 2024-02-04 17:35:35 +01:00
J. Nick Koston 7c86ab14c3
Refactor service enumeration methods to better match existing use cases (#108671) 2024-01-22 14:21:17 -10:00
J. Nick Koston a3f9fc45e3
Refactor async_listen_once to remove nonlocal (#108627) 2024-01-21 22:52:47 -05:00
J. Nick Koston 52b5d2e370
Avoid json encoder default fallback when serializing config (#108360)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
2024-01-19 19:22:17 -10:00
J. Nick Koston c399cab427
Small speed up to checking core state (#107845) 2024-01-18 08:41:32 -10:00
J. Nick Koston 60ab360fe7
Avoid bytes to string to bytes conversion in websocket api (#108139) 2024-01-16 21:37:34 +01:00
J. Nick Koston 3d595fff13
Avoid duplicate timestamp conversions for websocket api and recorder (#108144)
* Avoid duplicate timestamp conversions for websocket api and recorder

We convert the time from datetime to timestamps one per
open websocket connection and the recorder for every
state update. Only do the conversion once since its
~30% of the cost of building the state diff

* more

* two more

* two more in live history
2024-01-16 14:05:01 +01:00
J. Nick Koston 1cdfb06d77
Add cached_property to State.name (#108011) 2024-01-14 11:50:40 +01:00
J. Nick Koston 4b7a313ece
Use identity checks for CoreState (#107846)
Some of the checks used ==, and some used is. Switch
everything to is as its faster
2024-01-12 10:21:26 +01:00
J. Nick Koston d04e2d56da
Add support for JSON fragments (#107213) 2024-01-07 17:36:31 -10:00
J. Nick Koston 44018a4183
Use faster identity checks for SupportsResponse Enum (#107351) 2024-01-06 10:12:31 +01:00
Erik Montnemery c805ea7b4f
Include deprecated constants in wildcard imports (#107114) 2024-01-05 11:46:45 +01:00
Erik Montnemery 9eefd95e91
Deduplicate handling of duplicated constants (#107074)
* Deduplicate handling of duplicated constants

* Use DeprecatedConstant + DeprecatedConstantEnum

* Fixup

* Remove test cases with unnamed tuples
2024-01-04 13:25:09 +01:00
J. Nick Koston 1cbacd13aa
Use identity checks for HassJobType (#106860) 2024-01-02 12:33:02 +01:00
J. Nick Koston 9e3869ae1c
Avoid recreating ReadOnly dicts when attributes do not change (#106687) 2023-12-29 17:10:40 -10:00
Erik Montnemery a4e9a053c7
Fix missing await when running shutdown jobs (#106632) 2023-12-29 10:04:16 +01:00
Richard Kroegel bd4177edc8
Add supports_response to ServiceRegistry.register() (#106164)
Co-authored-by: rikroe <rikroe@users.noreply.github.com>
2023-12-27 13:01:20 +01:00
Robert Resch fbcb31b103
Deprecate deprecated unit of measurement constants (#106455) 2023-12-27 09:04:25 +01:00
Robert Resch b08268da31
Deprecate deprecated core constants (#106456) 2023-12-27 08:42:57 +01:00
Tudor Sandu 636e38f4b3
Trigger Home Assistant shutdown automations right before the stop event instead of during it (#91165)
Co-authored-by: Erik <erik@montnemery.com>
2023-12-05 11:24:41 -10:00
J. Nick Koston 1522118453
Remove aiohttp cancel on disconnect workaround (#104175) 2023-11-29 11:20:36 -07:00
Jan Bouwhuis 487ff8cd7f
Rename ex to exc as name for exceptions (#104479) 2023-11-25 08:30:18 +01:00
J. Nick Koston cc31d77205
Use ulid_now instead of ulid_at_timestamp if no timestamp is passed (#104226)
* Use ulid_now instead of ulid_at_timestamp if no timestamp is passed

ulid_now is slightly faster than ulid_at_timestamp

* tweak usage
2023-11-19 22:27:24 -05:00
tronikos 9af5e838c6
Add type annotation for service functions with response (#102813)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2023-11-09 12:31:12 +01:00
J. Nick Koston dca72c598e
Small speed up to async_listen (#103307)
Avoid constructing an inner function each time we call async_listen
and use a partial which holds a reference to the function body with new
args instead of making another full function
2023-11-03 20:58:03 +01:00
Kevin Stillhammer 06c9719cd6
Support multiple responses for service calls (#96370)
* add supports_response to platform entity services

* support multiple entities in entity_service_call

* support legacy response format for service calls

* revert changes to script/shell_command

* add back test for multiple responses for legacy service

* remove SupportsResponse.ONLY_LEGACY

* Apply suggestion

Co-authored-by: Allen Porter <allen.porter@gmail.com>

* test for entity_id remove None

* revert Apply suggestion

* return EntityServiceResponse from _handle_entity_call

* Use asyncio.gather

* EntityServiceResponse not Optional

* styling

---------

Co-authored-by: Allen Porter <allen.porter@gmail.com>
2023-11-02 18:37:35 -07:00
J. Nick Koston b3743937de
Avoid looking up the callable type for HassJob when we already know it (#102962)
* Avoid looking up the callable type for HassJob when we already know it

When we connect the frontend we call async_listen with run_immediately MANY
times when we already know the job type (it will always be a callback). This
reduces the latency to get the frontend going

* missing coverage
2023-10-30 12:45:22 +01:00
J. Nick Koston 009dc91b97
Fix inner callback decorators with partials (#102873) 2023-10-28 08:38:42 -05:00
Erik Montnemery 97cc05d0b4
Make it possible to restart core in safe mode (#102606) 2023-10-24 14:47:58 +02:00
Erik Montnemery c481fdb7d0
Rename safe mode to recovery mode (#102580) 2023-10-23 20:33:08 +02:00
Jan Bouwhuis 31bd500222
Make get_channel available as generic helper (#101721)
* Make get_channel available as generic helper

* Follow up comment
2023-10-10 14:02:32 +02:00
Erik Montnemery fbcc5318c5
Move attributes to be excluded from recording to entity classes (#100239)
Co-authored-by: J. Nick Koston <nick@koston.org>
2023-09-20 18:09:12 +02:00
J. Nick Koston 547f32818c
Make core States use cached_property (#100312)
Need to validate this is worth removing __slots__
2023-09-13 20:33:25 -04:00
Erik Montnemery 6057fe5926
Replace StateMachine._domain_index with a UserDict (#100270)
* Replace StateMachine._domain_index with a UserDict

* Access the UserDict's backing dict directly

* Optimize
2023-09-13 18:05:17 +02:00
J. Nick Koston ad4619c038
Speed up serializing event messages (#100017) 2023-09-10 08:25:13 -05:00
Marc Mueller 5d54660802
Fix asyncio.wait typing (#99726) 2023-09-06 16:53:41 +02:00
Erik Montnemery 7643820e59
Add loader.async_get_loaded_integration (#99440)
* Add loader.async_get_loaded_integration

* Decorate async_get_loaded_integration with @callback
2023-09-04 14:12:33 +02:00
Erik Montnemery 7c595ee2da
Validate state in template helper preview (#99455)
* Validate state in template helper preview

* Deduplicate state validation
2023-09-04 14:10:43 +02:00
J. Nick Koston 186e796e25
Speed up fetching states by domain (#99467) 2023-09-03 09:30:39 -05:00
J. Nick Koston 1ab2e900f9
Improve lingering timer checks (#99472) 2023-09-02 12:43:27 -05:00
Ville Skyttä 6399d74c15
Remove unnnecessary pylint configs from core (#98704) 2023-08-22 23:12:12 +02:00
Erik Montnemery 31f5932fe4
Log events with no listeners (#98540)
* Log events with no listeners

* Unconditionally create the Event object

* Reformat code
2023-08-16 20:14:49 +02:00
Erik Montnemery 91faa53843
Don't allow hass.config.config_dir to be None (#98442) 2023-08-16 13:00:14 +02:00
Marc Mueller a9ade1f84d
Use asyncio.timeout [core] (#98447) 2023-08-15 08:36:05 -05:00
Franck Nijhof 868a5f377f
Ruff: isort don't split imports based on trailing comma (#98162) 2023-08-10 14:27:03 +02:00
Franck Nijhof 30058297cf
Migrate backported StrEnum to built-in StrEnum (#97101) 2023-07-23 23:19:24 +02:00
Marc Mueller 77f38e33e5
Import names from typing instead of typing_extensions [3.11] (#97065) 2023-07-22 17:03:44 -05:00
J. Nick Koston 99def97ed9
Add cancel messages to core task cancelation (#96972) 2023-07-20 18:03:36 -05:00
J. Nick Koston 08af42b00e
Fix mixed case service schema registration (#96448) 2023-07-12 14:39:51 -10:00
Jan Bouwhuis 18ee9f4725
Refactor async_get_hass to rely on threading.local instead of a ContextVar (#96005)
* Test for async_get_hass

* Add Fix
2023-07-07 14:52:38 -04:00
J. Nick Koston dc5ee71d7a
Add slots to core EventBus (#95856) 2023-07-05 11:47:24 -05:00
J. Nick Koston ea57f78392
Add slots to the service registry (#95857) 2023-07-05 15:59:36 +02:00
J. Nick Koston b2e708834f
Add slots to the StateMachine class (#95849) 2023-07-05 14:00:37 +02:00
J. Nick Koston 91f334ca59
Small cleanups to service calls (#95873) 2023-07-05 09:25:38 +02:00
J. Nick Koston 0ff3836083
Use a normal tuple for the EventBus jobs (#95731) 2023-07-02 16:35:57 -05:00
Allen Porter 528c206094
Add script specific error messages for `response_variable` (#95188) 2023-06-24 21:34:57 -07:00
J. Nick Koston 5059cee53f
Reduce overhead to fire events (#95163) 2023-06-24 14:39:13 -05:00
Allen Porter 30e8f806c1
Improve service response data APIs (#94819)
* Improve service response data APIs

Make the API naming more consistent, and require registration that a
service supports response data so that we can better integrate with
the UI and avoid user confusion with better error messages.

* Improve test coverage

* Add an enum for registering response values

* Assign enum values

* Convert SupportsResponse to StrEnum

* Update service call test docstrings

* Add tiny missing full stop in comment

---------

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
2023-06-20 06:24:31 -07:00
Allen Porter 84c66b3cad
Add support for services to return data (#94401)
* Add support for service calls with resopnse data.

Update the service calls to allow returning responses with data,
with an initial use case supporting basic service calls usable
within script.

* Revert enttiy platform/component changes

* Remove unnecessary comma diff

* Revert additional unnecessary changes

* Simplify service call

* Simplify and fix typing and revert whitespace

* Clarify typing intent

* Revert more entity service calls

* Revert additional entity service changes

* Set blocking=True for group notify service call

* Revert unnecessary changes

* Reverting more whitespace changes

* Revert more service changes

* Add test coverage for None return case

* Add parameter to service calls indicating return values were requested

* Update tests/test_core.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>

* Add additional service call tests

* Update test comment

---------

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
2023-06-16 12:43:35 -04:00
Allen Porter 12129e9d21
Update service call return values and error handling (#94657)
* Update return signature of service calls

* Add timeout error handling in websocket api for service calls

* Update recorder tests to remove assertion on service call

* Remove timeout behavior and update callers that depend on it today

* Fix tests

* Add missing else

* await coro directly

* Fix more tests

* Update the intent task to use wait instead of timeout

* Remove script service call limits and limit constants

* Update tests that depend on service call limits

* Use wait instead of wait_for and add test

* Update homeassistant/helpers/intent.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

---------

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2023-06-16 07:01:40 -07:00
J. Nick Koston c721cbd10c
Collapse valid state check when creating a state (#93655)
Inline valid state check when creating a state

This was added in #9696 and only used in one place so
it does not need to be another function
2023-05-27 19:53:08 -04:00
J. Nick Koston e09e4f14d6
Speed up setting state (#93472) 2023-05-25 07:34:07 -05:00
J. Nick Koston 780f9bcdc0
Fix shutdown being delayed for cancelling tasks (#93287) 2023-05-21 14:17:34 +02:00
J. Nick Koston 32481065a8
Increase size of valid entity id cache (#93125) 2023-05-17 13:06:52 +02:00
J. Nick Koston bf16f6b104
Remove as_compressed_state cache (#93169)
* Remove as_compressed_state cache

All calls to as_compressed_state are now covered by a higher level
JSON cache so there is no need to store these in memory anymore

* Remove as_compressed_state cache

All calls to as_compressed_state are now covered by a higher level
JSON cache so there is no need to store these in memory anymore
2023-05-16 12:39:16 -05:00
J. Nick Koston 99265a983a
Speed up reconnects by caching state serialize (#93050) 2023-05-16 02:33:12 -05:00
J. Nick Koston 24284fe379
Speed up validating domains in templates (#92975)
This path gets called quite a bit since most templates
access the state via states.DOMAIN...
2023-05-12 14:57:51 +02:00
J. Nick Koston 07d1a16efd
Add as_dict cache to Context and Event (#92162) 2023-04-28 21:15:39 +02:00
J. Nick Koston 237faf62ac
Add a name to the background service call tasks (#91252) 2023-04-11 19:23:25 -10:00
epenet 9705607db4
Fix lingering timer in cloud (#90822)
* Fix lingering timer in cloud

* Rename variable

* Improve

* Improve again

* Adjust

* Adjust

* Add property to HassJob instead

* Adjust

* Rename

* Adjust

* Adjust

* Make it read-only

* Add specific test
2023-04-07 11:38:17 +02:00
J. Nick Koston 5eb0c35a97
Add names to common helper tasks (#90803) 2023-04-05 08:41:15 +02:00
J. Nick Koston 4c21caa917
Fix filesize doing blocking I/O in the event loop (#90479)
Fix filesize doing I/O in the event loop
2023-03-29 17:26:28 -04:00
Jesse Moody d7de23fa65
Adjust eventloop -> event loop spelling (#89931)
eventloop -> event loop spelling
2023-03-19 21:53:21 +01:00
J. Nick Koston 5dbab21f9a
Fix missing f-string in filterable_job (#89340)
* Fix missing f-string in filterable_job

* remove bad test
2023-03-08 15:53:48 -05:00
J. Nick Koston 099f16f6b8
Fix missing f-string in async_listen (#89336) 2023-03-07 20:19:08 -05:00
Erik Montnemery aa2267d68e
Rename hass context variable (#89302) 2023-03-07 23:21:47 +01:00
J. Nick Koston 11681f3f31
Pass a helpful name when creating common asyncio tasks in core (#89171) 2023-03-05 12:46:02 +01:00
J. Nick Koston 8f6cfc25c0
Use ulid-transform for constructing ulids (#88939)
* Use ulid-transform for constructing ulids

A future PR will use the new library to reduce the storage
overhead of ulids in the database

* tweak

* tweak

* bump
2023-02-28 21:09:47 -05:00
J. Nick Koston b5223e1196
Restore previous behavior of only waiting for new tasks at shutdown (#88740)
* Restore previous behavior of only waiting for new tasks at shutdown

* cleanup

* do a swap instead

* await canceled tasks

* await canceled tasks

* fix

* not needed since we no longer clear

* log it

* reword

* wait for airvisual

* tests
2023-02-26 22:36:18 -05:00
J. Nick Koston 7b61d3763b
Log futures that are blocking shutdown stages (#88736) 2023-02-25 12:01:30 +01:00
Erik Montnemery 594b7b7e2c
Add clarifying comment about unit of elevation (#88489)
* Add clarifying comment about unit of elevation

* Make it a docstring
2023-02-22 09:06:43 -05:00
J. Nick Koston 5bc0636905
Replace fire_coroutine_threadsafe with asyncio.run_coroutine_threadsafe (#88572)
fire_coroutine_threadsafe did not hold a reference to the asyncio
task which meant the task had the risk of being prematurely
garbage collected
2023-02-21 21:16:18 -05:00
modrisb 9fd35d53e7
Change core return code processing (#88326)
Co-authored-by: J. Nick Koston <nick@koston.org>
2023-02-17 20:56:02 -05:00
Paulus Schoutsen 3a32d2bdcb
Add background tasks to config entries (#88335)
* Use a set for config entries task tracking

* Allow adding background tasks to config entries

* Add tests for config entry add tasks

* Update docstrings on core create task

* Migrate roon and august

* Use in more places

* Guard for None
2023-02-17 13:50:05 -05:00
Paulus Schoutsen 6cab27f378
Add support for background tasks in HA (#88265)
* Add support for background tasks

* make name mandatory for background tasks

* Update docstring

* async_create_background_task

* Grammar
2023-02-16 20:39:29 -05:00
Paulus Schoutsen d54f59478f
Keep task references while running (#87970)
* Keep task references while running

* Update pilight tests pointing at correct logger call

* Fix graphite tests

* Fix profiler tests

* More graphite test fixes

* Remove extra sleep

* Fix tests

* Shutdown background tasks as part of stage 1

* Remove unnecessary sleep in test

* Remove unused method on mock hass

* Skip on cancelled too

* Remove background tasks

* Test trigger variables without actually sleeping

* Fix graphite

* One more graphite grrrrrrr
2023-02-13 23:16:59 -05:00
J. Nick Koston e41af8928b
Restore original aiohttp cancelation behavior (#88046) 2023-02-13 22:00:36 -06:00
Marc Mueller 342b406dc0
Add Self typing (1) [mypy 1.0] (#87598) 2023-02-06 22:29:47 -06:00
Franck Nijhof ca1a12898c
Enable Ruff D212 (#87347) 2023-02-03 23:08:48 +01:00
J. Nick Koston c612a92cfb
Use python defaults for comparing State, LazyState, and Event objects (#86856)
* Speed up comparing State and Event objects

Use default python implementation for State and Event __hash__ and __eq__

The default implementation compared based on the id() of the object
which is effectively what we want here anyways. These overrides are
left over from the days when these used to be attrs objects

By avoiding implementing these ourselves all of the equality checks
can happen in native code

* tweak

* adjust tests

* write out some more

* fix test to not compare objects

* more test fixes

* more test fixes

* correct stats tests

* fix more tests

* fix more tests

* update sensor recorder tests
2023-01-29 13:31:43 -05:00
Marc Mueller 6397138589
Update Optional typing (1) [Py310] (#86417)
Co-authored-by: Franck Nijhof <git@frenck.dev>
2023-01-23 09:56:10 +01:00
Marc Mueller 45b4b0e990
Import `ParamSpec` from typing [Py310] (#86413)
* Import ParamSpec from typing [Py310]

* Update additional imports
2023-01-23 07:28:43 +01:00
Franck Nijhof 79b52a2b41
Stricter pylint message control (#86154) 2023-01-20 13:47:55 +01:00