Giter Club home page Giter Club logo

harestapi's Introduction

HARestAPI

An Arduino library to talk to Home Assistant using Rest API made for ESP8266.

HARestAPI

Beta

  • Can do POST effectively and not GET (reply is too long for esp8266). Try using sendGetHA(URL,Component) and provide feedback.

Using the Library

  • Download this GitHub library.
  • In Arduino, Goto Sketch -> Include Library -> Add .ZIP Library... and point to the zip file downloaded.

To use in your sketch include these lines.

#include <HARestAPI.h>

Declare clients before setup(). If using HA with no SSL declare WiFiClient and pass it to HARestAPI.

WiFiClient client;
HARestAPI ha(client);

If using HA with SSL declare WiFiClientSecure and pass it to HARestAPI.

WiFiClientSecure sclient;
HARestAPI ha(sclient);

In setup make sure to setup IP and port of HA server. Default is 8123 and if using SSL 443.

const char* ha_ip = "192.168.0.xxx";
uint16_t ha_port = 8123;
const char* ha_pwd = "HA_PASSWORD"; //long-lived password. On HA, Profile > Long-Lived Access Tokens > Create Token

ha.setHAServer(ha_ip, ha_port);
ha.setHAPassword(ha_pwd);
  
// Optional, but can use SHA1 fingerprint to confirm one is connecting to 
String fingerprint = "35 85 74 EF 67 35 A7 CE 40 69 50 F3 C0 F6 80 CF 80 3B 2E 19";
ha.setFingerPrint(fingerprint);

All the commands below do the same thing

Home Assistant

URL: "/api/services/light/turn_on" (HA -> Developer Tools -> Services -> Service)

Component: "light.bedroom_light" (HA -> Developer Tools -> Services -> Entity)

    // 1. Send custom DATA to HA
    ha.sendCustomHAData("/api/services/light/turn_on", "{\"entity_id\":\"light.bedroom_light\"}");
    // 2. Set component and send light to HA // Will set tmpURL to "/api/services/light/turn_on"
    ha.setComponent("light.bedroom_light");
    ha.sendHALight(true);
    // 3. Set light on/off as bool and component is 2nd input, Send Light to HA 
    //        Will set tmpURL to "/api/services/light/turn_on"
    ha.sendHALight(true, "light.bedroom_light");
    // 4. Set component and URL is 2nd input, Send custom URL to HA
    ha.setComponent("light.bedroom_light");
    ha.sendHAURL("/api/services/light/turn_on");
    // 5. Set URL and Send component to HA
    ha.setURL("/api/services/light/turn_on");
    ha.sendHAComponent("light.bedroom_light");
    // 6. Set URL 1st argument, component 2nd, send to HA
    ha.sendHAComponent("/api/services/light/turn_on", "light.bedroom_light");
    // 7. Set URL , Set component, send to HA
    ha.setURL("/api/services/light/turn_on");
    ha.setComponent("light.bedroom_light");
    ha.sendHA();
    // 8. Set URL and Send area to HA
    ha.setURL("/api/services/light/turn_on");
    ha.sendHAArea("bedroom");

harestapi's People

Contributors

debsahu avatar mobilgame06 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

harestapi's Issues

i have error

C:\Users\conkhidan\Documents\Arduino\libraries\HARestAPI-master\src\HARestAPI.cpp: In member function 'String HARestAPI::sendGetHA(String, String)':

C:\Users\conkhidan\Documents\Arduino\libraries\HARestAPI-master\src\HARestAPI.cpp:82:15: error: expected primary-expression before '=' token

 posturl + = "Authorization: Bearer " + _password + "\r\n";  //long-lived password

           ^

C:\Users\conkhidan\Documents\Arduino\libraries\HARestAPI-master\src\HARestAPI.cpp:83:13: error: expected primary-expression before '=' token

posturl + = "Content-Type: application/json" + "\r\n" +

         ^

C:\Users\conkhidan\Documents\Arduino\libraries\HARestAPI-master\src\HARestAPI.cpp:83:52: error: invalid operands of types 'const char [31]' and 'const char [3]' to binary 'operator+'

posturl + = "Content-Type: application/json" + "\r\n" +

                                                ^

C:\Users\conkhidan\Documents\Arduino\libraries\HARestAPI-master\src\HARestAPI.cpp: In member function 'bool HARestAPI::sendPostHA(String, String)':

C:\Users\conkhidan\Documents\Arduino\libraries\HARestAPI-master\src\HARestAPI.cpp:158:15: error: expected primary-expression before '=' token

 posturl + = "Authorization: Bearer " + _password + "\r\n";  //long-lived password

           ^

C:\Users\conkhidan\Documents\Arduino\libraries\HARestAPI-master\src\HARestAPI.cpp:159:13: error: expected primary-expression before '=' token

posturl + = "Content-Type: application/json" + "\r\n" +

         ^

C:\Users\conkhidan\Documents\Arduino\libraries\HARestAPI-master\src\HARestAPI.cpp:159:52: error: invalid operands of types 'const char [31]' and 'const char [3]' to binary 'operator+'

posturl + = "Content-Type: application/json" + "\r\n" +

                                                ^

exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).

WiFiClientSecure' has no member named 'verify'

