2019-02-28 17:46:38 +05:30
|
|
|
"""Support for myStrom switches."""
|
2015-11-25 17:21:11 +01:00
|
|
|
import logging
|
2016-02-18 21:27:50 -08:00
|
|
|
|
2020-04-04 16:09:49 +02:00
|
|
|
from pymystrom.exceptions import MyStromConnectionError
|
|
|
|
from pymystrom.switch import MyStromPlug
|
2016-07-15 18:02:20 +02:00
|
|
|
import voluptuous as vol
|
2015-11-25 17:21:11 +01:00
|
|
|
|
2019-12-04 00:44:50 +01:00
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
|
|
|
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
2020-02-01 23:04:42 +01:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2016-07-15 18:02:20 +02:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-11-25 17:21:11 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
DEFAULT_NAME = "myStrom Switch"
|
2015-11-25 17:21:11 +01:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-07-15 18:02:20 +02:00
|
|
|
|
2015-11-25 17:21:11 +01:00
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Find and return myStrom switch."""
|
2016-08-21 00:40:16 +02:00
|
|
|
name = config.get(CONF_NAME)
|
2016-07-15 18:02:20 +02:00
|
|
|
host = config.get(CONF_HOST)
|
2015-11-26 16:37:00 +01:00
|
|
|
|
2015-11-25 17:21:11 +01:00
|
|
|
try:
|
2016-07-15 18:02:20 +02:00
|
|
|
MyStromPlug(host).get_status()
|
2020-04-04 16:09:49 +02:00
|
|
|
except MyStromConnectionError:
|
2018-06-10 11:38:23 +02:00
|
|
|
_LOGGER.error("No route to device: %s", host)
|
2020-02-01 23:04:42 +01:00
|
|
|
raise PlatformNotReady()
|
2015-11-25 17:21:11 +01:00
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
add_entities([MyStromSwitch(name, host)])
|
2015-11-25 17:21:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MyStromSwitch(SwitchDevice):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Representation of a myStrom switch."""
|
|
|
|
|
2015-11-25 17:21:11 +01:00
|
|
|
def __init__(self, name, resource):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Initialize the myStrom switch."""
|
2015-11-25 17:21:11 +01:00
|
|
|
self._name = name
|
|
|
|
self._resource = resource
|
2016-07-15 18:02:20 +02:00
|
|
|
self.data = {}
|
|
|
|
self.plug = MyStromPlug(self._resource)
|
2020-02-01 23:04:42 +01:00
|
|
|
self._available = True
|
2015-11-25 17:21:11 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Return the name of the switch."""
|
2015-11-25 17:21:11 +01:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Return true if switch is on."""
|
2019-07-31 12:25:30 -07:00
|
|
|
return bool(self.data["relay"])
|
2015-11-25 17:21:11 +01:00
|
|
|
|
|
|
|
@property
|
2017-03-19 21:02:11 +00:00
|
|
|
def current_power_w(self):
|
|
|
|
"""Return the current power consumption in W."""
|
2019-07-31 12:25:30 -07:00
|
|
|
return round(self.data["power"], 2)
|
2015-11-25 17:21:11 +01:00
|
|
|
|
2020-02-01 23:04:42 +01:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Could the device be accessed during the last update call."""
|
|
|
|
return self._available
|
|
|
|
|
2015-11-25 17:21:11 +01:00
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Turn the switch on."""
|
2015-11-25 18:07:29 +01:00
|
|
|
try:
|
2016-07-15 18:02:20 +02:00
|
|
|
self.plug.set_relay_on()
|
2020-04-04 16:09:49 +02:00
|
|
|
except MyStromConnectionError:
|
2018-06-10 11:38:23 +02:00
|
|
|
_LOGGER.error("No route to device: %s", self._resource)
|
2015-11-25 17:21:11 +01:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Turn the switch off."""
|
2015-11-25 18:07:29 +01:00
|
|
|
try:
|
2016-07-15 18:02:20 +02:00
|
|
|
self.plug.set_relay_off()
|
2020-04-04 16:09:49 +02:00
|
|
|
except MyStromConnectionError:
|
2018-06-10 11:38:23 +02:00
|
|
|
_LOGGER.error("No route to device: %s", self._resource)
|
2015-11-25 17:21:11 +01:00
|
|
|
|
|
|
|
def update(self):
|
2016-07-15 18:02:20 +02:00
|
|
|
"""Get the latest data from the device and update the data."""
|
2015-11-25 17:21:11 +01:00
|
|
|
try:
|
2016-07-15 18:02:20 +02:00
|
|
|
self.data = self.plug.get_status()
|
2020-02-01 23:04:42 +01:00
|
|
|
self._available = True
|
2020-04-04 16:09:49 +02:00
|
|
|
except MyStromConnectionError:
|
2019-07-31 12:25:30 -07:00
|
|
|
self.data = {"power": 0, "relay": False}
|
2020-02-01 23:04:42 +01:00
|
|
|
self._available = False
|
2018-06-10 11:38:23 +02:00
|
|
|
_LOGGER.error("No route to device: %s", self._resource)
|