Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions homeassistant/components/mystrom/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"""Constants for the myStrom integration."""
DOMAIN = "mystrom"
54 changes: 30 additions & 24 deletions homeassistant/components/mystrom/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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__)
Expand All @@ -39,28 +40,29 @@
)


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)
Comment thread
balloob marked this conversation as resolved.
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
Expand All @@ -69,12 +71,18 @@ def __init__(self, bulb, name):
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."""
Expand Down Expand Up @@ -103,11 +111,10 @@ def effect_list(self):
@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)

Expand All @@ -121,33 +128,32 @@ def turn_on(self, **kwargs):

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:
Expand All @@ -160,5 +166,5 @@ def update(self):

self._available = True
except MyStromConnectionError:
_LOGGER.warning("No route to device")
_LOGGER.warning("No route to myStrom bulb")
self._available = False
2 changes: 1 addition & 1 deletion homeassistant/components/mystrom/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
55 changes: 30 additions & 25 deletions homeassistant/components/mystrom/switch.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,30 +22,30 @@
)


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()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was missing from light.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it was.


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):
Expand All @@ -55,38 +55,43 @@ def name(self):
@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")
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,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
Expand Down