Adding support for Smappee

I’m trying to integrate my smappee in home assistant.
I’ve found this in github.
I use this code in my smappee.py file:

import logging
import socket
from urllib.error import URLError
from datetime import timedelta

from homeassistant.const import (
CONF_ACCESS_TOKEN, CONF_NAME, CONF_API_KEY, CONF_PASSWORD,
CONF_USERNAME, STATE_UNKNOWN)
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle

_LOGGER = logging.getLogger(__name__)

REQUIREMENTS = [
    'https://github.com/EnergyID/smappy/archive/master.zip'
    '#smappee/smappee==0.1.1']

And i get these errors:

16-05-03 12:54:45 INFO (MainThread) [homeassistant.loader] Loaded sensor.smappee from homeassistant.components.sensor.smappee
Process Process-1:
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/packaging/requirements.py", line 90, in __init__
    req = REQUIREMENT.parseString(requirement_string)
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/pyparsing.py", line 1137, in parseString
    raise exc
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/pyparsing.py", line 1127, in parseString
    loc, tokens = self._parse( instring, 0 )
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/pyparsing.py", line 1001, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/pyparsing.py", line 2412, in parseImpl
    loc, exprtokens = e._parse( instring, loc, doActions )
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/pyparsing.py", line 1005, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/pyparsing.py", line 2215, in parseImpl
    raise ParseException(instring, loc, self.errmsg, self)
pkg_resources._vendor.pyparsing.ParseException: Expected stringEnd (at char 5), (line:1, col:6)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/__init__.py", line 2788, in __init__
    super(Requirement, self).__init__(requirement_string)
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/packaging/requirements.py", line 94, in __init__
    requirement_string[e.loc:e.loc + 8]))
pkg_resources.extern.packaging.requirements.InvalidRequirement: Invalid requirement, parse error at "'://githu'"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/util/package.py", line 46, in check_package_exists
    req = pkg_resources.Requirement.parse(package)
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/__init__.py", line 2833, in parse
    req, = parse_requirements(s)
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/__init__.py", line 2781, in parse_requirements
    yield Requirement(line)
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/__init__.py", line 2790, in __init__
    raise RequirementParseError(str(e))
pkg_resources.RequirementParseError: Invalid requirement, parse error at "'://githu'"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/packaging/requirements.py", line 90, in __init__
    req = REQUIREMENT.parseString(requirement_string)
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/pyparsing.py", line 1137, in parseString
    raise exc
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/pyparsing.py", line 1127, in parseString
    loc, tokens = self._parse( instring, 0 )
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/pyparsing.py", line 1001, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/pyparsing.py", line 2412, in parseImpl
    loc, exprtokens = e._parse( instring, loc, doActions )
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/pyparsing.py", line 1005, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/pyparsing.py", line 2215, in parseImpl
    raise ParseException(instring, loc, self.errmsg, self)
pkg_resources._vendor.pyparsing.ParseException: Expected stringEnd (at char 7), (line:1, col:8)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/__init__.py", line 2788, in __init__
    super(Requirement, self).__init__(requirement_string)
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/_vendor/packaging/requirements.py", line 94, in __init__
    requirement_string[e.loc:e.loc + 8]))
pkg_resources.extern.packaging.requirements.InvalidRequirement: Invalid requirement, parse error at "'/smappee'"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.4/multiprocessing/process.py", line 254, in _bootstrap
    self.run()
  File "/usr/lib/python3.4/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/__main__.py", line 245, in setup_and_run_hass
    skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days)
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/bootstrap.py", line 303, in from_config_file
    skip_pip=skip_pip)
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/bootstrap.py", line 275, in from_config_dict
    _setup_component(hass, domain, config)
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/bootstrap.py", line 128, in _setup_component
    p_name)
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/bootstrap.py", line 206, in prepare_setup_platform
    if not _handle_requirements(hass, platform, platform_path):
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/bootstrap.py", line 68, in _handle_requirements
    if not pkg_util.install_package(req, target=hass.config.path('deps')):
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/util/package.py", line 22, in install_package
    if check_package_exists(package, target):
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/util/package.py", line 49, in check_package_exists
    req = pkg_resources.Requirement.parse(urlparse(package).fragment)
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/__init__.py", line 2833, in parse
    req, = parse_requirements(s)
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/__init__.py", line 2781, in parse_requirements
    yield Requirement(line)
  File "/usr/local/lib/python3.4/dist-packages/pkg_resources/__init__.py", line 2790, in __init__
    raise RequirementParseError(str(e))
