npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

switchbee-api-package

v1.0.2

Published

A Package to use SwitchBee's Open Api

Downloads

5

Readme

switchbee-api-package

Downloads Version

This is a SwitchBee api package that uses the open api to manage and read/log energy statistics. The package helps users to avoid any tedious coding or error checking. 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-Authentication

The users are authenticated by email and password and passed a JSON Web Token (JWT) - RFC 7519 that is valid for one day. To refresh the tokens just run the login function again.

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);

Energy

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