Skip to content

Commit

Permalink
Device support of the Xiaomi Power Strip updated (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored and rytilahti committed Sep 26, 2017
1 parent e8ed75a commit 82f38b8
Showing 1 changed file with 56 additions and 34 deletions.
90 changes: 56 additions & 34 deletions mirobo/strip.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,23 @@
from .device import Device
from typing import Any, Dict
import logging
import enum
from typing import Dict, Any, Optional
from collections import defaultdict
from .device import Device

_LOGGER = logging.getLogger(__name__)


class PowerMode(enum.Enum):
Eco = 'green'
Normal = 'normal'


class Strip(Device):
"""Main class representing the smart strip."""

def on(self):
"""Power on."""
return self.send("set_power", ["on"])

def off(self):
"""Power off."""
return self.send("set_power", ["off"])

def status(self):
"""Retrieve properties."""
properties = ['power', 'temperature', 'current', 'mode']
values = self.send(
"get_prop",
properties
)
return StripStatus(dict(zip(properties, values)))

def set_power_mode(self, mode: PowerMode):
"""Set mode."""

# green, normal
return self.send("set_power_mode", [mode.value])


class StripStatus:
"""Container for status reports from the strip."""

def __init__(self, data: Dict[str, Any]) -> None:
# Device model: qmi.powerstrip.v1, zimi.powerstrip.v2
#
# {'power': 'on', 'temperature': 48.11,
# 'current': 0.06, 'mode': 'green'}
self.data = data
Expand All @@ -56,16 +35,59 @@ def temperature(self) -> float:
return self.data["temperature"]

@property
def current(self) -> float:
return self.data["current"]
def load_power(self) -> Optional[float]:
if self.data["current"] is not None:
# The constant of 110V is used intentionally. The current was
# calculated with a wrong reference voltage already.
return self.data["current"] * 110
return None

@property
def mode(self) -> PowerMode:
return PowerMode(self.data["mode"])

def __str__(self) -> str:
s = "<StripStatus power=%s, temperature=%s, " \
"current=%s mode=%s>" % \
(self.power, self.temperature,
self.current, self.mode)
"load_power=%s mode=%s>" % \
(self.power,
self.temperature,
self.load_power,
self.mode)
return s


class Strip(Device):
"""Main class representing the smart strip."""

def on(self):
"""Power on."""
return self.send("set_power", ["on"])

def off(self):
"""Power off."""
return self.send("set_power", ["off"])

def status(self):
"""Retrieve properties."""
properties = ['power', 'temperature', 'current', 'mode']
values = self.send(
"get_prop",
properties
)

properties_count = len(properties)
values_count = len(values)
if properties_count != values_count:
_LOGGER.debug(
"Count (%s) of requested properties does not match the "
"count (%s) of received values.",
properties_count, values_count)

return StripStatus(
defaultdict(lambda: None, zip(properties, values)))

def set_power_mode(self, mode: PowerMode):
"""Set mode."""

# green, normal
return self.send("set_power_mode", [mode.value])

0 comments on commit 82f38b8

Please sign in to comment.