Coder Social home page Coder Social logo

homeassistant-switchbot-api's Introduction

Home Assistant - Switchbot API

Connect Home Assistant to Switchbot API

SECRETS.YAML

switchbot_meter1_status_url: "https://api.switch-bot.com/v1.0/devices/<DEVICE ID>/status"
switchbot_contactsensor1_status_url: "https://api.switch-bot.com/v1.0/devices/<DEVICE ID>/status"
switchbot_api: <TOKEN>
switchbot_lightstrip_deviceId: <DEVICE ID>

CONFIGURATION.YAML

# CONNECT TO API
rest_command:
  switchbot_device_command:
    url: 'https://api.switch-bot.com/v1.0/devices/{{ deviceId }}/commands'
    method: post
    content_type: 'application/json'
    headers:
      Authorization: !secret switchbot_api
    payload: '{"command": "{{ command }}","parameter": "{{ parameter }}","commandType": "{{ commandType }}"}'

# SWITCHBOT LIGHTSTRIP
light:
  - platform: template
    lights:
      switchbot_lightstrip:
        friendly_name: Ruban chambre
        unique_id: Ruban chambre
        temperature_template: "{{states('input_number.temperature_input') | int}}"
        color_template: "{{ rgb_color.split(',') }}"
        turn_on:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_lightstrip_deviceId
            command: "turnOn"
        turn_off:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_lightstrip_deviceId
            command: "turnOff"
        set_level:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_lightstrip_deviceId
            command: "setBrightness"
            parameter: "{{brightness}}"
        set_color:
          service: rest_command.switchbot_device_command
          data:
            supported_color_modes:
              - color_temp
              - RGB
            color_mode: RGB
            deviceId: !secret switchbot_lightstrip_deviceId
            command: "setColor"
            parameter: "{{ rgb_color[0] }}:{{ rgb_color[1] }}:{{ rgb_color[2] }}"

# SWITCHBOT CONTACT SENSOR
binary_sensor:
  - platform: rest
    name: 'Contact Sensor 1 JSON'
    resource: !secret switchbot_contactsensor1_status_url
    method: GET
    # Refresh every 30 seconds
    scan_interval: 30
    headers:
      Authorization: !secret switchbot_api
      Content-Type: 'application/json'
    value_template: '{{ value_json.body }}'
  - platform: template
    sensors:
      switchbot_contactsensor1_openstate:
        friendly_name: "Contact sensor 1 Open State"
        value_template: '{{ states.sensor.meter1_json.attributes["openState"] }}'
        device_class: "opening"

# SWITCHBOT METER
sensor:
  - platform: rest
    name: 'Meter1 JSON'
    resource: !secret switchbot_meter1_status_url
    method: GET
    # Refresh every 5 minutes
    scan_interval: 300
    headers:
      Authorization: !secret switchbot_api
      Content-Type: 'application/json'
    value_template: '{{ value_json.body }}'
    json_attributes_path: "$.body"
    json_attributes:
      - deviceId 
      - deviceType
      - hubDeviceId
      - humidity
      - temperature
  - platform: template
    sensors:
      switchbot_meter1_temp:
        friendly_name: "Meter1 Temperature"
        value_template: '{{ states.sensor.meter1_json.attributes["temperature"] }}'
        unit_of_measurement: "°C"
        device_class: "temperature"
      switchbot_meter1_humidity:
        friendly_name: "Meter1 Humidity"
        value_template: '{{ states.sensor.meter1_json.attributes["humidity"] }}'
        unit_of_measurement: "%"
        device_class: "humidity"

homeassistant-switchbot-api's People

Contributors

littlej avatar

Stargazers

 avatar

Watchers

 avatar

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.