Add validation and switch python-mystrom (#2529)

pull/2530/head
Fabian Affolter 2016-07-15 18:02:20 +02:00 committed by Paulus Schoutsen
parent 6694f29918
commit 89972ed940
2 changed files with 41 additions and 41 deletions

View File

@ -6,36 +6,38 @@ https://home-assistant.io/components/switch.mystrom/
"""
import logging
import requests
import voluptuous as vol
from homeassistant.const import (CONF_PLATFORM, CONF_NAME, CONF_HOST)
import homeassistant.helpers.config_validation as cv
from homeassistant.components.switch import SwitchDevice
REQUIREMENTS = ['python-mystrom==0.3.6']
DEFAULT_NAME = 'myStrom Switch'
_LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = vol.Schema({
vol.Required(CONF_PLATFORM): 'mystrom',
vol.Optional(CONF_NAME): cv.string,
vol.Required(CONF_HOST): cv.string,
})
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Find and return myStrom switch."""
host = config.get('host')
from pymystrom import MyStromPlug, exceptions
if host is None:
_LOGGER.error('Missing required variable: host')
return False
resource = 'http://{}'.format(host)
host = config.get(CONF_HOST)
try:
requests.get(resource, timeout=10)
except requests.exceptions.ConnectionError:
_LOGGER.error("No route to device %s. "
"Please check the IP address in the configuration file",
host)
MyStromPlug(host).get_status()
except exceptions.MyStromConnectionError:
_LOGGER.error("No route to device '%s'", host)
return False
add_devices([MyStromSwitch(
config.get('name', DEFAULT_NAME),
resource)])
add_devices([MyStromSwitch(config.get('name', DEFAULT_NAME), host)])
class MyStromSwitch(SwitchDevice):
@ -43,10 +45,13 @@ class MyStromSwitch(SwitchDevice):
def __init__(self, name, resource):
"""Initialize the myStrom switch."""
self._state = False
from pymystrom import MyStromPlug
self._name = name
self._resource = resource
self.consumption = 0
self.data = {}
self.plug = MyStromPlug(self._resource)
self.update()
@property
def name(self):
@ -56,45 +61,37 @@ class MyStromSwitch(SwitchDevice):
@property
def is_on(self):
"""Return true if switch is on."""
return self._state
return bool(self.data['relay'])
@property
def current_power_mwh(self):
"""Return the urrent power consumption in mWh."""
return self.consumption
"""Return the current power consumption in mWh."""
return round(self.data['power'], 2)
def turn_on(self, **kwargs):
"""Turn the switch on."""
from pymystrom import exceptions
try:
request = requests.get('{}/relay'.format(self._resource),
params={'state': '1'},
timeout=10)
if request.status_code == 200:
self._state = True
except requests.exceptions.ConnectionError:
_LOGGER.error("Can't turn on %s. Is device offline?",
self.plug.set_relay_on()
except exceptions.MyStromConnectionError:
_LOGGER.error("No route to device '%s'. Is device offline?",
self._resource)
def turn_off(self, **kwargs):
"""Turn the switch off."""
from pymystrom import exceptions
try:
request = requests.get('{}/relay'.format(self._resource),
params={'state': '0'},
timeout=10)
if request.status_code == 200:
self._state = False
except requests.exceptions.ConnectionError:
_LOGGER.error("Can't turn on %s. Is device offline?",
self.plug.set_relay_off()
except exceptions.MyStromConnectionError:
_LOGGER.error("No route to device '%s'. Is device offline?",
self._resource)
def update(self):
"""Get the latest data from REST API and update the state."""
"""Get the latest data from the device and update the data."""
from pymystrom import exceptions
try:
request = requests.get('{}/report'.format(self._resource),
timeout=10)
data = request.json()
self._state = bool(data['relay'])
self.consumption = data['power']
except requests.exceptions.ConnectionError:
self.data = self.plug.get_status()
except exceptions.MyStromConnectionError:
self.data = {'power': 0, 'relay': False}
_LOGGER.error("No route to device '%s'. Is device offline?",
self._resource)

View File

@ -317,6 +317,9 @@ python-forecastio==1.3.4
# homeassistant.components.media_player.mpd
python-mpd2==0.5.5
# homeassistant.components.switch.mystrom
python-mystrom==0.3.6
# homeassistant.components.nest
python-nest==2.9.2