home-assistant-ecosystem / home-assistant-cli

:computer: Command-line tool for Home Assistant
Other
446 stars 69 forks source link

Cannot add server #406

Closed oneseventhree closed 1 year ago

oneseventhree commented 1 year ago

Hi all, I am new to all this so can anyone tell me why this won't work: image

I've tried all 3 and none can add my server. Same thing for token (has-cli --token tokenhere)

What am I doing wrong?

maxandersen commented 1 year ago

You need to actually do an actual command. -s is just to tell it what server to use for this specific command.

oneseventhree commented 1 year ago

You need to actually do an actual command. -s is just to tell it what server to use for this specific command.

What would be the command to add the server and token? The docs don’t really help me

maxandersen commented 1 year ago

You don't add server/token.

You pass them as argument as you showed.

Or you set the environment variables as described in the "setup" section of the readme.

Hope that helps.

maxandersen commented 1 year ago

Let me repeat it here:

Then you can use --server and --token parameter on each call or as is recommended setup HASS_SERVER and HASS_TOKEN environment variables.

$ export HASS_SERVER=https://homeassistant.local:8123 $ export HASS_TOKEN=

oneseventhree commented 1 year ago

Let me repeat it here:

Then you can use --server and --token parameter on each call or as is recommended setup HASS_SERVER and HASS_TOKEN environment variables.

$ export HASS_SERVER=https://homeassistant.local:8123 $ export HASS_TOKEN=

Thanks for the help! That is the part I do not understand.

Where do I type: export HASS_SERVER=https://homeassistant.local:8123 $ export HASS_TOKEN=

I get errors if I type it in where I was typing it jn my screenshots (windows cmd)

how do I pass them as args?

aziraphale commented 1 year ago

Where do I type: export HASS_SERVER=https://homeassistant.local:8123 $ export HASS_TOKEN=

I get errors if I type it in where I was typing it jn my screenshots (windows cmd)

how do I pass them as args?

You have two options for specifying the server and token: you can either pass them as arguments to hass-cli every time you run the command, or you can save them as environment variables. Using environment variables is much easier in the long term (less to type or copy-paste), but passing them as option arguments might be easier for initial setup to verify that everything works.

Your main problem in your first post was that you didn't specify a command for hass-cli to execute: one of the area, config, device, discover, entity, event, ha, map, raw, service, state, system, template commands shown by hass-cli --help.

When you ran

hass-cli --server https://192.168.1.173:8123

… you basically told hass-cli to connect to your server but you didn't tell it want to do once connected - hence the "Missing command" errors. hass-cli sees little point in connecting to the server only to disconnect immediately because it has nothing to do :)

I suggest using area list as a simple test command. Try this (inserting your token as appropriate):

hass-cli --server https://192.168.1.173:8123 --token INSERT_TOKEN_HERE area list

(Jump to the end of this reply if you get an error like ClientConnectorCertificateError: Cannot connect to host … certificate verify failed: certificate has expired)

If that works, you can save the server address and token in environment variables. The main setup instructions assume a Linux-style command line such as bash; Windows cmd.exe requires a different approach, which is why those export commands didn't work for you.

I'd suggest using setx to set the HASS_SERVER and HASS_TOKEN environment variables:

setx HASS_SERVER https://192.168.1.173:8123
setx HASS_TOKEN INSERT_TOKEN_HERE

Then OPEN A NEW CMD.EXE WINDOW to run:

hass-cli area list

(because setx only applies to future sessions, not existing consoles…)

If you want to verify that HASS_SERVER and HASS_TOKEN are set correctly:

echo %HASS_SERVER%
echo %HASS_TOKEN%

Here are some screenshots of my tests, in case it helps: image

