mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Move myStrom light and switch to async (#34079)
This commit is contained in:
parent
afe15a2e6b
commit
637ff5698c
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,
|
Light,
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME
|
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME
|
||||||
|
from homeassistant.exceptions import PlatformNotReady
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -39,28 +40,29 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||||
"""Set up the myStrom Light platform."""
|
"""Set up the myStrom light integration."""
|
||||||
|
|
||||||
host = config.get(CONF_HOST)
|
host = config.get(CONF_HOST)
|
||||||
mac = config.get(CONF_MAC)
|
mac = config.get(CONF_MAC)
|
||||||
name = config.get(CONF_NAME)
|
name = config.get(CONF_NAME)
|
||||||
|
|
||||||
bulb = MyStromBulb(host, mac)
|
bulb = MyStromBulb(host, mac)
|
||||||
try:
|
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)
|
_LOGGER.error("Device %s (%s) is not a myStrom bulb", host, mac)
|
||||||
return
|
return
|
||||||
except MyStromConnectionError:
|
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):
|
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."""
|
"""Initialize the light."""
|
||||||
self._bulb = bulb
|
self._bulb = bulb
|
||||||
self._name = name
|
self._name = name
|
||||||
@ -69,12 +71,18 @@ class MyStromLight(Light):
|
|||||||
self._brightness = 0
|
self._brightness = 0
|
||||||
self._color_h = 0
|
self._color_h = 0
|
||||||
self._color_s = 0
|
self._color_s = 0
|
||||||
|
self._mac = mac
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the display name of this light."""
|
"""Return the display name of this light."""
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return a unique ID."""
|
||||||
|
return self._mac
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_features(self):
|
def supported_features(self):
|
||||||
"""Flag supported features."""
|
"""Flag supported features."""
|
||||||
@ -103,11 +111,10 @@ class MyStromLight(Light):
|
|||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if light is on."""
|
"""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."""
|
"""Turn on the light."""
|
||||||
|
|
||||||
brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
|
brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
|
||||||
effect = kwargs.get(ATTR_EFFECT)
|
effect = kwargs.get(ATTR_EFFECT)
|
||||||
|
|
||||||
@ -121,33 +128,32 @@ class MyStromLight(Light):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
if not self.is_on:
|
if not self.is_on:
|
||||||
self._bulb.set_on()
|
await self._bulb.set_on()
|
||||||
if brightness is not None:
|
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)
|
int(color_h), int(color_s), round(brightness * 100 / 255)
|
||||||
)
|
)
|
||||||
if effect == EFFECT_SUNRISE:
|
if effect == EFFECT_SUNRISE:
|
||||||
self._bulb.set_sunrise(30)
|
await self._bulb.set_sunrise(30)
|
||||||
if effect == EFFECT_RAINBOW:
|
if effect == EFFECT_RAINBOW:
|
||||||
self._bulb.set_rainbow(30)
|
await self._bulb.set_rainbow(30)
|
||||||
except MyStromConnectionError:
|
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."""
|
"""Turn off the bulb."""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._bulb.set_off()
|
await self._bulb.set_off()
|
||||||
except MyStromConnectionError:
|
except MyStromConnectionError:
|
||||||
_LOGGER.warning("myStrom bulb not online")
|
_LOGGER.warning("myStrom bulb not online")
|
||||||
|
|
||||||
def update(self):
|
async def async_update(self):
|
||||||
"""Fetch new state data for this light."""
|
"""Fetch new state data for this light."""
|
||||||
|
|
||||||
try:
|
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:
|
try:
|
||||||
color_h, color_s, color_v = colors.split(";")
|
color_h, color_s, color_v = colors.split(";")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
@ -160,5 +166,5 @@ class MyStromLight(Light):
|
|||||||
|
|
||||||
self._available = True
|
self._available = True
|
||||||
except MyStromConnectionError:
|
except MyStromConnectionError:
|
||||||
_LOGGER.warning("No route to device")
|
_LOGGER.warning("No route to myStrom bulb")
|
||||||
self._available = False
|
self._available = False
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
"domain": "mystrom",
|
"domain": "mystrom",
|
||||||
"name": "myStrom",
|
"name": "myStrom",
|
||||||
"documentation": "https://www.home-assistant.io/integrations/mystrom",
|
"documentation": "https://www.home-assistant.io/integrations/mystrom",
|
||||||
"requirements": ["python-mystrom==0.5.0"],
|
"requirements": ["python-mystrom==1.1.2"],
|
||||||
"dependencies": ["http"],
|
"dependencies": ["http"],
|
||||||
"codeowners": ["@fabaff"]
|
"codeowners": ["@fabaff"]
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
"""Support for myStrom switches."""
|
"""Support for myStrom switches/plugs."""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from pymystrom.exceptions import MyStromConnectionError
|
from pymystrom.exceptions import MyStromConnectionError
|
||||||
from pymystrom.switch import MyStromPlug
|
from pymystrom.switch import MyStromSwitch as _MyStromSwitch
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
|
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):
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||||
"""Find and return myStrom switch."""
|
"""Set up the myStrom switch/plug integration."""
|
||||||
name = config.get(CONF_NAME)
|
name = config.get(CONF_NAME)
|
||||||
host = config.get(CONF_HOST)
|
host = config.get(CONF_HOST)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
MyStromPlug(host).get_status()
|
plug = _MyStromSwitch(host)
|
||||||
|
await plug.get_state()
|
||||||
except MyStromConnectionError:
|
except MyStromConnectionError:
|
||||||
_LOGGER.error("No route to device: %s", host)
|
_LOGGER.error("No route to myStrom plug: %s", host)
|
||||||
raise PlatformNotReady()
|
raise PlatformNotReady()
|
||||||
|
|
||||||
add_entities([MyStromSwitch(name, host)])
|
async_add_entities([MyStromSwitch(plug, name)])
|
||||||
|
|
||||||
|
|
||||||
class MyStromSwitch(SwitchDevice):
|
class MyStromSwitch(SwitchDevice):
|
||||||
"""Representation of a myStrom switch."""
|
"""Representation of a myStrom switch/plug."""
|
||||||
|
|
||||||
def __init__(self, name, resource):
|
def __init__(self, plug, name):
|
||||||
"""Initialize the myStrom switch."""
|
"""Initialize the myStrom switch/plug."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._resource = resource
|
self.plug = plug
|
||||||
self.data = {}
|
|
||||||
self.plug = MyStromPlug(self._resource)
|
|
||||||
self._available = True
|
self._available = True
|
||||||
|
self.relay = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -55,38 +55,43 @@ class MyStromSwitch(SwitchDevice):
|
|||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if switch is on."""
|
"""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
|
@property
|
||||||
def current_power_w(self):
|
def current_power_w(self):
|
||||||
"""Return the current power consumption in W."""
|
"""Return the current power consumption in W."""
|
||||||
return round(self.data["power"], 2)
|
return self.plug.consumption
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self):
|
||||||
"""Could the device be accessed during the last update call."""
|
"""Could the device be accessed during the last update call."""
|
||||||
return self._available
|
return self._available
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Turn the switch on."""
|
"""Turn the switch on."""
|
||||||
try:
|
try:
|
||||||
self.plug.set_relay_on()
|
await self.plug.turn_on()
|
||||||
except MyStromConnectionError:
|
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."""
|
"""Turn the switch off."""
|
||||||
try:
|
try:
|
||||||
self.plug.set_relay_off()
|
await self.plug.turn_off()
|
||||||
except MyStromConnectionError:
|
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."""
|
"""Get the latest data from the device and update the data."""
|
||||||
try:
|
try:
|
||||||
self.data = self.plug.get_status()
|
await self.plug.get_state()
|
||||||
|
self.relay = self.plug.relay
|
||||||
self._available = True
|
self._available = True
|
||||||
except MyStromConnectionError:
|
except MyStromConnectionError:
|
||||||
self.data = {"power": 0, "relay": False}
|
|
||||||
self._available = 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
|
python-mpd2==1.0.0
|
||||||
|
|
||||||
# homeassistant.components.mystrom
|
# homeassistant.components.mystrom
|
||||||
python-mystrom==0.5.0
|
python-mystrom==1.1.2
|
||||||
|
|
||||||
# homeassistant.components.nest
|
# homeassistant.components.nest
|
||||||
python-nest==4.1.0
|
python-nest==4.1.0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user