GadgetReactor / pyHS100

Python Library to control TPLink Switch (HS100 / HS110)
Other
409 stars 128 forks source link

HS110: Connection Refused every few weeks #3

Closed mweinelt closed 7 years ago

mweinelt commented 8 years ago

I'm using a HS110 via Home-Assistant and every few weeks there seems to be a process on the device dying, so it becomes unqueryable, uncontrollable.

Is this a known issue? Is there a solution?

16-10-19 16:53:00 homeassistant.core: BusHandler:Exception doing job
Traceback (most recent call last):
  File "/home/martin/home-assistant/env/lib/python3.5/site-packages/homeassistant/core.py", line 1224, in job_handler
    func(*args)
  File "/home/martin/home-assistant/env/lib/python3.5/site-packages/homeassistant/helpers/entity_component.py", line 229, in _update_entity_states
    entity.update_ha_state(True)
  File "/home/martin/home-assistant/env/lib/python3.5/site-packages/homeassistant/helpers/entity.py", line 170, in update_ha_state
    self.async_update_ha_state(force_refresh), self.hass.loop
  File "/usr/lib64/python3.5/concurrent/futures/_base.py", line 405, in result
    return self.__get_result()
  File "/usr/lib64/python3.5/concurrent/futures/_base.py", line 357, in __get_result
    raise self._exception
  File "/usr/lib64/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "/home/martin/home-assistant/env/lib/python3.5/site-packages/homeassistant/helpers/entity.py", line 197, in async_update_ha_state
    state = STATE_UNKNOWN if self.state is None else str(self.state)
  File "/home/martin/home-assistant/env/lib/python3.5/site-packages/homeassistant/helpers/entity.py", line 275, in state
    return STATE_ON if self.is_on else STATE_OFF
  File "/home/martin/home-assistant/env/lib/python3.5/site-packages/homeassistant/components/switch/tplink.py", line 54, in is_on
    return self.smartplug.state == 'ON'
  File "/home/martin/.homeassistant/deps/pyHS100/pyHS100.py", line 32, in state
    response = self.hs100_status()
  File "/home/martin/.homeassistant/deps/pyHS100/pyHS100.py", line 79, in hs100_status
    s.connect((self.ip, self.port))
ConnectionRefusedError: [Errno 111] Connection refused

Is it possibly hammering the service until it gives up? The access to the state property is not being throttled, see https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/switch/tplink.py#L51-L54.

GadgetReactor commented 8 years ago

Thanks! I have not encountered this, but I have not left my server on for long periods as you described. I am using a HS100. Perhaps @kirichkov can share his experience too.

I was looking at the Hue component in the Home-Assistant library.

I will try to add in and test a throttle function soon, but seems like the default setting on Home-Assistant is set at 30 seconds?

kirichkov commented 8 years ago

I have observed this, whenever I force quit homeassistant (Multiple times Ctrl+C). I found out that the process that accept the control commands stops listening on the port. It is possible that the process dies or the socket is hung on the switch but closed on the HASS end. This never happened to me when exiting cleanly - dev services / shutdown, but I've never ran home assistant for more than a day because I got the socket a week ago, and was working on implementing the energy meter.

Can you give me some more information - besides hass do you use the phone app? I'm suspecting the process might have a bug if two hosts try to simultaneously communicate with the switch.

I'll start testing and report back.

kirichkov commented 8 years ago

As a last resort we can consider adding a remote reboot method that uses the TP-Link debugging protocol which runs in a different process on port 9999.

Proof of concept is available at https://github.com/softScheck/tplink-smartplug/. Given this is covered by a patent I'm not sure whether we can legally implement it without TP-Link's approval.

mweinelt commented 8 years ago

I believe this switch is only being switched on in the morning and switched off in the evening, nothing more. The instance there is usually being restarted from within homeassistants frontend.

kirichkov commented 8 years ago

Some more ideas - when you notice this happening can you check via nmap or nc whether the power plug is actually listening on port 9999/tcp. Let me know if you need help how to do this.

Also - have you connected it to the tp-link cloud, if so, and when you get a connection refused, can you check whether you can switch it on/off via the tp-link cloud?

mweinelt commented 8 years ago

I will check when it happens the next time. The device is not cloud connected.

kirichkov commented 8 years ago

This happened to me yesterday, while I was fiddling with hass and starting and stopping it, but it also happened during the night, so it's probably buggy firmware.

mweinelt commented 8 years ago

I noticed today that the device has been inactive since sunday. It's sitting plugged in, but no light is on, the button on the device does not react either and the plug wasn't on the wifi network.

It works again after disconnecting it from the power socket and reconnecting it.

kirichkov commented 8 years ago

Mine has been running without a glitch for almost two weeks now. In my case the button does toggle the relay and the light is working. I noticed other people on amazon reviews have the same experience but using the TP-Link Kasa app.

I noticed there will be a new plug - HS105 - hopefully there will be a firmware upgrade that resolves this issue, otherwise this plug becomes pretty useless.

kirichkov commented 8 years ago

My plug has started crashing - never surviving for more than 3 hours, after I moved it to a different location. The only difference from my home where it was working reliably for a few weeks is that voltage is very unstable - it's usually 210-220V but sometimes it drops to 180V for very short periods. I'm also running a PowerLan but that shouldn't interfere with the plug.

@mweinelt have you noticed similar low voltage around the time your plug crashes?

mweinelt commented 8 years ago

No new data from my end yet, sorry. The thing has been stable for a while now.

mweinelt commented 7 years ago

I expect this will benefit from the protocol changes correctly handling the packet size as well.

kirichkov commented 7 years ago

Let's see. I'm 90% sure that my problems are caused by either low voltage (I've seen it drop to 160V) or the fact I'm running powerlan and it's causing some interference. I put the plug at a different location and it's running steadily but voltage is 230-240V.

rytilahti commented 7 years ago

Any update on this on the newest release? If not, maybe the reason is really the voltage jumps and there's nothing we can do about it, in which case this issue could be closed.

kirichkov commented 7 years ago

Let's close it. I'll give the newest release a try and report back. If it's still crashing and we don't get any further feedback suggesting otherwise I consider it voltage-related.