(It's possible to change environment variables in the current session using set (as opposed to setX), but that only applies to the current session - they aren't permanently saved - and set has a slightly different syntax that may just be confusing:

set HASS_SERVER=https://192.168.1.173:8123
set HASS_TOKEN=INSERT_TOKEN_HERE

But don't add any spaces around the =!cmd.exe is a mess 🤣 another reference)


SSL Certificate Errors (Let's Encrypt)

(This may or may not apply to you, depending on whether you're using HTTPS and, if so, how you have it configured. Your only option for SSL to a bare IP address is using a self-signed certificate, in which case you probably already have it installed and trusted and this therefore won't apply. However, it might help someone else trying to set up hass-cli on Windows.)

When I tried hass-cli in cmd.exe I got this error message:

error: ClientConnectorCertificateError: Cannot connect to host … ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1122)')]

My Let's Encrypt-issued SSL certificate wasn't expired, but the solution was for me to export the intermediate SSL certificate via Chrome and install that into the "Intermediate Certification Authorities" certificate store:

(Thanks to these Stack Overflow answers for pointing me in the right direction!)

oneseventhree commented 1 year ago

Where do I type: export HASS_SERVER=https://homeassistant.local:8123 $ export HASS_TOKEN= I get errors if I type it in where I was typing it jn my screenshots (windows cmd) how do I pass them as args?

You have two options for specifying the server and token: you can either pass them as arguments to hass-cli every time you run the command, or you can save them as environment variables. Using environment variables is much easier in the long term (less to type or copy-paste), but passing them as option arguments might be easier for initial setup to verify that everything works.

Your main problem in your first post was that you didn't specify a command for hass-cli to execute: one of the area, config, device, discover, entity, event, ha, map, raw, service, state, system, template commands shown by hass-cli --help.

When you ran

hass-cli --server https://192.168.1.173:8123

… you basically told hass-cli to connect to your server but you didn't tell it want to do once connected - hence the "Missing command" errors. hass-cli sees little point in connecting to the server only to disconnect immediately because it has nothing to do :)

I suggest using area list as a simple test command. Try this (inserting your token as appropriate):

hass-cli --server https://192.168.1.173:8123 --token INSERT_TOKEN_HERE area list

(Jump to the end of this reply if you get an error like ClientConnectorCertificateError: Cannot connect to host … certificate verify failed: certificate has expired)

If that works, you can save the server address and token in environment variables. The main setup instructions assume a Linux-style command line such as bash; Windows cmd.exe requires a different approach, which is why those export commands didn't work for you.

I'd suggest using setx to set the HASS_SERVER and HASS_TOKEN environment variables:

setx HASS_SERVER https://192.168.1.173:8123
setx HASS_TOKEN INSERT_TOKEN_HERE

Then OPEN A NEW CMD.EXE WINDOW to run:

hass-cli area list

(because setx only applies to future sessions, not existing consoles…)

If you want to verify that HASS_SERVER and HASS_TOKEN are set correctly:

echo %HASS_SERVER%
echo %HASS_TOKEN%

Here are some screenshots of my tests, in case it helps: image

(It's possible to change environment variables in the current session using set (as opposed to setX), but that only applies to the current session - they aren't permanently saved - and set has a slightly different syntax that may just be confusing:

set HASS_SERVER=https://192.168.1.173:8123
set HASS_TOKEN=INSERT_TOKEN_HERE

But don't add any spaces around the =!cmd.exe is a mess 🤣 another reference)

SSL Certificate Errors (Let's Encrypt)

(This may or may not apply to you, depending on whether you're using HTTPS and, if so, how you have it configured. Your only option for SSL to a bare IP address is using a self-signed certificate, in which case you probably already have it installed and trusted and this therefore won't apply. However, it might help someone else trying to set up hass-cli on Windows.)

When I tried hass-cli in cmd.exe I got this error message:

error: ClientConnectorCertificateError: Cannot connect to host … ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1122)')]

My Let's Encrypt-issued SSL certificate wasn't expired, but the solution was for me to export the intermediate SSL certificate via Chrome and install that into the "Intermediate Certification Authorities" certificate store:

  • Open your Home Assistant instance in Chrome (other browsers can probably do this as well, but I don't know the exact steps).
  • Click the padlock icon to the left of the URL, then "Connection is secure", and "Certificate is valid".
  • Switch to the "Details" tab.
  • Under "Certificate Hierarchy", select the entry directly above your own server, e.g. "R3" for my Let's Encrypt certificate.
  • Click the "Export…" button at the bottom of the dialog.
  • Save the file somewhere (don't change the default .crt file extension).
  • Navigate to that exported certificate file, right-click it, and select "Install Certificate".
  • Leave "Current User" selected and click "Next".
  • Select "Place all certificates in the following store", click "Browse…", then select "Intermediate Certification Authorities" and click "OK", "Next" and "Finish".
  • Try running the hass-cli command again. That fixed it for me.

(Thanks to these Stack Overflow answers for pointing me in the right direction!)

You. Are. Amazing! This makes so much more sense than the docs! Thank you - this worked perfectly :)

As someone whos new to command line this made perfect sense.