Giter VIP home page Giter VIP logo

switchbot-api-script-caller's Introduction

License All rights to the API belong to OpenWonderLabs.

⚠️⚠️⚠️⚠️ Attention following the creation of the official HomeAssisant integration I have decided that, if it is necessary, I will work to bring any features into the said integration, so unless something new comes along, this repository will soon become discontinued ⚠️⚠️⚠️⚠️

SwitchBot API Script Caller (v 0.2.1)

This (Py)Script allows you to control all (WIP) your SwitchBot devices via API calls (1.1).

‼️‼️At the moment we have implemented all the devices that we can test, so if you want to be a tester please open an issue with "test" in the title or if you are also able to develop go ahead and do a PR‼️‼️

For more info click here

Requirements

  • HACS (docs)
    • PyScript Integration (docs)

Installation

⚠️ If you update from one of the following versions there have been breaking changes:

Versions

Procedure

  1. Clone this repository in your config folder
    cd /config
    git clone https://github.com/SiriosDev/SwitchBot-API-Script-Caller.git
  2. Include pyscript/switchbot.yaml in your pyscript/config.yaml under the switchbot section
    # /config/pyscript/config.yaml
    allow_all_imports: true
    apps:
    # (...)
    # ↓↓↓ attention indentation
     switchbot: !include /config/SwitchBot-API-Script-Caller/pyscript/switchbot.yaml
    # (...)
  3. Set the authentication secrets in secrets.yaml homeassistant file
    • Random Value (switchbot_nonc) (I suggest using an UUID generator, but any unique alphanumeric string is fine)
    # secrets.yaml
    # (...)
    # Token and Secret Key : from `Developer Option` in the SwitchBot App (version ≥6.14)
    switchbot_token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    switchbot_sec: xxxxxxxxxxxx
    # Random Value: you can use a UUID generator, but any unique alphanumeric string is OK
    switchbot_nonc: xxxxxxxxxx
  4. Link the files in the pyscript directory
    # use `mkdir -p /config/pyscript/apps/` if the directory doesn't exist
    cd /config/pyscript/apps/
    
    # Create a symbolic link to the apps directory named switchbot
    ln -s /config/SwitchBot-API-Script-Caller/pyscript/apps/switchbot.py switchbot.py

Further Update

By following this procedure, the script can then be updated with newer version using git.

cd SwitchBot-API-Script-Caller
git pull

⚠️ See changelog before updating.
The project is still in developpement and breaking changes may occurs.

Installation Notes

  • In order to see the Developper options in the Switchbot app (version ≥6.14), click repetively on the version number in the App's settings.

    Click here for detailed procedure

    SwitchBot

  • A symbolic link is symbolic and represent the exact path you enter, if you move the targeted file or if the target is outside of the container (e.g. when using docker) the link will not work. Make sure that you are using a relative path that is accessible for the host reading the link.

  • Ensure that pyscript is operational before to install this script.

  • Except dirs strictly related to pyscript, all others dir are recommended, so organize them as you like, keeping in mind that changing the contents of the "clone", could cause the update via git pull to fail.

How To Use

This script (for now) provides the following services in home assisant. It is important to execute SwitchBot Refresh Devices first in order to be able to use the other features, as it will generate the required Home Assistant entities for your devices.

Summary

🔸SwitchBot Refresh Devices

Create Home Assistant switch entity for each IR Device connected with your SwitchBot Hubs. Devices are stored as switch.switchbot_remote_<device_name>.
<device_name> correspond to the name of the device in the SwitchBot app.
if <device_name> doesn't contains Alphanum characters (e.g is written in another alphabet), it is replaced by <deviceType>_<deviceId[-4:]> (e.g. switch.switchbot_remote_light_0D62)
The entities can then be used for sending commands using other functions of this pyscript. ⚠️ Not working stand alone ⚠️ In case the device doesn't exist in the future, you will be notified on your devices.

Parameters: None

🔸SwitchBot Turn On

Turn a device ON

Parameters:

🔸SwitchBot Turn Off

Turn a device OFF

Parameters:

🔸SwitchBot IR HVAC Control

Interface for infrared HVAC (heating, ventilation and air conditioning) device.

Parameters:

  • device
  • temperature:
    • int value from 16 to 30
  • mode:
    • int value between 1 (auto), 2 (cool), 3 (dry), 4 (fan), 5 (heat)
  • fan_speed:
    • int value between 1 (auto), 2 (low), 3 (medium), 4 (high)
  • state:
    • string value between on and off

🔸SwitchBot IR Light Control

Interface for infrared Light (turnOn, turnOff, brightnessUp and brightnessDown) device.

Parameters:

  • device
  • command:
    • string value between turnOn, turnOff, brightnessUp and brightnessDown
  • steps:
    • int value from 1 to 10, only works with brightnessUp/Down, iterates the command as many times as selected.

🔸SwitchBot Generic Command

Allows you to send any request to the API. (See documentation)

Parameters:

  • device
  • command:
    • One of the command supported by the device. (see documentation)
  • parameter: (optional)
    • Parameter for the command, if required (e.g. SetChannel)
    • use default if not used
  • commandType:
    • command for standard commands
    • customize for custom commands

Work in Progress

