Commit Graph

64 Commits (8901508df6b4f6bc44c1709eb8ba084f111c7b82)

Author SHA1 Message Date
Paulus Schoutsen aef06a3544
Directly call write state 2 (#33513)
* Directly call async_write_ha_state pt2

* Directly call async_write_ha_state pt2

* Fix mock

* Address comments
2020-04-03 09:34:50 +02:00
Paulus Schoutsen f95c3e265d
Fix script logging with name (#33120) 2020-03-22 13:29:50 +01:00
Phil Bruckner 5f5cb8bea8
Add support for simultaneous runs of Script helper - Part 2 (#32442)
* Add limit parameter to service call methods

* Break out prep part of async_call_from_config for use elsewhere

* Minor cleanup

* Fix improper use of asyncio.wait

* Fix state update

Call change listener immediately if its a callback

* Fix exception handling and logging

* Merge Script helper if_running/run_mode parameters into script_mode

- Remove background/blocking _ScriptRun subclasses which are no longer needed.

* Add queued script mode

* Disable timeout when making fully blocking script call

* Don't call change listener when restarting script

This makes restart mode behavior consistent with parallel & queue modes.

* Changes per review

- Call all script services (except script.turn_off) with no time limit.
- Fix handling of lock in _QueuedScriptRun and add comments to make it
  clearer how this code works.

* Changes per review 2

- Move cancel shielding "up" from _ScriptRun.async_run to Script.async_run
  (and apply to new style scripts only.) This makes sure Script class also
  properly handles cancellation which it wasn't doing before.
- In _ScriptRun._async_call_service_step, instead of using script.turn_off
  service, just cancel service call and let it handle the cancellation
  accordingly.

* Fix bugs

- Add missing call to change listener in Script.async_run
  in cancelled path.
- Cancel service task if ServiceRegistry.async_call cancelled.

* Revert last changes to ServiceRegistry.async_call

* Minor Script helper fixes & test improvements

- Don't log asyncio.CancelledError exceptions.
- Make change_listener a public attribute.
- Test overhaul
  - Parametrize tests.
  - Use common test functions.
  - Mock timeout so tests don't need to wait for real time to elapse.
  - Add common function for waiting for script action step.
2020-03-11 16:34:50 -07:00
Paulus Schoutsen 6a21afa2a8
Improve script validation (#32461) 2020-03-05 11:44:42 -08:00
Phil Bruckner b2d7bc40dc
Add support for simultaneous runs of Script helper (#31937)
* Add tests for legacy Script helper behavior

* Add Script helper if_running and run_mode options

- if_running controls what happens if Script run while previous run
  has not completed. Can be:
  - error: Raise an exception
  - ignore: Return without doing anything (previous run continues as-is)
  - parallel: Start run in new task
  - restart: Stop previous run before starting new run
- run_mode controls when call to async_run will return. Can be:
  - background: Returns immediately
  - legacy: Implements previous behavior, which is to return when done,
            or when suspended by delay or wait_template
  - blocking: Returns when run has completed
- If neither is specified, default is run_mode=legacy (and if_running
  is not used.) Otherwise, defaults are if_running=parallel and
  run_mode=background. If run_mode is set to legacy then if_running must
  be None.
- Caller may supply a logger which will be used throughout instead of
  default module logger.
- Move Script running state into new helper classes, comprised of an
  abstract base class and two concrete clases, one for legacy behavior
  and one for new behavior.
- Remove some non-async methods, as well as call_from_config which has
  only been used in tests.
- Adjust tests accordingly.

* Change per review

- Change run_mode default from background to blocking.
- Make sure change listener is called, even when there's an unexpected
  exception.
- Make _ScriptRun.async_stop more graceful by using an asyncio.Event for
  signaling instead of simply cancelling Task.
- Subclass _ScriptRun for background & blocking behavior.

Also:

- Fix timeouts in _ScriptRun by converting timedeltas to float seconds.
- General cleanup.

* Change per review 2

- Don't propagate exceptions if call from user has already returned
  (i.e., for background runs or legacy runs that have suspended.)
- Allow user to specify if exceptions should be logged. They will still
  be logged regardless if exception is not propagated.
- Rename _start_script_delay and _start_wait_template_delay for
  clarity.
- Remove return value from Script.async_run.
- Fix missing await.
- Change call to self.is_running in Script.async_run to direct test of
  self._runs.

* Change per review 3 and add tests

- Remove Script.set_logger().
- Enhance existing tests to check all run modes.
- Add tests for new features.
- Fix a few minor bugs found by tests.
2020-02-24 14:56:00 -08:00
Paulus Schoutsen d24e397a80
Handle service calls that do not refer entity IDs (#31317) 2020-01-30 09:28:06 -08:00
Paulus Schoutsen 424e15c7a7
Find related items scripts/automations (#31293)
* Find related items scripts/automations

* Update manifest
2020-01-29 16:19:13 -08:00
Paulus Schoutsen e9e44dbd97
Fix callback and async (#31281)
* Fix callback and async

* Fix a return

* Fix test

* Fix mqtt tests

* Fix some more callbacks
2020-01-29 13:59:45 -08:00
Ville Skyttä fa4fa30461 Various string cleanups (#30435)
* Remove some unnecessary string concatenations

* Replace some simple str.formats with f-strings

* Replace some string concatenations with f-strings
2020-01-03 14:47:06 +01:00
Bas Nijholt 67c56c860d Sort imports according to PEP8 for 'homeassistant' folder (#29789)
Components are already done
2019-12-09 16:42:10 +01:00
Ville Skyttä 99c7608fb4 Lint config cleanups (#28864)
* Remove bunch of unneeded lint exclusions

* Use symbolic names instead of identifiers in pylint disables

* Tighten scope of some pylint disables
2019-11-25 22:40:08 -08:00
Matthew Donoughe d16edb3ef0 add script shortcut for activating scenes (#27223)
* add script shortcut for activating scenes

use `- scene: scene.<scene name>` in a script to activate a scene

* Update validation
2019-10-05 13:30:43 -07:00
Erik Montnemery c43eeee62f Improve validation of device condition config (#27131)
* Improve validation of device condition config

* Fix typing
2019-10-02 15:58:14 -07:00
Erik Montnemery 65ce3b49c1
Add support for `for` to binary_sensor, light and switch device triggers (#26658)
* Add support for `for` to binary_sensor, light and switch device triggers

* Add WS API device_automation/trigger/capabilities
2019-10-02 22:14:52 +02:00
Erik Montnemery 5a1da72d5e Improve validation of device action config (#27029) 2019-10-01 20:35:36 -07:00
Pascal Vizeli c1851a2d94
Cleanup coroutine threadsafe (#27080)
* Cleanup coroutine threadsafe

* fix lint

* Fix typing

* Fix tests

* Fix black
2019-10-01 16:59:06 +02:00
Paulus Schoutsen 6fdff9ffab
Reorg device automation (#26880)
* async_trigger -> async_attach_trigger

* Reorg device automations

* Update docstrings

* Fix types

* Fix extending schemas
2019-09-24 14:57:05 -07:00
Ville Skyttä 54242cd65c Type hint additions (#26765) 2019-09-20 08:23:34 -07:00
Erik Montnemery 02ded7a4a8 Remove device from device action schema (#26536) 2019-09-09 10:40:22 -07:00
Erik Montnemery b14b14c3c9
Add device automation support to switch entities (#26466)
* Add device automation support to switch entities

* Update switch translations

* Address review comments
2019-09-09 16:55:47 +02:00
Erik Montnemery b1c2a5fa08 Add device automation action (#26455)
* Add support for device actions, with light as example.

* Add translation; return list
2019-09-05 16:26:22 -07:00
Erik Montnemery f7dc537275
Add device automation condition (#26313)
* Add support for device conditions

* Lint

* Update test case

* Make and+or conditions async, adjust tests

* Cleanup tests

* Remove non callback versions of conditions, correct typing

* Correct typing

* Update light/strings.json

* Address review comments

* Make device automation lists simple lists, not dicts

* Add device_automation/const.py

* Use IS_ON/IS_OFF everywhere for conditions
2019-09-05 16:49:32 +02:00
Franck Nijhof 2f0eb07624 Migrate legacy typehints in core to PEP-526 (#26403)
* Migrate legacy typehints in core to PEP-526

* Fix one type
2019-09-03 20:36:04 -07:00
Paulus Schoutsen 4de97abc3a Black 2019-07-31 12:25:30 -07:00
Ville Skyttä d64f1e767c Type check all helpers (#25373)
* Type check all helpers, add inline exclusions for work in progress

* Remove unused Script._template_cache

* Add some missing type hints

* Remove unneeded type: ignore

* Type hint fixes

* Mypy assistance tweaks

* Don't look for None in deprecated config "at most once" check

* Avoid None name slugify attempt when generating entity id

* Avoid None state store attempt on entity remove
2019-07-21 09:59:02 -07:00
Paulus Schoutsen 9d9e11372b
Make automations log errors (#18965) 2018-12-13 12:21:16 +01:00
Paulus Schoutsen d028236bf2
Refactor script helper actions into their own methods (#18962)
* Refactor script helper actions into their own methods

* Lint

* Lint
2018-12-03 15:46:25 +01:00
Phil Bruckner 83b4e56978 Log delay and wait_template steps in scripts (#18448)
* Log delay and wait_template steps in scripts

Help improve script debugging by logging delay and wait_template steps in scripts.

* Update tests

* Fix lint issue
2018-11-19 11:14:00 +01:00
Anders Melchiorsen 5db7d702c8 Remove warning on script delay (#17264)
* Remove warning on script delay

* Use suppress
2018-10-09 10:06:42 +02:00
Anders Melchiorsen 720b05c301 Fix race between script delay and turn_off (#16923) 2018-09-27 22:08:09 -06:00
Rohan Kapoor 3d1c8ee467 Implement support for complex templates in script delays (#16442)
* Implement support for complex templates in script delays

* Swap out dict instead of collections.Mapping
2018-09-21 11:57:01 +02:00
Paulus Schoutsen 746f4ac158 Add context to scripts and automations (#16415)
* Add context to script helper

* Update script component

* Add context to automations

* Lint
2018-09-04 21:16:24 +02:00
Hovo (Luke) 6aee535d7c Allow wait template to run the remainder of the script (#15836)
* Adding new feature to allow a wait template to run the remainer of the script on timeout

* Styling changes

* Fixing file permissions, adding test for new code

* changed variable name, refactored script to pass information into async_set_timeout

* Changing the default behaviour to continue to run the script after timeout
2018-08-13 11:23:27 +02:00
Ville Skyttä b7c336a687 Pylint cleanups (#15626)
* Pylint 2 no-else-return fixes

* Remove unneeded abstract-class-not-used pylint disable
2018-07-23 10:16:05 +02:00
Anders Melchiorsen 7166d53e2b Log invalid templates in script delays (#13423)
* Log invalid templates in script delays

* Abort on error

* Remove unused import
2018-03-25 01:12:26 +01:00
Matthias Urlichs f5cc40024d Rename homeassistant.util.async to .async_ (#13088)
"async" is (going to be) a reserved word.
2018-03-11 10:01:12 -07:00
Julius Mittenzwei 16cb7388ee Removing asyncio.coroutine syntax from HASS core (#12509)
* changed asyncio.coroutine syntax to new async def/await

* removed py34 from tox environment

* reverted some changes within entity.py

* -

* reverted changes within bootstrap.py

* reverted changes within discovery.py

* switched decorators

* Reverted change within aiohttp_client.py

* reverted change within logging.py

* switched decorators

* Await lock properly

* removed asyncio.coroutine from test
2018-02-25 03:38:46 -08:00
Otto Winter 678f284015 Upgrade pylint to 1.8.2 (#12274)
* Upgrade pylint to 1.8.1

* Fix no-else-return

* Fix bad-whitespace

* Fix too-many-nested-blocks

* Fix raising-format-tuple

See https://github.com/PyCQA/pylint/blob/master/doc/whatsnew/1.8.rst

* Fix len-as-condition

* Fix logging-not-lazy

Not sure about that TEMP_CELSIUS though, but internally it's probably just like if you concatenated any other (variable) string

* Fix stop-iteration-return

* Fix useless-super-delegation

* Fix trailing-comma-tuple

Both of these seem to simply be bugs:
 * Nest: The value of self._humidity never seems to be used anywhere
 * Dovado: The called API method seems to expect a "normal" number

* Fix redefined-argument-from-local

* Fix consider-using-enumerate

* Fix wrong-import-order

* Fix arguments-differ

* Fix missed no-else-return

* Fix no-member and related

* Fix signatures-differ

* Revert "Upgrade pylint to 1.8.1"

This reverts commit af78aa00f125a7d34add97b9d50c14db48412211.

* Fix arguments-differ

* except for device_tracker

* Cleanup

* Fix test using positional argument

* Fix line too long

I forgot to run flake8 - shame on me... 🙃

* Fix bad-option-value for 1.6.5

* Fix arguments-differ for device_tracker

* Upgrade pylint to 1.8.2

* 👕 Fix missed no-member
2018-02-11 09:20:28 -08:00
Otto Winter 8dcfd35b8b Spelling fixes (#12041)
* Spelling fixes

*Lots* of them.

* Spelling breaking changes

* Fix lint errors
2018-01-29 23:37:19 +01:00
tschmidty69 48619c9d7c Implemented event_data_template (new) (#11057)
* Implemented event_data_template

* The hound does not like my indentation

* Added passed variables to tests for event and svc template calls

* Moved recursive function to template.py

* Update template.py

* Update template.py

* Cleaned up service.py and fixed unit tests

* Blank lines

* Removed stray logger statement

* Blank lines again
2018-01-18 22:13:14 -08:00
cdce8p be5c0b2d92 Wait_template - support for 'trigger.entity_id' and data_template values (#9807)
* *Added support for use of 'trigger.entity_id' and service->data_template->script in wait_template

* * Fixed style violations

* * Fixed regular expression (_RE_GET_POSSIBLE_ENTITIES)

* * combined 'extract_entities' and 'extract_entities_with_variables'
* fixed regular expression

* * Added first test for extract_entities_with_variables

* * Added Unittests (tests/helpers/test_template.py test_extract_entities_with_variables)

* * Added Unittests (tests/helpers/test_script.py test_wait_template_variables)

* * Added Unittests (tests/components/automation/test_template.py test_wait_template_with_trigger)

* * Added Unittests (tests/components/automation/test_state.py test_wait_template_with_trigger)

* * Added Unittests (tests/components/automation/test_numeric_state.py test_wait_template_with_trigger)

* * Fixed style violations

* * Fixed style violations

* * Fixed style violations

* * Fixed style violations

* * Fixed style violations

* * Fixed style violations

* * Updated regular expression and delete whitespaces
2017-10-12 16:57:18 +02:00
Michael Prokop 08b0629eca Fix a bunch of typos (#9545)
s/Addres /Address /
s/Chnage/Change/
s/Converion/Conversion/
s/Supressing/Suppressing/
s/agains /against /
s/allready/already/
s/analagous/analogous/
s/aquired/acquired/
s/arbitray/arbitrary/
s/argment/argument/
s/aroung/around/
s/attibute/attribute/
s/auxillary/auxiliary/
s/befor /before /
s/commmand/command/
s/conatin/contain/
s/conection/connection/
s/coresponding/corresponding/
s/entites/entities/
s/enviroment/environment/
s/everyhing/everything/
s/expected expected/expected/
s/explicity/explicitly/
s/formated/formatted/
s/incomming/incoming/
s/informations/information/
s/inital/initial/
s/inteface/interface/
s/interupt/interrupt/
s/mimick/mimic/
s/mulitple/multiple/
s/multible/multiple/
s/occured/occurred/
s/occuring/occurring/
s/overrided/overridden/
s/overriden/overridden/
s/platfrom/platform/
s/positon/position/
s/progess/progress/
s/recieved/received/
s/reciever/receiver/
s/recieving/receiving/
s/reponse/response/
s/representaion/representation/
s/resgister/register/
s/retrive/retrieve/
s/reuqests/requests/
s/segements/segments/
s/seperated/separated/
s/sheduled/scheduled/
s/succesfully/successfully/
s/suppport/support/
s/targetting/targeting/
s/thats/that's/
s/the the/the/
s/unkown/unknown/
s/verison/version/
s/while loggin out/while logging out/
2017-09-23 17:15:46 +02:00
Fabian Affolter a4f1f6e724 Update docstrings (#7374)
* Update docstrings

* Update docstrings

* Update docstrings

* Update docstrings

* Update docstrings

* Update docstrings

* Update docstring

* Update docstrings

* Update docstrings

* Fix lint issues

* Update docstrings

* Revert changes in dict
2017-05-02 09:18:47 -07:00
Pascal Vizeli 9aac2113b6 Add 'wait_template' to script commands / Refactory track_template (#5827)
* Add 'wait' to script commands.

* Add track_template + unittest / rename wait_template

* fix lint & test

* Fix handling / change automation-template / add tests

* address paulus comments
2017-02-12 13:27:53 -08:00
Daniel Høyer Iversen 467cb18625 Add last triggered to script (#5261)
* Add last triggered to script

* Add tests for script last_triggered
2017-01-11 16:23:05 +01:00
Paulus Schoutsen a343c20404 Async gather wait (#4247)
* Fix config validation for input_*, script

* Allow scheduling coroutines

* Validate entity ids when entity ids set by platform

* Async: gather -> wait

* Script/Group: use async_add_job instead of create_task
2016-11-06 09:26:40 -08:00
Fabian Affolter be272ac64a Disable too-many-* (#4107)
* Disable too-many-* and too-few-public-methods

* Remove globally disabled pylint warnings
2016-10-30 22:18:53 +01:00
Lewis Juggins 10c9132046 Resolve issue with delay not passing variables to render (#3901) 2016-10-16 16:08:12 -07:00
Clemens Wolff 711526e574 Cache condition in helpers.Script (#3797)
* Cache condition in helpers.Script

The caching is a simple in-memory, per-instance dictionary.
We use __str__ to format cache keys.

This naive implementation has some disadvantages (e.g., we won't be able
to cache two conditions that contain references to
distinct-but-equivalent object instances and we don't have any control
over the size of the condition cache), but for most simple use-cases the
approach should be good enough.

Resolves #3629

* Fix docstring style
2016-10-10 23:36:38 -07:00
Paulus Schoutsen d58548dd1c Address asyncio comments (#3663)
* Template platforms: create_task instead of yield from

* Automation: less yielding, more create_tasking

* Helpers.script: less yielding, more create_tasking

* Deflake logbook test

* Deflake automation reload config test

* MQTT: Use async_add_job and threaded_listener_factory

* Deflake other logbook test

* lint

* Add test for automation trigger service

* MQTT client can be called from within async
2016-10-03 22:39:27 -07:00