pkg_resources.RequirementParseError: Invalid requirement, parse error at "'/smappee'"

What am i doing wrong?

Remove /smappee from the requirements string.

I’ve already tried that, restarting homeassistant looks like this for 10 minutes

[email protected]:~ $ hass
Config directory: /home/pi/.homeassistant
16-05-04 06:55:42 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: domain=homeassistant, service=turn_off>
16-05-04 06:55:42 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: domain=homeassistant, service=turn_on>
16-05-04 06:55:42 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: domain=homeassistant, service=toggle>
16-05-04 06:55:42 INFO (MainThread) [homeassistant.bootstrap] Home Assistant core initialized
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded sensor from homeassistant.components.sensor
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded updater from homeassistant.components.updater
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded media_player from homeassistant.components.media_player
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded thermostat from homeassistant.components.thermostat
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded history from homeassistant.components.history
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded recorder from homeassistant.components.recorder
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded http from homeassistant.components.http
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded frontend from homeassistant.components.frontend
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded api from homeassistant.components.api
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded logbook from homeassistant.components.logbook
16-05-04 06:55:43 INFO (MainThread) [homeassistant.core] Bus:Handling <Event component_loaded[L]: component=recorder>
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded sensor.time_date from homeassistant.components.sensor.time_date
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded sensor.smappee from homeassistant.components.sensor.smappee
16-05-04 06:55:43 INFO (MainThread) [homeassistant.util.package] Attempting install of https://github.com/EnergyID/smappy/archive/master.zip#smappee==0.1.1

And when i abort with control C, i get this:

[email protected]:~ $ hass
Config directory: /home/pi/.homeassistant
16-05-04 06:55:42 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: domain=homeassistant, service=turn_off>
16-05-04 06:55:42 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: domain=homeassistant, service=turn_on>
16-05-04 06:55:42 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: domain=homeassistant, service=toggle>
16-05-04 06:55:42 INFO (MainThread) [homeassistant.bootstrap] Home Assistant core initialized
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded sensor from homeassistant.components.sensor
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded updater from homeassistant.components.updater
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded media_player from homeassistant.components.media_player
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded thermostat from homeassistant.components.thermostat
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded history from homeassistant.components.history
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded recorder from homeassistant.components.recorder
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded http from homeassistant.components.http
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded frontend from homeassistant.components.frontend
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded api from homeassistant.components.api
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded logbook from homeassistant.components.logbook
16-05-04 06:55:43 INFO (MainThread) [homeassistant.core] Bus:Handling <Event component_loaded[L]: component=recorder>
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded sensor.time_date from homeassistant.components.sensor.time_date
16-05-04 06:55:43 INFO (MainThread) [homeassistant.loader] Loaded sensor.smappee from homeassistant.components.sensor.smappee
16-05-04 06:55:43 INFO (MainThread) [homeassistant.util.package] Attempting install of https://github.com/EnergyID/smappy/archive/master.zip#smappee==0.1.1
^C^CProcess Process-1:
Traceback (most recent call last):
  File "/usr/local/bin/hass", line 11, in <module>
Traceback (most recent call last):
    sys.exit(main())
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/__main__.py", line 342, in main
    keep_running, exit_code = run_hass_process(hass_proc)
TypeError: 'bool' object is not iterable
  File "/usr/lib/python3.4/subprocess.py", line 539, in call
    return p.wait(timeout=timeout)
[email protected]:~ $ 

Have you tried installing smappee manually using pip with the the same string? Do you get any errors in the console then?

I looked at the github repo and the package is called smappy not smappee. Change to smappy in the string and try installing manually first.

When I trie that, I get this

[email protected]:~ $ pip install 'https://github.com/EnergyID/smappy/archive/master.zip'

Collecting https://github.com/EnergyID/smappy/archive/master.zip
  Downloading https://github.com/EnergyID/smappy/archive/master.zip
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/local/lib/python3.4/dist-packages (from smappy==0.1.1)
Collecting pandas (from smappy==0.1.1)
  Using cached pandas-0.18.0.tar.gz
Collecting python-dateutil>=2 (from pandas->smappy==0.1.1)
  Using cached python_dateutil-2.5.3-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): pytz>=2011k in /usr/local/lib/python3.4/dist-packages (from pandas->smappy==0.1.1)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.7.0 in /usr/lib/python3/dist-packages (from pandas->smappy==0.1.1)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2->pandas->smappy==0.1.1)
Building wheels for collected packages: pandas
  Running setup.py bdist_wheel for pandas ... -

until control C

