Code Monkey home page Code Monkey logo

Comments (9)

goodsale avatar goodsale commented on August 30, 2024

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       

from ha-switchbot-remote.

johnuopini avatar johnuopini commented on August 30, 2024

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

from ha-switchbot-remote.

KiraPC avatar KiraPC commented on August 30, 2024

@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

from ha-switchbot-remote.

goodsale avatar goodsale commented on August 30, 2024

@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/

from ha-switchbot-remote.

johnuopini avatar johnuopini commented on August 30, 2024

@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

from ha-switchbot-remote.

johnuopini avatar johnuopini commented on August 30, 2024

@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')

from ha-switchbot-remote.

joshepw avatar joshepw commented on August 30, 2024

@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.

from ha-switchbot-remote.

joshepw avatar joshepw commented on August 30, 2024

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

from ha-switchbot-remote.

jebentancour avatar jebentancour commented on August 30, 2024

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.

from ha-switchbot-remote.

Related Issues (20)

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.