Resurrected Feature: Home Assistant CLI

Update 2019-02-04: 0.4 was released last week that changes the command structure and make output default to tables so the video and some of the commands have changed. See https://github.com/home-assistant/home-assistant-cli for updated docs.

When I started using Home Assistant I quickly got really frustrated having to wait for the browser to connect, click through and do operations - I really could use a command line tool; I couldn’t find one so I made one!

While working on it I found out that @fabaff had done early work in https://github.com/home-assistant/home-assistant-cli and he was so friendly to give commit acces thus this is now being actively developed next to Home Assistant itself.

Its under heavy development, thus its still considered Alpha (things will change/break), but we are looking for early feedback!

To try out latest release:

$ pip install homeassistant-cli

After this hass-cli should be available to you. Make sure hass-cli is at least 0.2 or above as 0.1 does not work with recent Home Assistant versions.

$ hass-cli --version                                                                                                                                                               
  hass-cli, version 0.2.0.dev0

Setup environment with autocompletion:

$ export HASS_SERVER=https://hassio.local:8123
$ export HASS_TOKEN=<long lasting token from /profile>
$  source <(hass-cli completion bash)

And then off you go.

There are more complete examples at https://github.com/home-assistant/home-assistant-cli, but a few highlights are:

Browse your entities with TAB completion:

$ hass-cli entity get light.<TAB>  
  light.kitchen_light_5          light.office_light             light.basement_light_4         
  light.basement_light_9         light.dinner_table_light_4     
  light.winter_garden_light_2    light.kitchen_light_2
  light.kitchen_table_light_1    light.hallroom_light_2         
  light.basement_light_5         light.basement_light_10        
  light.dinner_table_wall_light  light.winter_garden_light_4    

Open a Map for your location:

$ hass-cli map

Toggle entites:

$ hass-cli --output=table entity toggle light.office_light                                                                                                                 
  ENTITY                          DESCRIPTION    STATE
  ------------------------------  -------------  ------- 
  group.office_lights_motionview  office lights  on
  light.office_light              Office Light   on
  group.all_lights                all lights     on

and a bunch more - see https://github.com/home-assistant/home-assistant-cli for details.

I got a lot more ideas I want to add, some/most of them listed at https://github.com/home-assistant/home-assistant-cli/issues.

If you have additional ideas or find bugs do go there and let me know.

Thanks to @fabaff for help, @flowolf for initial feedback and for @frenck being courageous and already included early version of hass-cli in the Hass.io SSH addon - so you can try it out there too (but it will be a few releases behind).

27 Likes

0.2.0 is now available on pypi.

$ pip install homeassistant-cli

(also updated post as use of git clone is no longer needed)

1 Like

Brilliant! Thank you!

1 Like

wow this looks awesome! The dev’s should really implement this

1 Like

Awesome work. I would love to see this added to the core.

1 Like

well, that is what is happening - at least its at same github organization :slight_smile:

I understand your sentiment, but I gave it a think initially and decided to do it as standalone tool. First is that hass-cli can move faster here in the start but also not be tied to specific versions.

Second, at this time it is too early to add it to the core - lets see where it goes first and if there is enough interested.

But even then; I think it will be more interesting to at least make sure hass-cli releases as often as hass itself or/and at least gets tested against the latest releases. If that is in place is there really any need/benefits to be in/closer to the core hass ?

Great work! Love CLI and this was a missing component of HAss. Will test it as soon as possible.

1 Like

First of all, GREAT work! I installed Home Assistant in a virtual environment. Can I run this outside the virtual environment or do I have to be inside the venv to run this? Home Assistant is getting more accessible via multiple channels, and thanks to folks like you that make it happen!

1 Like

You can just run a normal pip install’ and it should just work. Virtual environment is mainly only for Developpment.

Just released 0.3.0 with a few fixes and new features.

pip install homeassistant-cli to get the latest.

Highlights are:

  • add server=auto now uses zeroconf to find HA
    This means that if you haven’t used --server or set HASS_SERVER hass-cli will try locate homeassistant on the local network and use that.

  • Add --password flag for providing API password
    contribution by @dotlambda to allow using the legacy API password feature.

  • Add support for client side certificate
    This was added by request on reddit - to support client side certificates. I have not had a good way to test this yet. Thus if anyone interested in validating if this works or open bug report with tips on how to reproduce it then I would appreciate it!

  • Remove dependency on homeassistant core
    This might sound non-intuitive but this makes the installation significantly smaller and
    we run much less risk interfering with any other dependency you might have installed.
    Also helps make it clear hass-cli can work with multiple versions of HA, not just one specfic one.

For 0.4.0 i’ll focus on getting all the various commands to have beautiful and useful table outputs rather than just “blobs” of data.

Until next time :slight_smile:

Wow, just tested this out and it worked flawlessly, first time. Within 5 minutes the CLI is installed, talking to my HomeAssistant server and autocompleting commands in ZSH. Amazing work, you’ve clearly put a lot of time and energy into this project. Thank you!

1 Like

Hi there - command-line junky here.

I run home assistant in a venv on Ubuntu 18.04. Should I install homeassistant-cli in my normal environment, or in the venv? When I try in my normal environment I get:

nick@nuccy:~$ pip install homeassistant-cli
Collecting homeassistant-cli
Downloading https://files.pythonhosted.org/packages/16/c2/131c48538c54b5c8c7cb99b0767888deaa9fc0ec0e21d7baee76aed85f56/homeassistant-cli-0.1.0.tar.gz
homeassistant-cli requires Python ‘>=3.5.3’ but the running Python is 2.7.15

That’s because your default python is 2.x.

Try using ‘pip3 install’ instead.

You should create a new venv for homeassistant-cli. There is no guarantee that the packages required by homeassistant-cli will work for either homeassistant or the current programs you have in your default environment.

But make sure you create the venv with python3 :grinning:

For what it is worth I’ll consider an incompatible dependency with homeassistant a bug.

hass-cli is a completely separate program from HA, it doesn’t even have to run on the same computer. There is no reason to limit it to the same dependencies as HA.

It makes shared development and sharing of release/cleanup script easier :slight_smile:

0.4 released last week - see https://github.com/home-assistant/home-assistant-cli for updated commands and new features.

So oh my zsh-gawd I just installed the cli 10 seconds ago and already LOVE it!

This exactly what I needed all these months trying to debug a device or appdaemon automation! Thanks @fabaff and @maxandersen for this great work!

I must confess that I always use oh my zsh on any able linux box, but I hardly use 10% of it’s possibilities (simply can’t find the time to invest in the time-saver (I know)). Anyway… I have a hard time finding out what the right way is to incorporate the autocompletion in the right ohmyzsh way. Does anybody know?