[APP][Pro] ESPhome - Control your ESP8266/ESP32 by simple configuration files

@Peter_Kawa The new Wizard is live, enjoy!

1 Like

Thanks!! Playing with it, filed some issues @ Git :wink:
The wiki is awesome, hats off!

Concerning retrieving the bearer token, I found a neat scrip here.
Adjusted it a little bit, for when one uses 2FA authentication.

View Script
// Retrieve my Bearer token (access token) -> gets available in Homeyscript tag 'bearertoken'
//
// 2FA specific info: 
// When using 2FA with your account: Enter your 2FA code by means of a (flow card) argument, otherwise ignore it
const twofaKey = args[0] ?? ''; 
//
// -------- o - Configure these parameters -------- o -------- o 
//
// About configuring:
// A client_id and client_secret can be found at https://tools.developer.homey.app/api/projects
// Your cloudid can be found right after this URL part https://my.homey.app/homeys/
// or here https://tools.developer.homey.app/tools/system 
// or here https://my.homey.app -> cogwheel -> System -> General
// 
let email = 'Your_Homey_emailaddress'
// password: With 2FA enabled, just append the 6-figure number to your pw
// use args[0] to be able to enter it as flow argument
let password = 'your_Homey_password' + twofaKey  ////Don't touch the "+ twofaKey" part pls!
let client_id = 'client_id'
let client_secret = 'client_secret'
// redirect_url: With 2FA enabled, use 'http://localhost/oauth2/callback', otherwise use 'http://localhost/'
let redirect_url = 'http://localhost/oauth2/callback'
let cloudid = 'your_cloud_id'
// -------- o -------- o -------- o -------- o -------- o 

const between = function(str, strf, strt) {
    return str.split(strf).pop().split(strt)[0].trim();
}

const authurl = 'https://accounts.athom.com/login'
console.log("POST authentication " + authurl)
const response2 = await fetch(authurl, {
  "headers": {
    "accept": "application/json, text/javascript, */*; q=0.01",
    "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
  },
  "referrerPolicy": "no-referrer-when-downgrade",
  "body": 'email=' +encodeURIComponent(email) + '&password=' + encodeURIComponent(password) + '&otptoken=',
  "method": "POST",
  "mode": "cors",
  "credentials": "omit"
})
const body2 = await response2.text()
const token = JSON.parse(body2)

const authorizeurl = 'https://accounts.athom.com/oauth2/authorise?client_id=' + client_id + 
    '&redirect_uri=' + encodeURIComponent(redirect_url) + '&response_type=code&user_token=' + token.token


console.log(" Response from accounts.athom.com/login ", body2)
console.log("GET Authorization " + authorizeurl)

const response3 = await fetch(authorizeurl, {
  "headers": {
  },
  "method": "GET",
  "mode": "cors",
  "credentials": "include"
})
const body3 = await response3.text()
let csrf = between(body3, 'name="_csrf" value="', '">')
let cookiecsrf = null;
let raw = response3.headers.raw()['set-cookie'];
for (let cookie of raw) {
  if (cookie.startsWith('_csrf=')) {
    cookiecsrf = cookie.match(/=(.+?);/)[1];
    break;
  }
}

let cookie4 = '_csrf=' + cookiecsrf
// console.log("Cookie4", cookie4)
console.log(" CSRF input parameter", csrf)
console.log(" CSRF cookie", cookiecsrf)

let authorizeurl2 = 'https://accounts.athom.com/authorise?client_id=' + client_id +   '&redirect_uri=' + encodeURIComponent(redirect_url) + '&response_type=code&user_token=' + token.token
console.log("GET Authorization", authorizeurl2)
const response4 = await fetch(authorizeurl2, {
  "headers": {
    "content-type": "application/x-www-form-urlencoded",
    "cookie": cookie4
  },
  "redirect": "manual",
  "body": "resource=resource.homey." + cloudid + "&_csrf=" + csrf + "&allow=Allow",
  "method": "POST",
  "mode": "cors",
  "credentials": "include"
});

const body4 = await response4.text()

let code = response4.headers.get('location').split('=')[1]

console.log(" Response from authorization. Redirect to ", response4.headers.get('location'))
console.log(" Response content ", body4)
console.log(" Parsed the following code ", code)



let tokenendpoint = 'https://api.athom.com/oauth2/token'
console.log("POST token (resolve code to token) " + tokenendpoint)
const response5 = await fetch(tokenendpoint, {
  "headers": {
    "content-type": "application/x-www-form-urlencoded",
  },
  "body": 'client_id=' + encodeURIComponent(client_id) +  '&client_secret=' + encodeURIComponent(client_secret) + 
    '&grant_type=authorization_code&code=' + encodeURIComponent(code),
  "method": "POST",
  "mode": "cors",
  "credentials": "include"
});


//console.log("Response5", response5)
const body5 = await response5.text()
let accesstoken = JSON.parse(body5)





let delegationEndpoint = 'https://api.athom.com/delegation/token?audience=homey'
const response6 = await fetch(delegationEndpoint, {
  "headers": {
    "content-type": "application/x-www-form-urlencoded",
    "authorization": "Bearer " + accesstoken.access_token
  },
  "referrerPolicy": "no-referrer-when-downgrade",
  "body": "client_id=" + client_id + " &client_secret=" + client_secret + "&grant_type=refresh_token&refresh_token=" + accesstoken.refresh_token,
  "method": "POST",
  "mode": "cors",
  "credentials": "include"
});



