1.0.1 • Published 3 years ago

switchbee-api-rest v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

switchbee-api-package

Downloads Version

This is a SwitchBee api package that uses the open api. The package lets you connect to multiple (or single) central units and recieve data from them simultaneously.

Table of contents


Requirements

   

check with: node -v & view CU version in app and update if needed


Installation

Recommended usage with typescript (package includes types)

To install the api package run

npm i switchbee-api-package

or with yarn run

yarn add switchbee-api-package

Usage

The two parts of this api package are the find method which find central units on the network by broadcasting with a udp socket. The second part is the api class which deals with connection/commands to a single central unit or multiple

Import

import {api,find} from 'switchbee-api-package'

Find Central Units on Network

(async () => {
    try {
        const res = await find();
        console.log(res)
    } catch (e) {
        console.log(e)
    }
})();

Result:

[
  {
    ip: { address: '192.168.2.119', family: 'IPv4', port: 8872, size: 215 },
    details: {
      CUVersion: '1.4.2(3)',
      name: 'Smart Home',
      timeZone: 120,
      timeZoneName: 'Asia/Jerusalem',
      timeStr: '02-01-2021 19:17',
      mac: 'A7-00-00-11-32-99',
      ip: '1.1.1.1',
      port: 23789,
      NoUsers: false,
      switches: 4
    }
  }
]

Control Central Units on Network

First initalize api

const swApi = new api(res,"username","password");

Optional Paramter tokens [] can be passed if you want to use an existing session note tokens last only one day

const swApi = new api(res,"username","password",['fdadd481dadc482c825061ff25bafbea' 
 'fdadd481dadc482c825061ff25bafbea']);

List of Commands

export enum COMMANDS {
  LOGIN = "LOGIN",
  GETA = "GET_CONFIGURATION",
  OPERATE = "OPERATE",
  GET_STATE = "GET_STATE",
  GET_MULTIPLE_STATES = "GET_MULTIPLE_STATES",
  STATS = "STATS",
}

Examples

Login

swApi.login(res => console.log(res)).catch(err => console.log(err))

Result:

{
  status: 'OK',
  data: {
    token: 'fdadd481dadc482c825061ff25bafbea',
    expiration: 1609534763368
  }
}

Get Configuration

swApi.getAll(res => console.log(res)).catch(err => console.log(err))

Result:

[
    {
        "status": "OK",
        "data": {
            "mac": "00-A4-00-71-18-10",
            "name": "Smart Home",
            "version": "1.4.2(3)",
            "lastConfChange": 1609545227493,
            "zones": [
                {
                    "name": " Main Room",
                    "items": [
                        {
                            "id": 11,
                            "name": "Lights",
                            "hw": "DIMMABLE_SWITCH",
                            "type": "SWITCH"
                        },
                        {
                            "id": 21,
                            "name": "Shutter",
                            "hw": "SHUTTER",
                            "type": "SHUTTER"
                        }
                    ]
                },
                {
                    "name": "Second Room",
                    "items": [
                        {
                            "id": 41,
                            "name": "Aircon",
                            "hw": "SOCKET_IR",
                            "type": "SWITCH"
                        },
                        {
                            "id": 38,
                            "name": "TV",
                            "hw": "SOCKET_IR",
                            "type": "SWITCH"
                        }
                    ]
                }
            ]
        }
    }
]

Get All states

swApi.getState(res => console.log(res)).catch(err => console.log(err))

Result:

[
  { id: 11, mac: '36-A2-00-21-49-20', state: 'OFF'},
  { id: 21, mac: '36-A2-00-21-49-20', state: 'OFF' },
  { id: 41, mac: '36-A2-00-21-49-20', state: 'ON' },
  { id: 38, mac: '36-A2-00-21-49-20', state: 'OFF' }
]

This method can be polled to give a live state for example:

setInterval(() => {
              swApi.getState(res => console.log(res)).catch(err => console.log(err))
              }, 1500);

Stats

swApi.energy(res => console.log(res)).catch(err => console.log(err))
1.0.1

3 years ago

1.0.0

3 years ago