The script works fine, but everything is still WIP, including this file. For any problems open an Issue, (soon I will insert a template for that).

Changelog

2023.02.19 v0.2.1 (🛠️ Some Fixes)

Fixed (#15) commandType parameters in Generic Command.
Suggest updating if you need to control custom remotes created in the mobile app.

2023.01.16 v0.2.0 (🟢 New Features and 🛠️ Some Fixes)

Add service SwitchBot IR Light Control:
Send command via infrared to light device.

Corrected some descriptions.

Reworked the way Refresh Devices assigns Friendly Names.

Removed notifications to all channels in case of errors during Refresh Devices.

Now HVAC will have a dedicated icon once the dummy switch is created (Refresh Devices).

Renamed SwitchBot HVAC API Interface in SwitchBot IR HVAC Control: it doesn't affect function it's just a visual thing.

Renamed SwitchBot Generic Command API Interface in SwitchBot Generic Command: it doesn't affect function it's just a visual thing.

2023.01.14 v0.1.0 (⚠️ Breaking changes)

Add service SwitchBot Refresh Devices:
Retrieves your IR devices from the API. Services now requires device instead of deviceId. No need to copy paste the id manually anymore.

Previously:

  • Services param was deviceId

Now:

  • Services Param is device (home assistant ID for sensor, e.g. switch.switchbot_remote_my_light)

Make sure to run SwitchBot Refresh Devices before configuring anything else.

Full Changelog History here

switchbot-api-script-caller's People

Contributors

siriosdev avatar vinalti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

switchbot-api-script-caller's Issues

developing branch: switch_curtain_command errors

Re developing branch: I made errors in the switchbot_curtain_command. Do you want me to fix it and do another pull request? It should be as follows:

@service
def switchbot_curtain_command(device=None, command=None, parameter=None):
    """yaml
name: SwitchBot Curtain Command
description: Control Switchbot curtain.
fields:
  device:
    name: Device
    description: the name of the device as in the SwitchBot app
    example: cover.switchbot_remote_bedroom_curtains
    default:
    required: true
    selector:
      entity:
        domain: cover

  command:
    name: Command
    description: the name of the command
    example: turnOff
    default: 
    required: true
    selector:
      select:
        options:
          - turnOn
          - turnOff
          - setPosition
        mode: list

  parameter:
    name: Parameters
    description: the setPosition command requires a position parameter
    example: 
    default: 
    required: false
    selector:
      number:
        min: 0
        max: 100
        step: 1
        mode: box
    """
    deviceId = extract_device_id(device)
    headers_dict = auth(**pyscript.app_config)
    if parameter != None:
      parameter_string = "0,ff," + str(parameter)
      command_execute(headers_dict, deviceId, command, parameter=parameter_string)
    else:
      command_execute(headers_dict, deviceId, command, parameter=parameter)

What am i doing wrong?

Hi i cant get HA to get the "switch.switchbot_remote_<device_name>"

This is how i installed this.

I could not find this in HACS and custom repistories did not work so i downloaded the zip from here and put the "SwitchBot-API-Script-Caller" folder in Homeassistant map "config"

I think im doing wrong at this step:

# use `mkdir -p /config/pyscript/apps/` if the directory doesn't exist
cd /config/pyscript/apps/

# Create a symbolic link to the apps directory named switchbot
ln -s /config/SwitchBot-API-Script-Caller/pyscript/apps/switchbot.py switchbot.py

How do i do that in docker-compose.yaml? i have a synology nas. this is my dockercompose fpr homeassistant;

homeassistant:
container_name: home-assistant
image: homeassistant/home-assistant:latest
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/TZ:/etc/timezone:ro
- /run/dbus:/run/dbus:ro
- /volume3/docker/homeassistant:/config
- /volume3/docker/homeassistant/SwitchBot-API-Script-Caller/pyscript/apps/:/config/pyscript/apps/
environment:
- TZ=Europe/Stockholm
Please help teally want to use my ir devices in homeassistant ! :)

Custom Commands

Hi,

looks like there is a typo in your switchbot.py.
Currently the custom commands are not woring with your integration.

You need to change following from custom to customize:

image

Or you could also change your predefined options for the Switchbot Generic Command from
customize to custom.

And also thank you very much for the integration, helped me alot! 👍 :)

Br Adrian

developing branch: gen_icon function name clash

Re 'developing' branch: I made a mistake forgetting to rename the gen_icon(non_ir) function gen_non_ir_icon(non_ir) or similar. It clashes with the gen_icon(dev) function. Alternatively, you could combine gen_icon(dev) and gen_icon(non_ir) and pass in the type key as an argument. Do you want me to fix it and do another pull request?

Seeing 'pyscript.app_config' is not defined

Hey, I've just configured this caller and have pyscript setup. When I try to call refresh_devices I see the below error.

I'm pretty sure I've got the !include to pull in the config correctly so am not entirely sure what I'm missing.

Do you have any ideas?

2023-02-03 19:11:52.204 ERROR (MainThread) [custom_components.pyscript.file.switchbot.switchbot_refresh_devices] Exception in <file.switchbot.switchbot_refresh_devices> line 106:
        headers_dict=auth(**pyscript.app_config)
                            ^
NameError: name 'pyscript.app_config' is not defined

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.