const body6 = await response6.json()
console.log(" JWT token is " + body6)

let endpoint7 = 'https://' + cloudid + '.connect.athom.com/api/manager/users/login'
console.log("POST login endpoint " + endpoint7)
const response7 = await fetch(endpoint7, {
  "headers": {
    "content-type": "application/json",
    //"authorization": "Bearer " + accesstoken.access_token
  },
  "body": JSON.stringify({"token": body6}),
  "method": "POST"
});

const body7 = await response7.json()
console.log(" Response status " + response7.status)
console.log(" Response: " + body7)

await tag("bearertoken", body7)
return true

Well, this script require using homey script… I’m not convinced it’s easier than using F12 …
At least for most users, it will not make it easier :slight_smile:

Let’s fovus on the issues you met first, improvements will come later …

Thanks, it was just a thought, and I’m not saying it’s a priority :wink:
Nevermind, I just had some ideas of how it can be integrated (in the future) into the app.
But there’s this app which has it integrated already, just to show it’s possible, and just as an idea. Not wanting to push anything.

Thanks for the fix. It seems to work pretty well now. Changing the zone using the bearer token works as well. It feels natural working with it. You improved the wizard big time. Also your wiki is stunning :wink:

1 Like

No problem, it’s just as I release a big update, I need focus on issues first :slight_smile:
Happy yours is fixed!

Do not hesitate to open an enhancement request in the github about this bearer token subject.
To be honest, I’m unsure how easier it will make it for the users :slight_smile:

1 Like

Thanks, Baldhor. Oh, I couldn’t find info yet what’s possible with the bearer token added, besides zone edit? Or is it on the road map? Just curious :face_with_hand_over_mouth:

Yes, move the device, rename device and delete device

I just upgraded all my heater devices from tasmota to ESPhome, so handy to create so many device from the Wizard in one step :slight_smile:

Just input the bearer token, and you can choose the zone of each device so easily …

Yes I know, I’m the dev of this app but I never had time to do it lol
I removed tasmota app, MQTT app, and about 40 flows! :joy:
Just in time for winter!

1 Like

Hi @Baldhor, Thanks for the ESPHome app.

I’ve just connected with your updated guide a physical and then virtual ESPhome device.
All 6 expected sensor values are found. But i’m missing a “light” entity for this specific device. How can i manually add this?

I connected this to homey: https://www.tindie.com/products/hjhickinson/orconwificontroller-for-orcon-mvs-15/

Thank you for you response

Hi Woetski,

What if you set the device class to “Light”?
Can’t test it, but it seems logical to me:

Hi Peter, thanks for response.

I already tried that, unfortunately doesn’t change or add entities. Still gives 6 instead of expected 7.
Funny part is, when reading the log from the physical device in the ESPhome app in homey. I can see the 7th missing entity, I just can’t find / select this in the “manage virtual capabilities”

1 Like

I digged a little through the product pages, imho the RPM / Speed entity is not a light, but a slider.

Maybe the lightbulb icon on this example confuses.

I can adjust several settings from Homey with either ‘target_temperature’ or ‘dim’.
Shouldn’t that also be possible with the RPM slash Speed setting?

20231127_195031

2 Likes

I will try to take a look tonight, but please open github tickets if possible, it will make it much more easier :wink:

1 Like

Thans for opening an isuee in the github: [Add entity to fan class] OrconWifiController · Issue #75 · Baldhor/Homey-ESPhome-Enhanced · GitHub

Yes I need to implement it, should be easy once you sent me all the logs :slight_smile:

Just for info, the “esphome components” implemented in the app right now are:

  • BinarySensor
  • Button
  • Climate
  • Cover
  • Number
  • Select
  • Sensor
  • Switch
  • TextSensor

To implement the others, I need logs or money to buy devices :slightly_smiling_face:

edit: updated list of supported components to include the sensors.
I also added the list to the first post.

2 Likes

Support for Light onoff and brightness has been implemented.

1 Like

Can someone explain to me why the insight part is not visible on every device.
I did set up two usb dongle in a carrier airco one I have the insight part to log the current temp and desire temp on the other usb there is nothing visible.
I don’t know if it’s a setting or something else.

Maybe it’s possible the insert the option climate/airconditioning?

For a new user I stil find new stuff to set up to homey :innocent:.

insight can be enabled / disabled from the wizard, maybe you disabled it by “accident”?
You can just go edit the virtual device to check it out and modify it.

If it doesn’t come from that, then to be honest, I have no idea :slight_smile:

Thanks baldhor, dont know why but i can see it again. dint change anything or anything else but happy again :stuck_out_tongue:

@Baldhor first off kudo’s for starting this ESP app and efforts you put in! :clap: Great extension which opens homey up to a lot more capabilities.

I used ESP on SmartThings and want to migrate this to Homey (pro 2019). I would like to connect some air quality sensors and control a servo with PWM.

Please don’t take mind me asking; what is the advantage or difference with ESP Easy app? I see this one has not been updated since 2022 and not yet been ported to the new homey pro, but it does have support for a lot of devices. ESP Easy App voor Homey | Homey