core/homeassistant/components/mystrom/switch.py

93 lines
2.6 KiB
Python
Raw Normal View History

"""Support for myStrom switches."""
2015-11-25 16:21:11 +00:00
import logging
2016-02-19 05:27:50 +00:00
import voluptuous as vol
2015-11-25 16:21:11 +00:00
2019-07-31 19:25:30 +00:00
from homeassistant.components.switch import SwitchDevice, PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME, CONF_HOST
import homeassistant.helpers.config_validation as cv
2015-11-25 16:21:11 +00:00
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "myStrom Switch"
2015-11-25 16:21:11 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}
)
2015-11-25 16:21:11 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
2016-03-08 12:35:39 +00:00
"""Find and return myStrom switch."""
from pymystrom.switch import MyStromPlug, exceptions
2015-11-25 16:21:11 +00:00
name = config.get(CONF_NAME)
host = config.get(CONF_HOST)
2015-11-25 16:21:11 +00:00
try:
MyStromPlug(host).get_status()
except exceptions.MyStromConnectionError:
_LOGGER.error("No route to device: %s", host)
return
2015-11-25 16:21:11 +00:00
add_entities([MyStromSwitch(name, host)])
2015-11-25 16:21:11 +00:00
class MyStromSwitch(SwitchDevice):
2016-03-08 12:35:39 +00:00
"""Representation of a myStrom switch."""
2015-11-25 16:21:11 +00:00
def __init__(self, name, resource):
2016-03-08 12:35:39 +00:00
"""Initialize the myStrom switch."""
from pymystrom.switch import MyStromPlug
2015-11-25 16:21:11 +00:00
self._name = name
self._resource = resource
self.data = {}
self.plug = MyStromPlug(self._resource)
self.update()
2015-11-25 16:21:11 +00:00
@property
def name(self):
2016-03-08 12:35:39 +00:00
"""Return the name of the switch."""
2015-11-25 16:21:11 +00:00
return self._name
@property
def is_on(self):
2016-03-08 12:35:39 +00:00
"""Return true if switch is on."""
2019-07-31 19:25:30 +00:00
return bool(self.data["relay"])
2015-11-25 16:21:11 +00:00
@property
def current_power_w(self):
"""Return the current power consumption in W."""
2019-07-31 19:25:30 +00:00
return round(self.data["power"], 2)
2015-11-25 16:21:11 +00:00
def turn_on(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the switch on."""
from pymystrom import exceptions
2019-07-31 19:25:30 +00:00
2015-11-25 17:07:29 +00:00
try:
self.plug.set_relay_on()
except exceptions.MyStromConnectionError:
_LOGGER.error("No route to device: %s", self._resource)
2015-11-25 16:21:11 +00:00
def turn_off(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the switch off."""
from pymystrom import exceptions
2019-07-31 19:25:30 +00:00
2015-11-25 17:07:29 +00:00
try:
self.plug.set_relay_off()
except exceptions.MyStromConnectionError:
_LOGGER.error("No route to device: %s", self._resource)
2015-11-25 16:21:11 +00:00
def update(self):
"""Get the latest data from the device and update the data."""
from pymystrom import exceptions
2019-07-31 19:25:30 +00:00
2015-11-25 16:21:11 +00:00
try:
self.data = self.plug.get_status()
except exceptions.MyStromConnectionError:
2019-07-31 19:25:30 +00:00
self.data = {"power": 0, "relay": False}
_LOGGER.error("No route to device: %s", self._resource)