Add PlatformNotReady

This commit is contained in:
Fabian Affolter 2020-02-01 23:04:42 +01:00
parent 3aeaf3bb96
commit 29aa1463ef
No known key found for this signature in database
GPG Key ID: E23CD2DD36A4397F

View File

@ -5,6 +5,7 @@ import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.exceptions import PlatformNotReady
import homeassistant.helpers.config_validation as cv
DEFAULT_NAME = "myStrom Switch"
@ -30,7 +31,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
MyStromPlug(host).get_status()
except exceptions.MyStromConnectionError:
_LOGGER.error("No route to device: %s", host)
return
raise PlatformNotReady()
add_entities([MyStromSwitch(name, host)])
@ -46,7 +47,7 @@ class MyStromSwitch(SwitchDevice):
self._resource = resource
self.data = {}
self.plug = MyStromPlug(self._resource)
self.update()
self._available = True
@property
def name(self):
@ -63,6 +64,11 @@ class MyStromSwitch(SwitchDevice):
"""Return the current power consumption in W."""
return round(self.data["power"], 2)
@property
def available(self):
"""Could the device be accessed during the last update call."""
return self._available
def turn_on(self, **kwargs):
"""Turn the switch on."""
from pymystrom import exceptions
@ -87,6 +93,8 @@ class MyStromSwitch(SwitchDevice):
try:
self.data = self.plug.get_status()
self._available = True
except exceptions.MyStromConnectionError:
self.data = {"power": 0, "relay": False}
self._available = False
_LOGGER.error("No route to device: %s", self._resource)