HomeAssistantApiClient
An Elixir client for the Home Assistant REST API.
Provides functions to query entity states, call services, update configurations, and evaluate templates in your Home Assistant instance.
Installation
Add home_assistant_api_client to your dependencies in mix.exs:
def deps do
[
{:home_assistant_api_client, "~> 0.1.0"}
]
end
Configuration
In your config/config.exs:
config :home_assistant_api_client,
url: "http://YOUR_HOME_ASSISTANT_URL:8123",
token: "YOUR_LONG_LIVED_ACCESS_TOKEN"
To generate a long-lived access token, go to your Home Assistant profile page (/profile) and scroll to Long-Lived Access Tokens.
Usage
Reading state
# Get all entities
{:ok, entities} = HomeAssistantApiClient.get_entities()
# Get a specific entity
{:ok, state} = HomeAssistantApiClient.get_entity({:input_boolean, "my_switch"})
# Get all entities of a type
{:ok, lights} = HomeAssistantApiClient.get_entities_by_type(:light)
Changing state
# Toggle a boolean
{:ok, _} = HomeAssistantApiClient.set_entity_state({:input_boolean, "my_switch"}, true)
# Set a numeric value
{:ok, _} = HomeAssistantApiClient.set_entity_state({:input_number, "temperature"}, 22)
Calling services
# Turn off a boolean via service
{:ok, _} = HomeAssistantApiClient.set_service({:input_boolean, "my_switch"}, :turn_off)
# Set a numeric value via service
{:ok, _} = HomeAssistantApiClient.set_service({:input_number, "temperature"}, :set_value, 20)
# Restart Home Assistant
{:ok, _} = HomeAssistantApiClient.set_service(:homeassistant, :restart)
Other
# Check configuration
{:ok, result} = HomeAssistantApiClient.check_config()
# Evaluate a template
{:ok, value} = HomeAssistantApiClient.template_host("{{ states('sensor.temperature') }}")
Logging
This library logs its internal requests/responses at the :debug level, so they stay quiet
unless you ask for them. To see them:
# For the whole application
config :logger, level: :debug
# Or just for this library, without touching the rest of your app's logging
Logger.put_module_level(HomeAssistantApiClient, :debug)
Documentation
Full documentation available at https://hexdocs.pm/home_assistant_api_client.