MyStrom Switch Token Support

Heho
I didn’t found any information about the token support for the MyStrom Switch v2.
Is it possible to implement it or is it already?

My plan is to protect the MyStrom Switch with a token and to disable the integrated webinterface. So nobody can control it without HA.

Just ran into the same issue. While i am a complete newbie with home assistant i found a quick fix to get access using the token.

I installed Home Assistant on Raspberry Pi OS via Home Assistant Core so i have access to the Home Assistant files.

I changed
/srv/homeassistant/lib/python3.7/site-packages/pymystrom/__init__.py

from line 26:

    """Handle a request to the myStrom device."""
    headers = {
        "User-Agent": USER_AGENT,
        "Accept": "application/json, text/plain, */*"
    }

to:

    """Handle a request to the myStrom device."""
    headers = {
        "User-Agent": USER_AGENT,
        "Accept": "application/json, text/plain, */*",
        "Token": "Put_my_token_here"
    }

After a restart it works using the Token.

Today i installed the Home Assistant Operating System on a Raspberry 4 and got it working as well.

A bit more of an effort:

create the folder
/config/custom_components/
if there are no custom components already.

download or git-clone https://github.com/home-assistant/core
i downloaded the zip from https://github.com/home-assistant/core/archive/dev.zip
then extracted the contents of the mystrom folder
/homeassistant/components/mystrom
from the zip-file into a folder mystrom2

download or git-clone https://github.com/home-assistant-ecosystem/python-mystrom
i downloaded the zip from https://github.com/home-assistant-ecosystem/python-mystrom/archive/master.zip
then extracted the folder pymystrom folder from the zip-file into the mystrom2 folder
and renamed it to pymystrom2

The mystrom2 folder should look like this now:

mystrom2/
    pymystrom2/
        __init__.py
        bulb.py
        cli.py
        discovery.py
        exceptions.py
        pir.py
        switch.py
    __init__.py
    binary_sensor.py
    const.py
    light.py
    manifest.json
    switch.py

Now edit const.py:

2: DOMAIN = "mystrom2"

light.py:

4: from .pymystrom2.bulb import MyStromBulb
5: from .pymystrom2.exceptions import MyStromConnectionError

switch.py:

4: from .pymystrom2.exceptions import MyStromConnectionError
5: from .pymystrom2.switch import MyStromSwitch as _MyStromSwitch

change values in manifest.json:

"domain": "mystrom2"
"name": "myStrom2"
"requirements": []
and add:
"version": "2"

in pymystrom2/init.py change:

27:
headers = {
    "User-Agent": USER_AGENT,
    "Accept": "application/json, text/plain, */*",
    "Token": "PUT_TOKEN_HERE"
}

in pymystrom2/cli.py:

8: from .bulb import MyStromBulb

Upload the folder to /config/custom_components/,
I installed “SSH & Web Terminal” and uploaded via SFTP.

Edit configuration.yaml:

switch:
  - platform: mystrom2
    host: SWITCH_IP
    name: "myStrom Switch 1"
  - platform: mystrom2
    host: SWITCH_IP
    name: "myStrom Switch 2"
  - platform: mystrom2
    host: SWITCH_IP
    name: "myStrom Switch 3"

Reboot.

I tried first without token, and after the custom component itself worked, i set up the mystrom tokens. If no Token is set up, Token header is ignored.