Commit Graph

225 Commits (301c484e05e5f15a065a020c4e75bce816e91d43)

Author SHA1 Message Date
Marc Mueller 900b6211ef
Use PEP 695 for function annotations (2) (#117659) 2024-05-18 11:44:39 +02:00
Marc Mueller 87bb7ced79
Use PEP 695 for simple type aliases (#117633) 2024-05-17 14:42:21 +02:00
Matthias Alphart ec4c8ae228
Allow templates for enabling actions (#117049)
* Allow templates for enabling automation actions

* Use `cv.template` instead of `cv.template_complex`

* Rename test function
2024-05-15 21:03:52 +02:00
Sid ac54cdcdb4
Enable Ruff RUF010 (#115371)
Co-authored-by: J. Nick Koston <nick@koston.org>
2024-05-08 23:54:49 +02:00
Marc Mueller 8f614fb06d
Use HassKey for helpers (2) (#117013) 2024-05-07 11:24:13 -05:00
Sid 2cc916db6d
Replace pylint broad-except with Ruff BLE001 (#116250) 2024-05-07 14:00:27 +02:00
Sid b456d97e65
Replace pylint protected-access with Ruff SLF001 (#115735) 2024-05-06 20:33:26 +02:00
J. Nick Koston 91fa8b50cc
Turn on thread safety checks in async_dispatcher_send (#116867)
* Turn on thread safety checks in async_dispatcher_send

We keep seeing issues where async_dispatcher_send is called from
a thread which means we call the callback function on the other
side in the thread as well which usually leads to a crash

* Turn on thread safety checks in async_dispatcher_send

We keep seeing issues where async_dispatcher_send is called from
a thread which means we call the callback function on the other
side in the thread as well which usually leads to a crash

* adjust
2024-05-05 16:29:43 -04:00
J. Nick Koston 164403de20
Add thread safety checks to async_create_task (#116339)
* Add thread safety checks to async_create_task

Calling async_create_task from a thread almost always results in an
fast crash. Since most internals are using async_create_background_task
or other task APIs, and this is the one integrations seem to get wrong
the most, add a thread safety check here

* Add thread safety checks to async_create_task

Calling async_create_task from a thread almost always results in an
fast crash. Since most internals are using async_create_background_task
or other task APIs, and this is the one integrations seem to get wrong
the most, add a thread safety check here

* missed one

* Update homeassistant/core.py

* fix mocks

* one more internal

* more places where internal can be used

* more places where internal can be used

* more places where internal can be used

* internal one more place since this is high volume and was already eager_start
2024-04-28 18:29:00 -04:00
J. Nick Koston 7715bee6b0
Fix script in restart mode that is fired from the same trigger (#116247) 2024-04-27 07:08:29 -05:00
J. Nick Koston a22c221722
Rename bus._async_fire to bus.async_fire_internal (#116027) 2024-04-23 22:28:31 +02:00
J. Nick Koston 4529268544
Ensure scripts with timeouts of zero timeout immediately (#115830) 2024-04-19 18:24:54 +02:00
J. Nick Koston f80894d56f
Stop scripts with eager tasks (#115340) 2024-04-10 08:41:13 -04:00
J. Nick Koston 569f54d8e3
Terminate scripts with until and while conditions that execute more than 10000 times (#115110) 2024-04-07 11:02:53 -10:00
Sid 0d66d298ec
Enable Ruff RET504 (#114528)
* Enable Ruff RET504

* fix test

* Use noqa instead of cast

* fix sonos RET504

---------

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2024-04-06 11:07:37 +02:00
Marc Mueller 816ce116bf
Remove unnecessary functools.cached_property backport (#114239) 2024-04-04 11:24:26 +02:00
Sid f7b7f74d10
Enable Ruff TRY201 (#114269)
* Enable Ruff TRY201

* remove redundant rules
2024-03-28 10:18:07 +01:00
J. Nick Koston f4922edb4b
Fix empty delays in script helper (#114346)
fixes
```
Logger: homeassistant.components.automation.kamermaster_knop_4_acties_licht
Bron: components/automation/__init__.py:726
integratie: Automatisering (documentatie, problemen)
Eerst voorgekomen: 22:17:29 (5 gebeurtenissen)
Laatst gelogd: 22:59:24

While executing automation automation.kamermaster_knop_4_acties_licht
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 726, in async_trigger
    return await self.action_script.async_run(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1645, in async_run
    return await asyncio.shield(create_eager_task(run.async_run()))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 454, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 506, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 536, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 504, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 626, in _async_delay_step
    if timeout_future.done():
       ^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'done'
```
2024-03-28 00:29:43 -04:00
Marc Mueller 911a31f860
Use SignalType to improve typing [core] (#114298) 2024-03-27 14:25:02 +01:00
Franck Nijhof 670bd97777
Find referenced labels in automations & scripts (#113812) 2024-03-19 16:28:37 +01:00
Franck Nijhof 38d0854b70
Find referenced floors in automations & scripts (#113802) 2024-03-19 14:18:53 +01:00
Sid ccd2e989c3
Enable ruff RUF005 and fix occurrences (#113589) 2024-03-16 07:37:20 -10:00
J. Nick Koston bdede0e0da
Start script runs eagerly (#113190) 2024-03-14 16:53:26 -10:00
J. Nick Koston 09934d44c4
Reduce script overhead by avoiding creation of many tasks (#113183)
* Reduce script overhead by avoiding creation of many tasks

* no eager stop

* reduce

* make sure wait being cancelled is handled

* make sure wait being cancelled is handled

* make sure wait being cancelled is handled

* preen

* preen

* result already raises cancelled error, remove redundant code

* no need to raise it into the future

* will never set an exception

* Simplify long action script implementation

* comment

* preen

* dry

* dry

* preen

* dry

* preen

* no need to access protected

* no need to access protected

* dry

* name

* dry

* dry

* dry

* dry

* reduce name changes

* drop one more task

* stale comment

* stale comment
2024-03-14 20:28:27 -04:00
J. Nick Koston 427de00524
Remove unnecessary use of async_run_job in script helper (#113118)
The function being passed to `async_run` was always a callback
2024-03-11 21:36:18 -04:00
J. Nick Koston 324266a4e6
Fix race in script stop that could cause async_stop to hang forever (#113089) 2024-03-11 10:48:11 -10:00
Marc Mueller 19ab3d6daf
Add empty line after module docstring [helpers + other] (#112707) 2024-03-08 10:36:11 -05:00
J. Nick Koston 3877a56d23
Avoid creating tasks for automation and script validation (#111181)
These functions created tasks to run small validators, and the cost of
creating all the tasks was more expensive than running the validators
themselves. Since the code is unlikely to suspend its more efficient to
await them in series.
2024-02-23 13:41:36 -05:00
Marc Mueller cd0ee98dba
Use builtin TimeoutError [core + helpers] (#109684) 2024-02-05 12:09:54 +01:00
Erik Montnemery 3ce4e53b32
Sort script actions (#108247) 2024-01-31 20:41:53 +01:00
J. Nick Koston f96f4d31f7
Convert referenced registry functions to use cached_property (#108895)
* Convert referenced registry functions to use cached_property

These already implemented caching, but now that we can use cached_property
because the lock problem is solved, we can make the code simplier and faster

* missed one

* make them the same
2024-01-26 23:02:42 -05:00
Erik Montnemery 909e58066d
Fix changed_variables in automation and script traces (#108788) 2024-01-24 19:12:45 +01:00
Erik Montnemery 9bff039d17
Add set_conversation_response script action (#108233)
* Add set_conversation_response script action

* Update homeassistant/components/conversation/trigger.py

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

* Revert accidental change

* Add test

* Ignore mypy

* Remove incorrect callback decorator

* Update homeassistant/helpers/script.py

* Add test with templated set_conversation_response

---------

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
2024-01-23 09:13:42 -05:00
Marc Mueller ec708811d0
Enable strict typing for trace (#107945) 2024-01-14 09:39:22 +01:00
Erik Montnemery 461dad3039
Fix changed_variables in automation traces (#106665) 2023-12-30 08:34:21 +01:00
Erik Montnemery 09ad1a9a36
Remove unnecessary block use of pylint disable in components p-z (#100192) 2023-09-12 20:47:48 +02:00
Allen Porter 7b1c0c2df2
Extend template entities with a script section (#96175)
* Extend template entities with a script section

This allows making a trigger entity that triggers a few times a day,
and allows collecting data from a service resopnse which can be
fed into a template entity.

The current alternatives are to publish and subscribe to events or to
store data in input entities.

* Make variables set in actions accessible to templates

* Format code

---------

Co-authored-by: Erik <erik@montnemery.com>
2023-09-02 16:19:45 -07:00
Marc Mueller a9ade1f84d
Use asyncio.timeout [core] (#98447) 2023-08-15 08:36:05 -05:00
Franck Nijhof 3e85a29b86
Move overlapping pylint rules to ruff, disable mypy overlap (#94359) 2023-06-27 17:42:46 +02:00
Erik Montnemery cb22fb16f8
Allow returning a script variable from a script (#95346)
* Allow returning a script variable from a script

* Don't allow returning a template result

* Raise if response variable is undefined

* Add test

* Update homeassistant/helpers/script.py

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>

* Format code

---------

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
2023-06-27 17:13:53 +02:00
Paulus Schoutsen 5f14cdf69d
Allow stopping a script with a response value (#95284) 2023-06-27 08:24:22 +02:00
Allen Porter 1d9821efa2
Fix spelling mistake in script.py (#95210)
Update script.py
2023-06-25 21:00:50 -04:00
Allen Porter 528c206094
Add script specific error messages for `response_variable` (#95188) 2023-06-24 21:34:57 -07: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 c4284c07b6
Allow scripts to capture service response data in variables (#94757)
* Allow scripts service actions to save return values

* Simplify script service response data

* Rename result_variable to response_variable based on feedback
2023-06-16 22:59:44 -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
epenet 913156b0e0
Avoid lingering timer on script shutdown (#89753) 2023-03-16 08:00:21 +01:00
Karlie Meads e40a9822f5
Fix disabled condition within an automation action (#87213)
fixes undefined
2023-02-02 22:35:02 +01:00
Marc Mueller 8abce25948
Update Union typing (4) [Py310] (#86427) 2023-01-23 09:04:40 +01:00
Franck Nijhof 79b52a2b41
Stricter pylint message control (#86154) 2023-01-20 13:47:55 +01:00