[email protected]:~ $ pip install 'https://github.com/EnergyID/smappy/archive/master.zip'

Collecting https://github.com/EnergyID/smappy/archive/master.zip
  Downloading https://github.com/EnergyID/smappy/archive/master.zip
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/local/lib/python3.4/dist-packages (from smappy==0.1.1)
Collecting pandas (from smappy==0.1.1)
  Using cached pandas-0.18.0.tar.gz
Collecting python-dateutil>=2 (from pandas->smappy==0.1.1)
  Using cached python_dateutil-2.5.3-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): pytz>=2011k in /usr/local/lib/python3.4/dist-packages (from pandas->smappy==0.1.1)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.7.0 in /usr/lib/python3/dist-packages (from pandas->smappy==0.1.1)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2->pandas->smappy==0.1.1)
Building wheels for collected packages: pandas
  Running setup.py bdist_wheel for pandas ... -^error
  Failed building wheel for pandas
  Running setup.py clean for pandas
Failed to build pandas
Installing collected packages: python-dateutil, pandas, smappy
Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/pip/basecommand.py", line 209, in main
    status = self.run(options, args)
  File "/usr/local/lib/python3.4/dist-packages/pip/commands/install.py", line 317, in run
    prefix=options.prefix_path,
  File "/usr/local/lib/python3.4/dist-packages/pip/req/req_set.py", line 732, in install
    **kwargs
  File "/usr/local/lib/python3.4/dist-packages/pip/req/req_install.py", line 835, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/usr/local/lib/python3.4/dist-packages/pip/req/req_install.py", line 1030, in move_wheel_files
    isolated=self.isolated,
  File "/usr/local/lib/python3.4/dist-packages/pip/wheel.py", line 344, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/usr/local/lib/python3.4/dist-packages/pip/wheel.py", line 315, in clobber
    ensure_dir(destdir)
  File "/usr/local/lib/python3.4/dist-packages/pip/utils/__init__.py", line 83, in ensure_dir
    os.makedirs(path)
  File "/usr/lib/python3.4/os.py", line 237, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.4/dist-packages/python_dateutil-2.5.3.dist-info'
[email protected]:~ $ 

You need to install in user mode or use sudo.

pip install --user 'http...

or

sudo pip install 'http...

Then you might also have a build problem with pandas. I would try to install pandas first and see if that works or fails with any errors.

do you succeed in that tentative ?

Not really,
but i have a local api, received it from smappee support:

# Written by Michael Lucas @ Smappee. 2016

import json, requests, sched

smappeeIP = '192.168.0.153' # local IP of your Smappee
smappeePwd = 'admin'


headers = {'Content-Type': 'application/json;charset=UTF-8'}
payload = smappeePwd


r = requests.post('http://'+smappeeIP+'/gateway/apipublic/logon', data = payload, headers=headers)
print(r.content)


#r = requests.get('http://'+smappeeIP+'/gateway/apipublic/reportInstantaneousValues',  headers=headers )
#print(r.content)

payload = "loadInstantaneous"
r = requests.post('http://'+smappeeIP+'/gateway/apipublic/instantaneous',  headers=headers, data=payload )
print(r.content)


# restart the Smappee
#r = requests.post('http://'+smappeeIP+'/gateway/apipublic/restartEMeter', headers=headers)

maybe you can do something with this.

I’m busy making sensors for mysensors.
When all of this is finished, i can focus on python again.

Thanks !!!

I just add the intensity (in Ampere) and the Active Power (in Watts) thanks 2 bash scripts : (just remplace the IP adress with your local smappee IP address)

(intensity)
#!/bin/bash
var0=$(curl -silent -H “Content-Type: application/json” -X POST -d “admin” http://192.168.1.32/gateway/apipublic/logon)
var1=$(curl -silent http://192.168.1.32/gateway/apipublic/reportInstantaneousValues | awk ‘{split($0, a, “
”); print a[5]}’ | awk ‘{split($0, b, “,”); print b[1]}’ | awk ‘{split($0, c, “=”); print c[2]}’ | awk ‘{split($0, d, " "); print d[1]}’)
echo $var1

and

(active power)
#!/bin/bash
var0=$(curl -silent -H “Content-Type: application/json” -X POST -d “admin” http://192.168.1.32/gateway/apipublic/logon)
var1=$(curl -silent http://192.168.1.32/gateway/apipublic/reportInstantaneousValues | awk ‘{split($0, a, “
”); print a[5]}’ | awk ‘{split($0, b, “,”); print b[2]}’ | awk ‘{split($0, c, “=”); print c[2]}’ | awk ‘{split($0, d, " "); print d[1]}’)
echo $var1

