Move myStrom light and switch to async (#34079)
This commit is contained in:
parent
afe15a2e6b
commit
637ff5698c
5 changed files with 64 additions and 51 deletions
2
homeassistant/components/mystrom/const.py
Normal file
2
homeassistant/components/mystrom/const.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
"""Constants for the myStrom integration."""
|
||||
DOMAIN = "mystrom"
|
|
@ -17,6 +17,7 @@ from homeassistant.components.light import (
|
|||
Light,
|
||||
)
|
||||
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME
|
||||
from homeassistant.exceptions import PlatformNotReady
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -39,28 +40,29 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the myStrom Light platform."""
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up the myStrom light integration."""
|
||||
host = config.get(CONF_HOST)
|
||||
mac = config.get(CONF_MAC)
|
||||
name = config.get(CONF_NAME)
|
||||
|
||||
bulb = MyStromBulb(host, mac)
|
||||
try:
|
||||
if bulb.get_status()["type"] != "rgblamp":
|
||||
await bulb.get_state()
|
||||
if bulb.bulb_type != "rgblamp":
|
||||
_LOGGER.error("Device %s (%s) is not a myStrom bulb", host, mac)
|
||||
return
|
||||
except MyStromConnectionError:
|
||||
_LOGGER.warning("No route to device: %s", host)
|
||||
_LOGGER.warning("No route to myStrom bulb: %s", host)
|
||||
raise PlatformNotReady()
|
||||
|
||||
add_entities([MyStromLight(bulb, name)], True)
|
||||
async_add_entities([MyStromLight(bulb, name, mac)], True)
|
||||
|
||||
|
||||
class MyStromLight(Light):
|
||||
"""Representation of the myStrom WiFi Bulb."""
|
||||
"""Representation of the myStrom WiFi bulb."""
|
||||
|
||||
def __init__(self, bulb, name):
|
||||
def __init__(self, bulb, name, mac):
|
||||
"""Initialize the light."""
|
||||
self._bulb = bulb
|
||||
self._name = name
|
||||
|
@ -69,12 +71,18 @@ class MyStromLight(Light):
|
|||
self._brightness = 0
|
||||
self._color_h = 0
|
||||
self._color_s = 0
|
||||
self._mac = mac
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the display name of this light."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return a unique ID."""
|
||||
return self._mac
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
"""Flag supported features."""
|
||||
|
@ -103,11 +111,10 @@ class MyStromLight(Light):
|
|||
@property
|
||||
def is_on(self):
|
||||
"""Return true if light is on."""
|
||||
return self._state["on"] if self._state is not None else None
|
||||
return self._state
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
async def async_turn_on(self, **kwargs):
|
||||
"""Turn on the light."""
|
||||
|
||||
brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
|
||||
effect = kwargs.get(ATTR_EFFECT)
|
||||
|
||||
|
@ -121,33 +128,32 @@ class MyStromLight(Light):
|
|||
|
||||
try:
|
||||
if not self.is_on:
|
||||
self._bulb.set_on()
|
||||
await self._bulb.set_on()
|
||||
if brightness is not None:
|
||||
self._bulb.set_color_hsv(
|
||||
await self._bulb.set_color_hsv(
|
||||
int(color_h), int(color_s), round(brightness * 100 / 255)
|
||||
)
|
||||
if effect == EFFECT_SUNRISE:
|
||||
self._bulb.set_sunrise(30)
|
||||
await self._bulb.set_sunrise(30)
|
||||
if effect == EFFECT_RAINBOW:
|
||||
self._bulb.set_rainbow(30)
|
||||
await self._bulb.set_rainbow(30)
|
||||
except MyStromConnectionError:
|
||||
_LOGGER.warning("No route to device")
|
||||
_LOGGER.warning("No route to myStrom bulb")
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
async def async_turn_off(self, **kwargs):
|
||||
"""Turn off the bulb."""
|
||||
|
||||
try:
|
||||
self._bulb.set_off()
|
||||
await self._bulb.set_off()
|
||||
except MyStromConnectionError:
|
||||
_LOGGER.warning("myStrom bulb not online")
|
||||
|
||||
def update(self):
|
||||
async def async_update(self):
|
||||
"""Fetch new state data for this light."""
|
||||
|
||||
try:
|
||||
self._state = self._bulb.get_status()
|
||||
await self._bulb.get_state()
|
||||
self._state = self._bulb.state
|
||||
|
||||
colors = self._bulb.get_color()["color"]
|
||||
colors = self._bulb.color
|
||||
try:
|
||||
color_h, color_s, color_v = colors.split(";")
|
||||
except ValueError:
|
||||
|
@ -160,5 +166,5 @@ class MyStromLight(Light):
|
|||
|
||||
self._available = True
|
||||
except MyStromConnectionError:
|
||||
_LOGGER.warning("No route to device")
|
||||
_LOGGER.warning("No route to myStrom bulb")
|
||||
self._available = False
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
"domain": "mystrom",
|
||||
"name": "myStrom",
|
||||
"documentation": "https://www.home-assistant.io/integrations/mystrom",
|
||||
"requirements": ["python-mystrom==0.5.0"],
|
||||
"requirements": ["python-mystrom==1.1.2"],
|
||||
"dependencies": ["http"],
|
||||
"codeowners": ["@fabaff"]
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
"""Support for myStrom switches."""
|
||||
"""Support for myStrom switches/plugs."""
|
||||
import logging
|
||||
|
||||
from pymystrom.exceptions import MyStromConnectionError
|
||||
from pymystrom.switch import MyStromPlug
|
||||
from pymystrom.switch import MyStromSwitch as _MyStromSwitch
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
|
||||
|
@ -22,30 +22,30 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Find and return myStrom switch."""
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up the myStrom switch/plug integration."""
|
||||
name = config.get(CONF_NAME)
|
||||
host = config.get(CONF_HOST)
|
||||
|
||||
try:
|
||||
MyStromPlug(host).get_status()
|
||||
plug = _MyStromSwitch(host)
|
||||
await plug.get_state()
|
||||
except MyStromConnectionError:
|
||||
_LOGGER.error("No route to device: %s", host)
|
||||
_LOGGER.error("No route to myStrom plug: %s", host)
|
||||
raise PlatformNotReady()
|
||||
|
||||
add_entities([MyStromSwitch(name, host)])
|
||||
async_add_entities([MyStromSwitch(plug, name)])
|
||||
|
||||
|
||||
class MyStromSwitch(SwitchDevice):
|
||||
"""Representation of a myStrom switch."""
|
||||
"""Representation of a myStrom switch/plug."""
|
||||
|
||||
def __init__(self, name, resource):
|
||||
"""Initialize the myStrom switch."""
|
||||
def __init__(self, plug, name):
|
||||
"""Initialize the myStrom switch/plug."""
|
||||
self._name = name
|
||||
self._resource = resource
|
||||
self.data = {}
|
||||
self.plug = MyStromPlug(self._resource)
|
||||
self.plug = plug
|
||||
self._available = True
|
||||
self.relay = None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
|
@ -55,38 +55,43 @@ class MyStromSwitch(SwitchDevice):
|
|||
@property
|
||||
def is_on(self):
|
||||
"""Return true if switch is on."""
|
||||
return bool(self.data["relay"])
|
||||
return bool(self.relay)
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return a unique ID."""
|
||||
return self.plug._mac # pylint: disable=protected-access
|
||||
|
||||
@property
|
||||
def current_power_w(self):
|
||||
"""Return the current power consumption in W."""
|
||||
return round(self.data["power"], 2)
|
||||
return self.plug.consumption
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Could the device be accessed during the last update call."""
|
||||
return self._available
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
async def async_turn_on(self, **kwargs):
|
||||
"""Turn the switch on."""
|
||||
try:
|
||||
self.plug.set_relay_on()
|
||||
await self.plug.turn_on()
|
||||
except MyStromConnectionError:
|
||||
_LOGGER.error("No route to device: %s", self._resource)
|
||||
_LOGGER.error("No route to myStrom plug")
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
async def async_turn_off(self, **kwargs):
|
||||
"""Turn the switch off."""
|
||||
try:
|
||||
self.plug.set_relay_off()
|
||||
await self.plug.turn_off()
|
||||
except MyStromConnectionError:
|
||||
_LOGGER.error("No route to device: %s", self._resource)
|
||||
_LOGGER.error("No route to myStrom plug")
|
||||
|
||||
def update(self):
|
||||
async def async_update(self):
|
||||
"""Get the latest data from the device and update the data."""
|
||||
try:
|
||||
self.data = self.plug.get_status()
|
||||
await self.plug.get_state()
|
||||
self.relay = self.plug.relay
|
||||
self._available = True
|
||||
except MyStromConnectionError:
|
||||
self.data = {"power": 0, "relay": False}
|
||||
self._available = False
|
||||
_LOGGER.error("No route to device: %s", self._resource)
|
||||
_LOGGER.error("No route to myStrom plug")
|
||||
|
|
|
@ -1659,7 +1659,7 @@ python-miio==0.5.0.1
|
|||
python-mpd2==1.0.0
|
||||
|
||||
# homeassistant.components.mystrom
|
||||
python-mystrom==0.5.0
|
||||
python-mystrom==1.1.2
|
||||
|
||||
# homeassistant.components.nest
|
||||
python-nest==4.1.0
|
||||
|
|
Loading…
Add table
Reference in a new issue