Toon (eneco) integration with Home Assistant

Hi @krocat. I added smokedetectors to the library. If you could be so kind as to test if the latest commit on github works fine for you I will package it and upload it to pypi as 0.5.0 and then we can see how to add them to the component.

@PuckStar I would guess so (about setting things up with HA if the detectors are in toon).

1 Like

@krocat could you please have a look into your toon._state under deviceConfigInfo and deviceStatusInfo for relevant to the smokedetectors entries, because I think that the info exposed under smokedetectors is very generic to be of any use. How do the smokedetectors publish their state? I am guessing that something exists under the mentioned keys.

I’m having difficulty keeping up with you today @costas, you’re on a roll :slight_smile: Those two sections that you mention only contain Hue lights in my case. I think whether or not the smoke detector is connected and battery level is all we’ll get from them. It’s the same in the Toon app. When smoke is detected, I get a call and a text message and a message is displayed on Toon, this may be something they do outside the API. It’s not super useful, but it’s nice to have a complete Toon experience with this component.

Regarding the smoke detectors if smoke detection is not passed through the API and we can’t use it for triggers in Home Assistant then I won’t connect them to Toon but rather keep them connected directly to Home Assistant.

If you’re unsure if it’s passed or not I could try to trigger the detector and see what is passed through but I need some help in how I’ll be able to (in realtime) see what’s passing the API.
As Toon supports getting a call when there is smoke I prefer to have that solution initially. In HASS I can do the lights on etc. but don’t have a “call my mobile” component.

@krocat sorry, its a slow day for me so i was having fun… putting it to rest now… @PuckStar if you want to help with this I suggest you setup a local virtualenv and install toonlib there. Lower the caching for the library to 2 seconds or something. You can do that on the configuration.py in the path of your virtualenv (in my case its under ~/.virtualenvs/toonlib/lib/python2.7/site-packages/toonlib/configuration.py). Through a python console interactively instantiate the object enabling debug output, something like

import logging
logging.basicConfig(level=logging.DEBUG)
username=‘USERNAME’
password=‘PASSWORD’
from toonlib import Toon
toon=Toon(username, password)
print(toon._state)

Trigger your smoke detector
and do a few
print(toon._state)

and post the output of those prints for us to have a look.

@costas, I think I just had another incomplete response. The component still fails, unfortunately and with a less intuitive error than before. It looks like powerUsage did not come through, can you check if you default these to zero?

    17-04-26 15:09:30 ERROR (MainThread) [homeassistant.setup] Error during setup of component toon
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/setup.py", line 192, in _async_setup_component
    None, component.setup, hass, processed_config)
  File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
    value = future.result()
  File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
    raise self._exception
  File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/toon.py", line 52, in setup
    solar)
  File "/home/homeassistant/.homeassistant/custom_components/toon.py", line 83, in __init__
    self.update()
  File "/home/homeassistant/.homeassistant/custom_components/toon.py", line 87, in update
    self.data['power_current'] = self.toon.power.value
  File "/home/homeassistant/.homeassistant/deps/toonlib/toonlib.py", line 238, in power
    power = self._state['powerUsage']
  File "/home/homeassistant/.homeassistant/deps/cachetools/__init__.py", line 46, in wrapper
    v = func(*args, **kwargs)
  File "/home/homeassistant/.homeassistant/deps/toonlib/toonlib.py", line 167, in _state
    state.update(self._get_data('/client/auth/retrieveToonState'))
  File "/home/homeassistant/.homeassistant/deps/toonlib/toonlib.py", line 187, in _get_data
    return response.json()
  File "/srv/homeassistant/lib/python3.4/site-packages/requests/models.py", line 866, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

that is probably your session being invalidated… This is what I am trying to get to in my debugging setup so i can solve it.
What this says is that in line 187 we try to return response.json() which of course internally calls the json library. But seeing as there is no json response (we probably have an error code and a message in response.text) the json library borks. Nothing to do with incomplete response I think.

I received it when restarting HA, I wouldn’t expect a session timeout at that stage…

Following is my debugging changes in toonlib to catch the de authentication. Line 177 of toonlib.py

def _get_data(self, endpoint, params=None):
    url = '{base}{endpoint}'.format(base=self.base_url,
                                    endpoint=endpoint)
    response = requests.get(url, params=params or self._parameters)  # noqa
    if not response.ok:
        self._logger.error(('\n\tStatus Code :{}'
                            '\n\tText :{}').format(response.status_code,
                                                   response.text))
        return None
    try:
        return response.json()
    except Exception:
        self._logger.exception(('\n\tStatus Code :{}'
                                 '\n\tText :{}').format(response.status_code,
                                                        response.text))

It might be an incomplete authentication request. I have had a few 500 from the api. Sadly, very flaky… Try to add that method in your code so when it breaks we get something better. I am also looking at the same place. We will fix it in due time… We just need the data :slight_smile:

Actually i just added that to the library. if the smokedetectors work for you i can make a new version, we can bump the dependency on the component and anyone having any problem will give us some useful insights on what happens there. Once we fix it we can revert the broad exception handling on the next version with the fix in.

Hi @costas, this looks like a bug in the library:

17-04-26 16:53:39 ERROR (MainThread) [homeassistant.setup] Error during setup of component toon
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/setup.py", line 192, in _async_setup_component
    None, component.setup, hass, processed_config)
  File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
    value = future.result()
  File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
    raise self._exception
  File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/toon.py", line 52, in setup
    solar)
  File "/home/homeassistant/.homeassistant/custom_components/toon.py", line 83, in __init__
    self.update()
  File "/home/homeassistant/.homeassistant/custom_components/toon.py", line 120, in update
    for sd in self.toon.smokedetectors:
  File "/home/homeassistant/.homeassistant/deps/toonlib/toonlib.py", line 206, in smokedetectors
    {}).get('device', [])]
  File "/home/homeassistant/.homeassistant/deps/toonlib/toonlib.py", line 205, in <listcomp>
    for smokedetector in self._state.get('smokeDetectors',
TypeError: __new__() takes 7 positional arguments but 8 were given

Yeah, and I know where it is.I will fix it in half an hour and get back to you.currently in transit.

I pushed the fix. In line 198 of toonlib you can remove the self argument if you want to fix it locally. @krocat

Ok what I now did is replace all the toon.py files in the custom_components folder(s).
Also replaced toonlib.py.
And after restarting Hass I get this:

17-04-26 17:47:37 ERROR (MainThread) [homeassistant.loader] Unable to find component sensor.toon
17-04-26 17:47:37 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform sensor.toon: Platform not found.
17-04-26 17:47:37 ERROR (MainThread) [homeassistant.loader] Unable to find component climate.toon
17-04-26 17:47:37 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform climate.toon: Platform not found.

Did I do it the right way? Or should I update toonlib in a different way?

Please see @opdoffer 's post above. Basically you need to place the files from @krocat ToonHA github inside the appropriate structure of your custom_components. toonlib should have nothing to do with it, it is handled transparently from HA as a dependency. So clone ToonHA and move the toon.py files to the appropriate location.

@krocat wouldn’t it be easier for people to deploy and try out the component if you renamed the directories to lower case and then people could just git clone the whole project in the custom_component directory?

Yes, that is a bit silly isn’t it. Not sure if I can work on it tonight, so feel freeto change it if you have time. I’ve created a new branch for the smoke detector code btw

:slight_smile: nah I think I have done enough for today. My long weekend has started already. Let’s tackle things on Monday. Have a wonderful kings day everyone!