Add validation and switch python-mystrom (#2529)
parent
6694f29918
commit
89972ed940
|
@ -6,36 +6,38 @@ https://home-assistant.io/components/switch.mystrom/
|
||||||
"""
|
"""
|
||||||
import logging
|
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
|
from homeassistant.components.switch import SwitchDevice
|
||||||
|
|
||||||
|
REQUIREMENTS = ['python-mystrom==0.3.6']
|
||||||
|
|
||||||
DEFAULT_NAME = 'myStrom Switch'
|
DEFAULT_NAME = 'myStrom Switch'
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Find and return myStrom switch."""
|
"""Find and return myStrom switch."""
|
||||||
host = config.get('host')
|
from pymystrom import MyStromPlug, exceptions
|
||||||
|
|
||||||
if host is None:
|
host = config.get(CONF_HOST)
|
||||||
_LOGGER.error('Missing required variable: host')
|
|
||||||
return False
|
|
||||||
|
|
||||||
resource = 'http://{}'.format(host)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
requests.get(resource, timeout=10)
|
MyStromPlug(host).get_status()
|
||||||
except requests.exceptions.ConnectionError:
|
except exceptions.MyStromConnectionError:
|
||||||
_LOGGER.error("No route to device %s. "
|
_LOGGER.error("No route to device '%s'", host)
|
||||||
"Please check the IP address in the configuration file",
|
|
||||||
host)
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
add_devices([MyStromSwitch(
|
add_devices([MyStromSwitch(config.get('name', DEFAULT_NAME), host)])
|
||||||
config.get('name', DEFAULT_NAME),
|
|
||||||
resource)])
|
|
||||||
|
|
||||||
|
|
||||||
class MyStromSwitch(SwitchDevice):
|
class MyStromSwitch(SwitchDevice):
|
||||||
|
@ -43,10 +45,13 @@ class MyStromSwitch(SwitchDevice):
|
||||||
|
|
||||||
def __init__(self, name, resource):
|
def __init__(self, name, resource):
|
||||||
"""Initialize the myStrom switch."""
|
"""Initialize the myStrom switch."""
|
||||||
self._state = False
|
from pymystrom import MyStromPlug
|
||||||
|
|
||||||
self._name = name
|
self._name = name
|
||||||
self._resource = resource
|
self._resource = resource
|
||||||
self.consumption = 0
|
self.data = {}
|
||||||
|
self.plug = MyStromPlug(self._resource)
|
||||||
|
self.update()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
@ -56,45 +61,37 @@ class MyStromSwitch(SwitchDevice):
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if switch is on."""
|
"""Return true if switch is on."""
|
||||||
return self._state
|
return bool(self.data['relay'])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def current_power_mwh(self):
|
def current_power_mwh(self):
|
||||||
"""Return the urrent power consumption in mWh."""
|
"""Return the current power consumption in mWh."""
|
||||||
return self.consumption
|
return round(self.data['power'], 2)
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the switch on."""
|
"""Turn the switch on."""
|
||||||
|
from pymystrom import exceptions
|
||||||
try:
|
try:
|
||||||
request = requests.get('{}/relay'.format(self._resource),
|
self.plug.set_relay_on()
|
||||||
params={'state': '1'},
|
except exceptions.MyStromConnectionError:
|
||||||
timeout=10)
|
_LOGGER.error("No route to device '%s'. Is device offline?",
|
||||||
if request.status_code == 200:
|
|
||||||
self._state = True
|
|
||||||
except requests.exceptions.ConnectionError:
|
|
||||||
_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."""
|
||||||
|
from pymystrom import exceptions
|
||||||
try:
|
try:
|
||||||
request = requests.get('{}/relay'.format(self._resource),
|
self.plug.set_relay_off()
|
||||||
params={'state': '0'},
|
except exceptions.MyStromConnectionError:
|
||||||
timeout=10)
|
_LOGGER.error("No route to device '%s'. Is device offline?",
|
||||||
if request.status_code == 200:
|
|
||||||
self._state = False
|
|
||||||
except requests.exceptions.ConnectionError:
|
|
||||||
_LOGGER.error("Can't turn on %s. Is device offline?",
|
|
||||||
self._resource)
|
self._resource)
|
||||||
|
|
||||||
def update(self):
|
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:
|
try:
|
||||||
request = requests.get('{}/report'.format(self._resource),
|
self.data = self.plug.get_status()
|
||||||
timeout=10)
|
except exceptions.MyStromConnectionError:
|
||||||
data = request.json()
|
self.data = {'power': 0, 'relay': False}
|
||||||
self._state = bool(data['relay'])
|
|
||||||
self.consumption = data['power']
|
|
||||||
except requests.exceptions.ConnectionError:
|
|
||||||
_LOGGER.error("No route to device '%s'. Is device offline?",
|
_LOGGER.error("No route to device '%s'. Is device offline?",
|
||||||
self._resource)
|
self._resource)
|
||||||
|
|
|
@ -317,6 +317,9 @@ python-forecastio==1.3.4
|
||||||
# homeassistant.components.media_player.mpd
|
# homeassistant.components.media_player.mpd
|
||||||
python-mpd2==0.5.5
|
python-mpd2==0.5.5
|
||||||
|
|
||||||
|
# homeassistant.components.switch.mystrom
|
||||||
|
python-mystrom==0.3.6
|
||||||
|
|
||||||
# homeassistant.components.nest
|
# homeassistant.components.nest
|
||||||
python-nest==2.9.2
|
python-nest==2.9.2
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue