This is about integration to and from HASS regarding REST API and web framework related.
Webhook
https://www.home-assistant.io/docs/automation/trigger/#webhook-trigger
Create an automation and set the trigger to webhook.
The webhook will be a long randomized string for security

If Only Accessible from the local network is selected, only the ip:port can be used.
The webhook can accept both GET and POST requests.
curl -X POST -d 'key=value&key2=value2' https://192.168.0.1:8123/api/webhook/some_hook_id- the address is located at
/api/webhook
Rest API
To use rest API, need to create a key
- under Profile → Security → Long-lived access token
https://developers.home-assistant.io/docs/api/rest/
The authentication is done via header
Authorization: Bearer token_hereGet entity state or attribute GET
/api/states/entity.id
- exposes all attributes and state
- use
statesendpoint without ID to get all the entities
Trigger an Action POST (formerly known as service)
/api/services/<domain>/<service>
{"entity_id": "domain.entity_id"}- the
domaineg. switch, automation - the
serviceis the type of action
For example in HA, the action isswitch.turn_off, switch is the domain andturn_offis the service, the full URL would beapi/services/switch/turn_off
POST With Data (eg. to send command via adb shell)
The data is to be included in the payload, assuming in Developer Tools the YAML command is
action: androidtv.adb_command
target:
entity_id: media_player.android_tv_adb
data:
command: RIGHT- the action takes parameter
commandvia valueRIGHT
The respective JSON payload
{"entity_id": "media_player.android_tv_adb", "command": "RIGHT"}Rest Commands (Action) ^63dce3
https://www.home-assistant.io/integrations/rest_command/
To include restful commands in configuration.yaml
rest_command: !include rest.yamlname_of_action::
url: 'http://10.10.120.1:1378/api/StartAction'
method: POST
headers:
content-type: 'application/json'
payload: '{"key": "value", "array": [{"k": "v", "value": "{{ variable }}"}]}'name_of_actionthe action that will be displayed asrest_command.name_of_action- options for request types and header
- payload is the data that will be sent to the API
- the content of the payload can be customized using
{{ variable }}
To use custom variables in automation
- the content of the payload can be customized using
- service: rest_command.name_of_action
data: # additional options
variable: value_that_replace_variable
data: {} # if no additional options defined- the custom variable is defined under
datablock as key value pairs