Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add information from HUB 2 - API 1.1 #7

Closed
goodsale opened this issue Jul 18, 2023 · 9 comments
Closed

Add information from HUB 2 - API 1.1 #7

goodsale opened this issue Jul 18, 2023 · 9 comments
Labels
enhancement New feature or request

Comments

@goodsale
Copy link

Via API 1.1 it is possible to query the temperature and humidity of a HUB 2 (new version from Switchbot)

It would be interesting to add Hub 2 with its data to this integration.

As a support I send the instructions via API 1.1 to be sent after authentication to create the sensor which are currently in my configuration.yaml and which work correctly.

Thank you

  - resource: https://api.switch-bot.com/v1.1/devices/HUBDEVICEID/status
    scan_interval: 300
    headers:
      Authorization: "{{ states('input_text.switchbot_token') }}"
      sign: "{{ states('sensor.switchbot_sign') }}"
      t: "{{ state_attr('sensor.switchbot_sign','t') }}"
      Content-Type: "application/json"
      nonce: ""
    sensor:
      - name: "SwitchBot Hub 2 Temperature"
        unique_id: switchbot_hub_2_temperature
        value_template: "{{ value_json.body.temperature }}"
        unit_of_measurement: "°C"
        device_class: temperature
      - name: "SwitchBot Hub 2 Humidity"
        unique_id: switchbot_hub_2_humidity
        value_template: "{{ value_json.body.humidity }}"
        unit_of_measurement: "%"
        device_class: humidity
@KiraPC KiraPC added the enhancement New feature or request label Jul 18, 2023
@KiraPC KiraPC added this to the Support for more devices milestone Jul 18, 2023
@goodsale
Copy link
Author

With the same logic I noticed that the switchbot contact_sensor doesn't work properly with the original integration via bluetooth.
I created the sensors that with API 1.1, updating every 10 seconds
I don't know if this is correct but the created sensor works correctly and provides the open close status correctly.
If it helps, I attach the instructions and these too can be included in the integration

Anyone else having issues with the switchbot contact_sensor open/close states in original integration??

  - resource: https://api.switch-bot.com/v1.1/devices/contact_sensor_ID/status
    scan_interval: 10
    headers:
      Authorization: "{{ states('input_text.switchbot_token') }}"
      sign: "{{ states('sensor.switchbot_sign') }}"
      t: "{{ state_attr('sensor.switchbot_sign','t') }}"
      Content-Type: "application/json"
      nonce: ""
    sensor:
      - name: "SwitchBot Finestra Sala Battery"
        unique_id: switchbot_finestra_sala_battery
        value_template: "{{ value_json.body.battery }}"
        unit_of_measurement: "°%"
        device_class: battery
      - name: "SwitchBot Finestra Sala brightness"
        unique_id: switchbot_finestra_sala_brightness
        value_template: "{{ value_json.body.brightness }}"
        device_class: light 
      - name: "SwitchBot Finestra Sala open-close"
        unique_id: switchbot_finestra_sala_oc
        value_template: "{{ value_json.body.openState }}"
        device_class: door       

@johnuopini
Copy link

@goodsale how did you get the auth token from the integration? i cant make this work

@KiraPC
Copy link
Owner

KiraPC commented Aug 4, 2023

@goodsale how did you get the auth token from the integration? i cant make this work

Hi, you can get the token following this https://github.com/OpenWonderLabs/SwitchBotAPI#getting-started

@goodsale
Copy link
Author

goodsale commented Aug 4, 2023

@goodsale how did you get the auth token from the integration? i cant make this work

If like me you are not very experienced with code, you can look at this link.
Once you create the basis with this method to do authentication, then you can do whatever you want.

https://www.speaktothegeek.co.uk/2023/07/switchbot-api-v1-1-and-home-assistant/

@johnuopini
Copy link

@KiraPC yeah i get it, i thought the token was exposed by the integration so i didn't have to get it outside and i could create a sensor, anyway adding Hub2 temp/humidity shouldn't be too complex their are returned by the device list in the payload

@goodsale thanks, actually i am very experienced with coding but not experienced with HA :) thats what i was looking for, i could probably just call a script instead of embedding the entire python line in the command but thanks

@johnuopini
Copy link

@goodsale P.S. this is probably cleaner to extract the data

command_line:
  - sensor: 
      name: switchbot_data
      command: "python3 /config/packages/scripts/switchbot.py"
      scan_interval: 60 
      json_attributes:
        - temperature
        - humidity

sensor:
  - platform: template
    sensors:
      switchbot_hub2_temperature:
        value_template: "{{ states.sensor.switchbot_data.attributes.temperature }}"
        unit_of_measurement: "°C"
        device_class: temperature
      switchbot_hub2_humidity:
        value_template: "{{ states.sensor.switchbot_data.attributes.humidity }}"
        unit_of_measurement: "%"
        device_class: humidity

Then the script is just

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import json
import time
import hashlib
import hmac
import base64
import uuid

# Declare empty header dictionary
apiHeader = {}
# open token
token = 'TOKEN' # copy and paste from the SwitchBot app V6.14 or later
# secret key
secret = 'SECRET' # copy and paste from the SwitchBot app V6.14 or later
nonce = uuid.uuid4()
t = int(round(time.time() * 1000))
string_to_sign = '{}{}{}'.format(token, t, nonce)
string_to_sign = bytes(string_to_sign, 'utf-8')
secret = bytes(secret, 'utf-8')
sign = base64.b64encode(hmac.new(secret, msg=string_to_sign, digestmod=hashlib.sha256).digest())

#Build api header JSON
apiHeader['Authorization']=token
apiHeader['Content-Type']='application/json'
apiHeader['charset']='utf8'
apiHeader['t']=str(t)
apiHeader['sign']=str(sign, 'utf-8')
apiHeader['nonce']=str(nonce)

def get_and_dump_data(endpoint):
    url = f'https://api.switch-bot.com/v1.1/{endpoint}'
    response = requests.get(url, headers=apiHeader)
    if response.status_code == 200:
        response_json = response.json()
        print(json.dumps(response_json['body'], indent=4))
        return response_json['body']
    else:
        print('Error')
        print(response.text)
        return None

get_and_dump_data('devices/DEVICEID/status')

@joshepw
Copy link
Collaborator

joshepw commented Oct 3, 2023

@goodsale the purpose of this package is to support IR devices.

Have you managed to try adding Hub 2 through the Matter Server Addon? This works locally without connecting to the cloud.

@joshepw
Copy link
Collaborator

joshepw commented Oct 9, 2023

@goodsale, check the new Home Assistant update, have the native Cloud API integration option

@jebentancour
Copy link

I encountered some issues with the native Home Assistant integration as it didn't meet my requirements effectively. The integration lacked sensor information and the capability to use custom IR commands, prompting me to explore alternatives.

I also connected the Hub via Matter, which provided temperature and humidity data, but unfortunately, light level information was not available. Matter, too, doesn't support the use of custom IR commands, limiting its compatibility to only my AC unit.

Additionally, I observed a lack of synchronization in the IR AC status. After inspecting the API, I couldn't find a method to retrieve this information.

Considering these challenges, I believe it would be beneficial to enhance the existing HACS integration. Specifically, incorporating features to fetch temperature, humidity, and light data through the API could significantly improve the integration's functionality. This would address the shortcomings I've experienced and make the integration more comprehensive.

@KiraPC KiraPC closed this as not planned Won't fix, can't repro, duplicate, stale May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants