Skip to main content

Asynchronous Python API client for interacting with myStrom devices

Project description

Asynchronous Python API client for interacting with myStrom devices.

This module is not official, developed, supported or endorsed by myStrom AG. For questions and other inquiries, use the issue tracker in this repo please.

Without the support of myStrom AG it would have taken much longer to create this module which is the base for the integration into Home Assistant. myStrom AG has provided hardware. Their continuous support make further development of this module possible.

Requirements

You need to have Python installed.

  • myStrom device (bulb, plug or button)

  • The python-mystrom requirements

  • Network connection

  • Devices connected to your network

Installation

The package is available in the Python Package Index .

$ pip3 install python-mystrom

On a Fedora-based system or on a CentOS/RHEL machine which has EPEL enabled.

$ sudo dnf -y install python3-mystrom

Plug/switch

At the moment the following endpoints are covered according https://api.mystrom.ch:

  • /report: for getting the current state and the power consumption

  • /relay: for setting the relay state

You will still be able to use your device with the smartphone application, curl or other tools. The samples below shows how to use the switch with httpie and curl along with python-mystrom.

$ http http://IP_ADDRESS_PLUG/report
HTTP/1.1 200 OK
Content-Length: 39
Content-Type: application/json
Date: Mon, 15 Feb 2016 17:52:21 GMT

{
    "power": 51.630947,
    "relay": true
}
$ curl -X GET http://IP_ADDRESS_PLUG/relay?state=1

Bulb

If the bulb is on then you should be able to retrieve the current state of the bulb.

Browse to http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB or use a command-line tool.

$ curl -d "color=0;0;100" -d "action=on" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB
{
    "5DFF7FAHZ987":         {
            "on": true,
            "color": "0;0;100",
            "mode": "hsv",
            "ramp": 100,
            "notifyurl": ""
        }
}

The bulbs are not able to handle payload formatted as JSON. It’s required to use application/x-www-form-urlencoded. Keep that in mind if something is not working, especially around setting the color with HSV.

If you are planning to use your bulbs with Home Assistant set the bulb to a state from Colors with the app or use the command below.

$ curl -d "color=0;0;100" IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB

Set State

You can set the state with a POST request and a payload.

  • on: curl -d "action=on" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB

  • off: curl -d "action=off" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB

  • toggle: $ curl -d "action=toggle" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB

Set Color RGB

One of the supported modes for setting the color is RBG.

  • white: $ curl -d "color=FF000000" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB

  • red: $ curl -d "color=00FF0000" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB

  • green: $ curl -d "color=0000FF00" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB

  • blue: $ curl -d "color=000000FF" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB

Set Color HSV (Hue, Saturation, Value)

It’s also possible to use HSV.

$ curl -d "color=0;0;100" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB

While “color=” is composed with hue, saturation, and value.

Set Mono (white)

If you only want to set the “white” color of the bulb, use mono.

$ curl -d "color=10;100" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB

“color=” contains the value for the color temperature (from 1 to 18) and the brightness (from 0 to 100).

Dimming (ramp)

Add ramp and an interval to set up the transition time while changing colors.

$ curl -d "action=on&ramp=1000&color=00FF0000" http://IP_ADDRESS_BULB/api/v1/device/MAC_ADDRESS_BULB

The unit of measurement for ramp is milliseconds (ms).

Button

The buttons can be set with the myStrom app or directly via HTTP requests.

To set the configuration the payload must contains the relevant details for the actions:

$ curl -v -d "single=<url>&double=<url>&long=<url>&touch=<url>" http://IP_ADDRESS_BUTTON/api/v1/device/MAC_ADDRESS_BUTTON

Available actions:

  • single: Short push (approx. 1/2 seconds)

  • double: 2x sequential short pushes (within 2 seconds)

  • long: Long push (approx. 2 seconds)

  • touch: Touch of the button’s surface (only affective for the WiFi Button +)

The button is set up to extend the life span of the battery as much as possible. This means that only within the first 3 minutes or when connected to an USB port/USB charger and the battery is not full, the button is able to receive configuration information or publish its details.

mystrom helper tool

The command-line tool mystrom can help to set up the buttons and get the details from bulbs and plugs.

$ mystrom
Usage: mystrom [OPTIONS] COMMAND [ARGS]...

  Simple command-line tool to get and set the values of a myStrom devices.

  This tool can set the targets of a myStrom button for the different
  available actions single, double, long and touch.

Options:
  --version  Show the version and exit.
  --help     Show this message and exit.

Commands:
  bulb    Get and set details of a myStrom bulb.
  button  Get and set details of a myStrom button.
  config  Get and set the configuration of a myStrom...

The examples shows how to get the details of a given bulb.

$ mystrom config read
IP address of the myStrom device: IP_ADDRESS_BULB
MAC address of the device: MAC_ADDRESS_BULB
Read configuration from IP_ADDRESS_BULB
{
   'MAC_ADDRESS_BULB':{
      'type':'rgblamp',
      'battery':False,
      'reachable':True,
      'meshroot':False,
      'on':True,
      'color':'191;90;14',
      'mode':'hsv',
      'ramp':100,
      'power':0.953,
      'fw_version':'2.25'
   }
}

Example usage of the module

The sample below shows how to use this Python module.

import pymystrom

plug = pymystrom.MyStromPlug('IP_ADDRESS_PLUG')

# Preserve state
STATE_ON = plug.get_relay_state()

# Switch relay on if the plug is currently off
if not STATE_ON:
    print("Relay will be switched on")
    plug.set_relay_on()
    # Wait a few seconds to get a reading of the power consumption
    print("Waiting for a couple of seconds...")
    time.sleep(10)

# Get the new state of the switch
print("Relay state:", plug.get_relay_state())
print("Power consumption:", plug.get_consumption())

# Switch relay off if it was off.
if not STATE_ON:
    plug.set_relay_off()

Examples for the bulb can be found in the directory examples.

License

python-mystrom is licensed under MIT, for more details check LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-mystrom-2.0.0.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

python_mystrom-2.0.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file python-mystrom-2.0.0.tar.gz.

File metadata

  • Download URL: python-mystrom-2.0.0.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.1.3 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for python-mystrom-2.0.0.tar.gz
Algorithm Hash digest
SHA256 f24c0225a18576531738cb574c22beeac228a619d3d77b3e6aad669e449f0d14
MD5 66d74ce960322bc62768aa3bd3579554
BLAKE2b-256 0a8ed850386c7e9f375b658a466562fdb8db978bee930b1ec75b8540e3e1ffa7

See more details on using hashes here.

File details

Details for the file python_mystrom-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: python_mystrom-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.1.3 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for python_mystrom-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0a8f7b690f83eeadbeb0344393a7cbdf60bfd23832784634a00a4d8ba7afd244
MD5 41ea2ff6e281e35b7d927e5ba63d077a
BLAKE2b-256 9e14a17854e964344efc87a38b0da0a31e09f86dbc56ac9c70fd300563cb63f6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page