Catch connection timeout
parent
9dfa4ea233
commit
e45ef0013b
|
@ -10,7 +10,6 @@ import logging
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchDevice
|
from homeassistant.components.switch import SwitchDevice
|
||||||
from homeassistant.const import STATE_UNKNOWN
|
|
||||||
|
|
||||||
DEFAULT_NAME = 'myStrom Switch'
|
DEFAULT_NAME = 'myStrom Switch'
|
||||||
|
|
||||||
|
@ -45,7 +44,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
class MyStromSwitch(SwitchDevice):
|
class MyStromSwitch(SwitchDevice):
|
||||||
""" Represents a myStrom switch. """
|
""" Represents a myStrom switch. """
|
||||||
def __init__(self, name, resource):
|
def __init__(self, name, resource):
|
||||||
self._state = STATE_UNKNOWN
|
self._state = False
|
||||||
self._name = name
|
self._name = name
|
||||||
self._resource = resource
|
self._resource = resource
|
||||||
self.consumption = 0
|
self.consumption = 0
|
||||||
|
@ -67,24 +66,32 @@ class MyStromSwitch(SwitchDevice):
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
""" Turn the switch on. """
|
""" Turn the switch on. """
|
||||||
|
try:
|
||||||
request = requests.get('{}/relay'.format(self._resource),
|
request = requests.get('{}/relay'.format(self._resource),
|
||||||
params={'state': '1'},
|
params={'state': '1'},
|
||||||
timeout=10)
|
timeout=10)
|
||||||
if request.status_code == 200:
|
if request.status_code == 200:
|
||||||
self._state = True
|
self._state = True
|
||||||
else:
|
else:
|
||||||
|
raise requests.exceptions.ConnectionError
|
||||||
|
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
_LOGGER.error("Can't turn on %s. Is device offline?",
|
_LOGGER.error("Can't turn on %s. Is device offline?",
|
||||||
self._resource)
|
self._resource)
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
""" Turn the switch off. """
|
""" Turn the switch off. """
|
||||||
|
try:
|
||||||
request = requests.get('{}/relay'.format(self._resource),
|
request = requests.get('{}/relay'.format(self._resource),
|
||||||
params={'state': '0'},
|
params={'state': '0'},
|
||||||
timeout=10)
|
timeout=10)
|
||||||
if request.status_code == 200:
|
if request.status_code == 200:
|
||||||
self._state = False
|
self._state = False
|
||||||
else:
|
else:
|
||||||
_LOGGER.error("Can't turn off %s. Is device offline?",
|
raise requests.exceptions.ConnectionError
|
||||||
|
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
_LOGGER.error("Can't turn on %s. Is device offline?",
|
||||||
self._resource)
|
self._resource)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
|
|
Loading…
Reference in New Issue