Control Hass.io server using REST API from a third-party controller

Hello community! Happy New Year!

I ask for help. I am a novice user of Hass.io and have not yet been able to deal with the following question.

How can I control the devices integrated into Hass.io using the REST API through http/tcp from a third-party controller.

I read the following information: https://developers.home-assistant.io/docs/en/external_api_rest.html

I created a long term token. Is the token really so long?

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI3OGUyZjkyNTc0NGE0MjcxOWY0MTIwNDQxOWZmOWFmNCIsImlhdCI6MTU3ODIxOTU2OCwiZXhwIjoxODkzNTc5NTY4fQ.TEvEUieMyT1GYu6F9XCiDEFJI3QxIa8G9wEtnMTLsxQ

I try several ways to create commands, but I get errors:

with this version of the command from third-party controller with IP 192.168.1.10:

POST\x20/api/states/switch.switch_1gang\x0D\x0Aheaders:\x0D\x0Aauthorization:\x20'Bearer\x20eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI3OGUyZjkyNTc0NGE0MjcxOWY0MTIwNDQxOWZmOWFmNCIsImlhdCI6MTU3ODIxOTU2OCwiZXhwIjoxODkzNTc5NTY4fQ.TEvEUieMyT1GYu6F9XCiDEFJI3QxIa8G9wEtnMTLsxQ'\x0D\x0Acontent-type:\x20'application/json'\x0D\x0Apayload:\x20'{"state":"on"}'\x0D\x0A\x0D\x0A

I get this message in third-party controller:

HTTP/0.9\x20401\x20Unauthorized\x0D\x0AContent-Type:\x20text/plain;\x20charset=utf-8\x0D\x0AContent-Length:\x2017\x0D\x0ADate:\x20Thu,\x2009\x20Jan\x202020\x2013:26:06\x20GMT\x0D\x0AServer:\x20Python/3.7\x20aiohttp/3.6.1\x0D\x0A\x0D\x0A401:\x20Unauthorized

and I get this error in Hass.io:

Login attempt or request with invalid authentication from 192.168.1.10

Next.

with this version of the command from third-party controller with IP 192.168.1.10:

curl\x20-X\x20POST\x20-H\x20"Authorization:\x20Bearer\x20eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI3OGUyZjkyNTc0NGE0MjcxOWY0MTIwNDQxOWZmOWFmNCIsImlhdCI6MTU3ODIxOTU2OCwiZXhwIjoxODkzNTc5NTY4fQ.TEvEUieMyT1GYu6F9XCiDEFJI3QxIa8G9wEtnMTLsxQ"\x20-H\x20"Content-Type:\x20application/json"\x20-d\x20'{"entity_id":\x20"switch.switch_1gang"}'\x20http://192.168.1.4:8123/api/services/switch/turn_on

I get this message in third-party controller:

HTTP/1.0\x20400\x20Bad\x20Request\x0D\x0AContent-Type:\x20text/plain;\x20charset=utf-8\x0D\x0AContent-Length:\x2011\x0D\x0ADate:\x20Thu,\x2009\x20Jan\x202020\x2013:31:13\x20GMT\x0D\x0AServer:\x20Python/3.7\x20aiohttp/3.6.1\x0D\x0A\x0D\x0ABad\x20Request

and I get this error in Hass.io:

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 275, in data_received
    messages, upgraded, tail = self._request_parser.feed_data(data)
  File "aiohttp\_http_parser.pyx", line 523, in aiohttp._http_parser.HttpParser.feed_data
aiohttp.http_exceptions.BadStatusLine: invalid HTTP method

Maybe there are somewhere examples of how the command for http/tcp looks right?

Thanks.

What type of device is your third-party controller? I can interact with HA through the REST API with javascript, external python scripts and shell commands from other machines, and all works well. Please be a bit more specific on what you want to acheive.

I use a URC MRX-10 controller https://www.universalremote.com/product/mrx-10/

I need to create commands so that I can control the switches that are in Hass.io.

1 Like

And are you trying to configure a http POST request? Please, if possible, post a screen shot of the configuration page where you are entering the information.

Yes, the token is this long.

Screen shot of the configuration page.

screen shot 1

ok, your syntax is a bit off, I will post a correct line in a minute.

Thanks. Maybe you need more information about URC syntax?

INFO
Types of IP Communication

There are a few ways that devices will utilize to communicate over the network. Most of these concepts are similar in their approach but have different benefits, connection methods, etc. These methods may be Telnet, SSH, REST, Web/HTTP, etc. We will attempt to go over each of these and how they differ from each other as this guide grows. Generally “Telnet” will be considered to be one of the easiest methods of communication however it does not provide the security some of the other methods provide.

Each protocol will have instructions on how-to specifically interact with the hardware itself. At this time the “Web/HTTP” or “REST” method seems to be getting the most momentum as it tends to work well with the technologies developers are already familiar with.

With telnet you are essentially opening a “socket” (or pathway) of communication between your Controller and the device itself. If using default ports this will generally use port “23” but it could be changed based on the manufacturers (or yours if exposed in settings via “Control Port” option) API.

Data Encryption