Sadly the lib is broken :-(

.pio\libdeps\nodemcuv2\HARestAPI\src\HARestAPI.cpp: In member function 'String HARestAPI::sendGetHA(String)':
.pio\libdeps\nodemcuv2\HARestAPI\src\HARestAPI.cpp:102:21: error: 'class BearSSL::WiFiClientSecure' has no member named 'verify'
102 | if (wsclient->verify(_fingerprint.c_str(), _serverip.c_str()))
| ^~~~~~
.pio\libdeps\nodemcuv2\HARestAPI\src\HARestAPI.cpp: In member function 'bool HARestAPI::sendPostHA(String, String)':
.pio\libdeps\nodemcuv2\HARestAPI\src\HARestAPI.cpp:201:21: error: 'class BearSSL::WiFiClientSecure' has no member named 'verify'
201 | if (wsclient->verify(_fingerprint.c_str(), _serverip.c_str()))
| ^~~~~~
Compiling .pio\build\nodemcuv2\libc1a\ESP8266mDNS\LEAmDNS_Structs.cpp.o
*** [.pio\build\nodemcuv2\lib17d\HARestAPI\HARestAPI.cpp.o] Error 1

Error 'sendGetHA' was not declared in this scope

Hello,

great work and thanks a lot for that!

i have this error 'sendGetHA' was not declared in this scope

and dont understand what is wrong...the 'sendGetHA' is red, like recognize as a function,

what i am doing wrong please? i just call the function like
sendGetHA(URL, messageURL);

where URL and messageURL as define as String in global variable at the beginning of the skecth..;

If you could help,

thanks

Content-Length ignored

For reading the payload, it is necessary to read the content-length header to know the length of the payload.
The payload does not necessarily end with \r\n so you cannot read it with readString().
If the payload ist not terminated with \r\n, the program hangs at
replystr = wclient->readString();
until it gets a timeout after 1000s.

Release v0.3.2 bugs/typos?

I am a fan of using releases instead of master, but realised there are some typos?

Compile errors:

...\HARestAPI.cpp:102:53: error: expected ';' before '}' token
102 |       wsclient->setFingerprint(_fingerprint.c_str())
|                                                     ^
|                                                     ;
103 |     }
|     ~
...\HARestAPI.cpp: In member function 'bool HARestAPI::sendPostHA(String, String)':
...\HARestAPI.cpp:191:54: error: expected ';' before '}' token
191 |       wsclient-> setFingerprint(_fingerprint.c_str())
|                                                      ^
|                                                      ;
192 |     }
|     ~

After fixing those manually, everything worked fine.

Verify error

when compiling the code i got this error

C:\Users\yoann\Documents\Arduino\libraries\HARestAPI-0.3\src\HARestAPI.cpp: In member function 'String HARestAPI::sendGetHA(String)': C:\Users\yoann\Documents\Arduino\libraries\HARestAPI-0.3\src\HARestAPI.cpp:103:21: error: 'class BearSSL::WiFiClientSecure' has no member named 'verify' 103 | if (wsclient->verify(_fingerprint.c_str(), _serverip.c_str())) | ^~~~~~ C:\Users\yoann\Documents\Arduino\libraries\HARestAPI-0.3\src\HARestAPI.cpp: In member function 'bool HARestAPI::sendPostHA(String, String)': C:\Users\yoann\Documents\Arduino\libraries\HARestAPI-0.3\src\HARestAPI.cpp:200:21: error: 'class BearSSL::WiFiClientSecure' has no member named 'verify' 200 | if (wsclient->verify(_fingerprint.c_str(), _serverip.c_str())) | ^~~~~~ exit status 1 Erreur de compilation pour la carte LOLIN(WEMOS) D1 mini (clone)

connection failed

Hey @debsahu,
I would really like to use this library in a project i;m working on but i'm having no luck getting it to connect to my home assistant install.

I'm trying to run the example sketch you have and ii have filled in everything needed, WIFI details and password, Home Assistant long lived token, fingerprint, port etc, but ultimately i get a connection failed message

Here is the output in the serial console:

connecting to SKYA92CD
.......
WiFi connected
IP address:
192.168.86.19
Requesting URL: /api/services/light/toggle
Sending:
POST /api/services/light/toggle HTTP/1.1
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.*********************************************
Content-Type: application/json
Content-Length: 26

{"entity_id":"light.lamp"}
Connecting to b4zm62u2sem***************.ui.nabu.casa
connection failed
`
I'm using nabu casa, but I'm not sure that matter as I have used the same details in https://reqbin.com/ and that triggers fine

Any help you can offer here would be great.

Please let me know if you need anything further

Thank you
Joe

Memory leak

When uses sendCustomHAData every request couses heap corruption. It present in 0.3, but 0.2 ok,

integration with user & pass

Hello

I'm trying to integrate the library to esp32cam with MJPEG2SD, but I don't want to use a long term token.
I have added html and javascript to open a link like http://homeassistant.local:8123/auth/authorize?client_id="+hostURL+"&redirect_uri="+hostURL+"rest?setCode=1&state="+hostURL
With that link I identify myself with my homeassistant user account and it redirects me to the page I am preparing that recognizes the update code, but once I get the code I don't know how to get a valid token to register the device.

Couldn't you add a captive page that allows you to register the device or something similar?
or at least explain me how to register it

By the way, I had to edit the library because it didn't return anything when I received an httpCode 201

if (httpCode == 201 || httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)

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.