├── .gitignore ├── LICENSE ├── README.md ├── hacs.json ├── info.md └── python_scripts └── set_state.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Set State 2 | Home Assistant Python Script to force set an entity state 3 | 4 | ## How it works 5 | This script adds a service that lets you update the state and/or attributes of any entity, similar to the developer tools 6 | 7 | Requires `python_script:` to be enabled in you configuration 8 | 9 | ## Installation 10 | 11 | ### HACS 12 | Requirements: 13 | - HACS version 0.9 or higher 14 | - Enabled `python_scripts:` in your Home Assistant configuration 15 | - Enabled `python_scripts` as option, in the HACS configuration. (If you missed this, kindly read below!) 16 | 17 | You are only able to use this custom repo with HACS, if Python Scripts is enabled in Home Assistant. Also, "python_scripts" should have been enabled during the HACS setup through "Integrations" in Home Assistant. 18 | ![Missing Python as option](https://i.imgur.com/IGA4LVz.png) 19 | 20 | If you missed that part, you need to remove HACS, and add it again, in order to find the "Python Script" category! This time, remember to set a checkmark in "Enable python_scripts discovery & tracking": 21 | 22 | ![HACS Configuration](https://i.imgur.com/bjDUwDz.png) 23 | 24 | Find more information about the HACS configuration [here](https://hacs.xyz/docs/configuration/basic#changing-the-configuration) and [here](https://hacs.xyz/docs/categories/python_scripts). _If you used the YAML-way to configure HACS, it's a bit easier. Just edit the YAML configuration for HACS. That parts is also mentioned [https://hacs.xyz/docs/categories/python_scripts#enable-if-you-used-yaml-to-configure-hacs](here)._ 25 | 26 | ![All good](https://i.imgur.com/odPV98j.png) 27 | 28 | ### Manual ### 29 | Copy the Python script in to your `/config/python_scripts` directory. 30 | 31 | ## Service arguments 32 | key | required | type | description 33 | -- | -- | -- | -- 34 | `entity_id:` | True | string | The full entity id to update 35 | `state:` | False | any | The state value 36 | `allow_create:` | False | Boolean | Allow the entity to be created if it does not exists (Defaults to false so mis-typed entities do not accidentally get created.) 37 | `...:` | False | any | any additional name becomes or replaces and attribute on the entity 38 | 39 | ## Usage 40 | Each call requires at least an entity_id and a state or attributes (otherwise it wont do anything) 41 | 42 | example: 43 | 44 | ``` 45 | entity_id: sensor.random_sensor 46 | state: 0 47 | allow_create: true 48 | ``` 49 | 50 | (Thanks to [@rodpayne](https://community.home-assistant.io/u/rodpayne) for the initial script: 51 | (https://community.home-assistant.io/t/how-to-manually-set-state-value-of-sensor/43975) 52 | -------------------------------------------------------------------------------- /hacs.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Set Entity State Service", 3 | "homeassistant": "0.90.0" 4 | } 5 | -------------------------------------------------------------------------------- /info.md: -------------------------------------------------------------------------------- 1 | # Set State 2 | Home Assistant Python Script to force set an entity state 3 | 4 | ## How it works 5 | This script adds a service that lets you update the state and/or attributes of any entity, similar to the developer tools 6 | 7 | Requires `python_script:` to be enabled in you configuration 8 | 9 | ## Service arguments 10 | key | required | type | description 11 | -- | -- | -- | -- 12 | `entity_id:` | True | string | The full entity id to update 13 | `state:` | False | any | The state value 14 | `allow_create:` | False | Boolean | Allow the entity to be created if it does not exists (Defaults to false so mis-typed entities do not accidentally get created.) 15 | `...:` | False | any | any additional name becomes or replaces and attribute on the entity 16 | 17 | ## Usage 18 | Each call requires at least an entity_id and a state or attributes (otherwise it wont do anything) 19 | 20 | example: 21 | 22 | ``` 23 | entity_id: sensor.random_sensor 24 | state: 0 25 | allow_create: true 26 | ``` 27 | 28 | (Thanks to [@rodpayne](https://community.home-assistant.io/u/rodpayne) for the initial script: 29 | (https://community.home-assistant.io/t/how-to-manually-set-state-value-of-sensor/43975) 30 | -------------------------------------------------------------------------------- /python_scripts/set_state.py: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # python_scripts/set_state.py 3 | # modified from - https://community.home-assistant.io/t/how-to-manually-set-state-value-of-sensor/43975/37 4 | #================================================================================================== 5 | 6 | #-------------------------------------------------------------------------------------------------- 7 | # Set the state or other attributes for the entity specified in the Automation Action 8 | #-------------------------------------------------------------------------------------------------- 9 | 10 | inputEntity = data.get('entity_id') 11 | if inputEntity is None: 12 | logger.warning("===== entity_id is required if you want to set something.") 13 | else: 14 | inputStateObject = hass.states.get(inputEntity) 15 | if inputStateObject is None and not data.get('allow_create'): 16 | logger.warning("===== unknown entity_id: %s", inputEntity) 17 | else: 18 | if not inputStateObject is None: 19 | inputState = inputStateObject.state 20 | inputAttributesObject = inputStateObject.attributes.copy() 21 | else: 22 | inputAttributesObject = {} 23 | 24 | for item in data: 25 | newAttribute = data.get(item) 26 | logger.debug("===== item = {0}; value = {1}".format(item,newAttribute)) 27 | if item == 'entity_id': 28 | continue # already handled 29 | elif item == 'allow_create': 30 | continue # already handled 31 | elif item == 'state': 32 | inputState = newAttribute 33 | else: 34 | inputAttributesObject[item] = newAttribute 35 | 36 | hass.states.set(inputEntity, inputState, inputAttributesObject) 37 | 38 | --------------------------------------------------------------------------------