-
Notifications
You must be signed in to change notification settings - Fork 3
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
Comments
With the same logic I noticed that the switchbot contact_sensor doesn't work properly with the original integration via bluetooth. Anyone else having issues with the switchbot contact_sensor open/close states in original integration??
|
@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 |
If like me you are not very experienced with code, you can look at this link. https://www.speaktothegeek.co.uk/2023/07/switchbot-api-v1-1-and-home-assistant/ |
@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 |
@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') |
@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. |
@goodsale, check the new Home Assistant update, have the native Cloud API integration option |
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. |
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
The text was updated successfully, but these errors were encountered: