mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Add PlatformNotReady
This commit is contained in:
parent
3aeaf3bb96
commit
29aa1463ef
@ -5,6 +5,7 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
|
||||||
from homeassistant.const import CONF_HOST, CONF_NAME
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
||||||
|
from homeassistant.exceptions import PlatformNotReady
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
DEFAULT_NAME = "myStrom Switch"
|
DEFAULT_NAME = "myStrom Switch"
|
||||||
@ -30,7 +31,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
MyStromPlug(host).get_status()
|
MyStromPlug(host).get_status()
|
||||||
except exceptions.MyStromConnectionError:
|
except exceptions.MyStromConnectionError:
|
||||||
_LOGGER.error("No route to device: %s", host)
|
_LOGGER.error("No route to device: %s", host)
|
||||||
return
|
raise PlatformNotReady()
|
||||||
|
|
||||||
add_entities([MyStromSwitch(name, host)])
|
add_entities([MyStromSwitch(name, host)])
|
||||||
|
|
||||||
@ -46,7 +47,7 @@ class MyStromSwitch(SwitchDevice):
|
|||||||
self._resource = resource
|
self._resource = resource
|
||||||
self.data = {}
|
self.data = {}
|
||||||
self.plug = MyStromPlug(self._resource)
|
self.plug = MyStromPlug(self._resource)
|
||||||
self.update()
|
self._available = True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -63,6 +64,11 @@ class MyStromSwitch(SwitchDevice):
|
|||||||
"""Return the current power consumption in W."""
|
"""Return the current power consumption in W."""
|
||||||
return round(self.data["power"], 2)
|
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):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the switch on."""
|
"""Turn the switch on."""
|
||||||
from pymystrom import exceptions
|
from pymystrom import exceptions
|
||||||
@ -87,6 +93,8 @@ class MyStromSwitch(SwitchDevice):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
self.data = self.plug.get_status()
|
self.data = self.plug.get_status()
|
||||||
|
self._available = True
|
||||||
except exceptions.MyStromConnectionError:
|
except exceptions.MyStromConnectionError:
|
||||||
self.data = {"power": 0, "relay": False}
|
self.data = {"power": 0, "relay": False}
|
||||||
|
self._available = False
|
||||||
_LOGGER.error("No route to device: %s", self._resource)
|
_LOGGER.error("No route to device: %s", self._resource)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user