Before executing them with HA, just made them launchable with : sudo chmod +x smappee.sh

and both called thanks to entries in sensor.yaml like this one :

  • platform: command_line
    name: "Intensité"
    command: "bash /home/pi/.homeassistant/smappee2.sh"
    unit_of_measurement: “A”

By the way, there is also others url you can call thanks to local API :

http:///gateway/apipublic/logon
http:///gateway/apipublic/statisticsPublicReport
http:///gateway/apipublic/reportInstantaneousValues
http:///gateway/apipublic/restartEMeter
http:///gateway/apipublic/commandControlPublic

If you want to control some smappee plugs, you can ask :

to get the list of your plugs and their ID:
curl -H “Content-Type: application/json” -X POST -d “load” http:///gateway/apipublic/commandControlPublic

To turn ON the plug with id 6:
curl -H “Content-Type: application/json” -X POST -d “control,controlId=1|6” http:///gateway/apipublic/commandControlPublic

To turn OFF the plug with id 6:
curl -H “Content-Type: application/json” -X POST -d “control,controlId=0|6” http:///gateway/apipublic/commandControlPublic

Hope it will help !

1 Like

Hi doune,

Great work!
I can get your examples to work. Var0 receives the login auth. ok, but var1 is empty.


var0=$(curl -silent -H “Content-Type: application/json” -X POST -d “admin” http://192.168.1.32/gateway/apipublic/logon)
var1=$(curl -silent http://192.168.1.32/gateway/apipublic/reportInstantaneousValues | awk ‘{split($0, a, “”); print a[5]}’ | awk ‘{split($0, b, “,”); print b[2]}’ | awk ‘{split($0, c, “=”); print c[2]}’ | awk ‘{split($0, d, " "); print d[1]}’)
echo $var1

Also: controlling the plugs dissent seem to work!? The response from the curl is:

HTTP ERROR 405

Problem accessing /apipublic/commandControlPublic. Reason:

    HTTP method POST is not supported by this URL

Powered by Jetty://

Any tips?
/Tonkin

Sorry!! I Cant get your examples to work

Hi Giel,

The Pandas package is only used for a single method in smappy (get_consumption_dataframe), so as long as you are not using that method you could perfectly omit pandas.

I have updated the package to exclude pandas as a requirement and top level import, so pip install smappy should now work without problems on a raspberry pi.

Also, installing Pandas through pip is not recommended on Linux systems, so you should use sudo apt-get install python-pandas if you plan on using the get_consumption_dataframe method.

Have you tried to update the IP adress in the script ?

Mine is 192.168.1.32 but yours ?

Hi

I’d like to share my findings on this integration.
The provided script doesn’t work for me, don’t know why but the output is somewhat different from the @doune ones. Also auth is not needed (var0).

These are the scripts I’m using just modifying the awk part:

Watt

#!/bin/bash

var1=$(curl -s http://192.168.1.100/gateway/apipublic/reportInstantaneousValues | awk '{split($0, a, " "); print a[6]}'|awk '{split($0, c, "="); print c[2]}')
echo $var1

Ampere

#!/bin/bash

var1=$(curl -s http://192.168.1.100/gateway/apipublic/reportInstantaneousValues | awk '{split($0, a, " "); print a[4]}'|awk '{split($0, c, "="); print c[2]}')
echo $var1

Cos fi

#!/bin/bash

var1=$(curl -s http://192.168.1.100/gateway/apipublic/reportInstantaneousValues | awk '{split($0, a, " "); print a[12]}'|awk '{split($0, c, "="); print c[2]}'|awk '{split($0, c, ","); print c[1]}http')
echo $var1

Following an example of the JSON my smappee is answering.

We could try and merge this topic with: Smappee custom component and get a proper component into HA

Sure! the Smappee component is quite cooler than these bunch of scripts.

1 Like

thanks for the “bunch of scripts”… #makemehappy

I’m sorry @doune, didn’t want to offend you or your useful work that let me succesfully integrate smappee in my home-assistant.
I just wanted to highlight having a native component is more elegant than external script dependancies.

Peace :peace_symbol:

Hi guys,

I just buyed a Smappee Energy and asked for API access.

Customer service told me that API access isn’t free anymore and suggest me to use MQTT.

They send me this document.

https://support.smappee.com/attachments/token/Fxcc6mPCUyHrTBtaYw41yEKW6/?name=Smappee+local+MQTT+topics+-20-09-2018.pdf

I think someone needs to update component page (I’m a total noob), no?