There are a few different ways that IP Strings may be formatted. These are different in the way they present our data but can be easily translated between each other. Methods available to you within driver creation are ASCII, Decimal, and Hexadecimal. Your API Documentation will generally go over which encryption method you will be using. In an overwhelming majority of situations you will be using “ASCII” which utilizes the characters and text you are generally familiar with (a=a, b=b, etc).

Here is an example of the same strings represented in each encryption method. Various translators/converters exist online and URC's Driver Creation Software will also handle this for you if needed.

ASCII: “Hello”
HEX: “48, 65, 6C, 6C, 6F”
DEC: “72, 101, 108, 108, 111”

Each of these methods have some general rules & guidelines. For example, “white space” (or one or multiple spaces between your characters) is generally not understood by a computer system. White Space is generally ignored by API Documents and requires some sort of “encoded characters” to inform the device you are communicating with that the white space should be included. URL Encoding has taken the lead with most API Documentations, URL Encoding provides a method for a computer to understand various characters that it would not normally understand. For example, a space would be represented by either a “%20”, “x20”, or “+” depending on the situation.
There are a few important URC-Specific encoding that is important to understand.
Below is a general list of these functionalities:

White Space (Space): \x20
Carriage Return: \x0D
Line Feed: \x0A

P.S. Maybe I need to add in configuration.yaml file?

http://IP_ADDRESS:8123/api/

Now I do not have such a record.

This is the correct syntax to use for a curl request:

curl -X POST -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI3OGUyZjkyNTc0NGE0MjcxOWY0MTIwNDQxOWZmOWFmNCIsImlhdCI6MTU3ODIxOTU2OCwiZXhwIjoxODkzNTc5NTY4fQ.TEvEUieMyT1GYu6F9XCiDEFJI3QxIa8G9wEtnMTLsxQ" -H "Content-Type: application/json" -d '{"entity_id": "switch.switch_1gang"}'  http://192.168.1.206:8123/api/services/switch/turn_on

You need to replace this part:

http://192.168.1.206:8123

with the correct ip of your HA instance.

Not work.

Command:

curl\x20-X\x20POST\x20-H\x20"Authorization:\x20Bearer\x20eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI3OGUyZjkyNTc0NGE0MjcxOWY0MTIwNDQxOWZmOWFmNCIsImlhdCI6MTU3ODIxOTU2OCwiZXhwIjoxODkzNTc5NTY4fQ.TEvEUieMyT1GYu6F9XCiDEFJI3QxIa8G9wEtnMTLsxQ"\x20-H\x20"Content-Type:\x20application/json"\x20-d\x20'{"entity_id":\x20"switch.switch_1gang"}'\x20\x20http://192.168.1.4:8123/api/services/switch/turn_on

answer controller:

HTTP/1.0\x20400\x20Bad\x20Request\x0D\x0AContent-Type:\x20text/plain;\x20charset=utf-8\x0D\x0AContent-Length:\x2011\x0D\x0ADate:\x20Thu,\x2009\x20Jan\x202020\x2015:34:03\x20GMT\x0D\x0AServer:\x20Python/3.7\x20aiohttp/3.6.1\x0D\x0A\x0D\x0ABad\x20Request

in log Hass.io:

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 275, in data_received
    messages, upgraded, tail = self._request_parser.feed_data(data)
  File "aiohttp\_http_parser.pyx", line 523, in aiohttp._http_parser.HttpParser.feed_data
aiohttp.http_exceptions.BadStatusLine: invalid HTTP method

Please try the curl request from a command line if you can ssh into the machine that is running HA to verify that it works.

yes, from putty in WinSCP its work.

Ok, so its just some settings that need to be adjusted. For instance, the network method should be http and the port should be 80 in the driver information box. Please also post a screen shot with the “edit function” windows closed so I can see the other available options.

And what options are available in the “network method” list?

No, you don’t need to add this.

I understand.

TCP or UDP

Isn’t port 8123 necessary to specify?

It’s only tomorrow, I already left the office.

Hello.

Edit Info

Edit Functions

I think you need to find out the exact request received by HA to find the error. You should increase the logging level in HA.

You can do this in configuration.yaml:

logger:
  default: debug

Then run the command again and have a look in the log.

Thank you, I will try this and let you know.

Hello.

I add this in configuration.yaml :

logger:
  default: debug

command (work from Putty)

curl -X POST -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI3OGUyZjkyNTc0NGE0MjcxOWY0MTIwNDQxOWZmOWFmNCIsImlhdCI6MTU3ODIxOTU2OCwiZXhwIjoxODkzNTc5NTY4fQ.TEvEUieMyT1GYu6F9XCiDEFJI3QxIa8G9wEtnMTLsxQ" -H "Content-Type: application/json" -d '{"entity_id": "switch.switch_1gang"}'  http://192.168.1.4:8123/api/services/switch/toggle

but nothing new in logs.

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 275, in data_received
    messages, upgraded, tail = self._request_parser.feed_data(data)
  File "aiohttp\_http_parser.pyx", line 523, in aiohttp._http_parser.HttpParser.feed_data
aiohttp.http_exceptions.BadStatusLine: invalid HTTP method