Giter Site home page Giter Site logo

hass-ruby's Introduction

hass-client, Ruby client for HomeAssistant

hass-client is a simple Ruby client for the HomeAssistant API.

The idea was to have a client which has actual classes for the available devices. This way, you can inspect objects and see your possible actions. It works for the available services but not yet for all states.

Installation

Install the gem via

gem install hass-client

or put it in your Gemfile

gem 'hass-client'

Usage

Command line client

The command line client allows to send simple commands to your HomeAssistant instance.

You need to set the environment variables HASS_HOST, HASS_PORT, and HASS_TOKEN. To get your API token, log in to your HomeAssistant instance and go your profile (the initials in the sidebar). Then scroll down and ceate an authentication token.

To set these variables in the shell use:

export HASS_HOST=your-hostname.example.com
export HASS_PORT=8123
export HASS_TOKEN=abcdef
export HASS_TLS=1

HASS_SSL=1 ensures a TLS (SSL) connection to your server.

hass-send <entity_id> <service>

for example:

hass-send light.living_room turn_on
hass-send light.living_room turn_off
hass-send light.living_room toggle

Always use the full object name (with the dot) so the client can derive the object type.

Library

A simple script the toogle a light switch might look like this:

require 'hass/client'

client = Hass::Client.new('localhost', 8123, 'api_token')

light = client.light('light.living_room')
light.toggle

You can get a list of your available device types (domains) via

pp(client.domains.map { |domain| domain['domain'] })

You can also get a list of your available entity ids via

pp(client.states.map {|state| state['entity_id']} )

After initializing the client, you can instantiate the available classes directly. You need to provide the client object to be able to use them:

client = Hass::Client.new('localhost', 8123, 'api_token')
mediaplayer = Hass::MediaPlayer.new('media_player.yamaha_receiver')
mediaplayer.client = client
pp mediaplayer.attributes
mediaplayer.select_source(source: 'HDMI1')

Caveats

To make multiple calls to client.domains faster, the result is cached. If you have a longer running application, you will have to set @domains to nil to re-request the data. Other requests are not cached at all and this might significantly slow down access to multiple devices.

hass-ruby's People

Contributors

kayssun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.