Sending a service request to Home Assistant
-
I'd like to invoke in Reactor a Home Assistant service through the HA API. I use the following code in a vera plugin I wrote to send an HTTP request to Home Assistant to trigger an announcement on Alexa--using a Home Assistant Alexa integration that you can install through HACS.
myEchoDevice = "alexa_media_".. myEchoDevice myEchoMessage = table.concat(myEchoMessage) local request_body = json.encode {message = myEchoMessage, data = {["type"] = "announce",method = "all"}} local response_body = {} local theURL = 'http://'..HomeAssistantIP..':'..HomeAssistantPort..'/api/services/notify/'..myEchoDevice r, c, h = http.request { url = theURL, method = "POST", headers = { ["Content-Type"] = "application/json", ["Authorization"] = "Bearer ".. HomeAssistantToken, ["Content-Length"] = request_body:len() }, source = ltn12.source.string(request_body), sink = ltn12.sink.table(response_body) }
I don't see the "notify" service in the HA attributes revealed by reactor. Is there a way to access the Home Assistant API through reactor, or do I need to use HTTP requests to initiate services.
-
One of the big gaps in the Home Assistant API is that it doesn't expose what services are available on every entity. I don't think it actually knows. But, you can tell MSR that an entity uses a particular service. You should have a file called
local_hass_devices.yaml
in yourconfig
subdirectory. If not, it's no trouble to just add it. There's also a default version indist-config
you can copy. To that file, in theentities
section, just add entry for the entity using its canonical ID, and the services you want to add to it. Here's a sample file to give you the general idea:# This file has local definitions/overrides for HomeAssistant devices. --- entities: # Entry shows how an entity can have a service added and its name forced "hass>sensor_connected_clients": name: "WebSocket Clients" services: - homeassistant # This media player is a Sonos device, so it supports TTS "hass>media_player_portable": services: - tts # This media player is a Sonos device, so it supports TTS "hass>media_player_media_room": services: - tts # "hass>switch_power_switch": services: - zwave_js
Or, to do it the simplest way possible, there's a
call_service
action on the system entity for the HassController instance. -
T toggledbits locked this topic on