Packages

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.

Current section

Files

Jump to

README.md

# HomeAssistantApiClient
An Elixir client for the [Home Assistant REST API](https://developers.home-assistant.io/docs/api/rest/).
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`:
```elixir
def deps do
[
{:home_assistant_api_client, "~> 0.1.0"}
]
end
```
## Configuration
In your `config/config.exs`:
```elixir
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
```elixir
# 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
```elixir
# 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
```elixir
# 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
```elixir
# 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:
```elixir
# For the whole application
config :logger, level: :debug
```
```elixir
# 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](https://hexdocs.pm/home_assistant_api_client).