Skip to content

Commit

Permalink
Partial reversal of PR #141 (#151)
Browse files Browse the repository at this point in the history
* Fixed has_light_control check
* Removed GU10 specific things from tests. The GU10 profile was that of a wrongly paired lamp. We shouldn't test that.
* Improved coverage for device.py
  • Loading branch information
Lakitna authored and Patrik committed Feb 1, 2018
1 parent e7fa96a commit f2a11b2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 65 deletions.
32 changes: 9 additions & 23 deletions pytradfri/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
RANGE_BRIGHTNESS,
RANGE_X,
RANGE_Y,
ROOT_SWITCH,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR_TEMP,
SUPPORT_HEX_COLOR,
Expand All @@ -35,14 +34,6 @@
from .error import ColorError


def device_type(data):
"""If regular bulb=>ATTR_LIGHT_CONTROL If GU10 light=>ROOT_SWITCH."""
if ATTR_LIGHT_CONTROL in data:
return ATTR_LIGHT_CONTROL
elif ROOT_SWITCH in data:
return ROOT_SWITCH


class Device(ApiResource):
"""Base class for devices."""

Expand Down Expand Up @@ -71,13 +62,11 @@ def reachable(self):
@property
def has_light_control(self):
return (self.raw is not None and
(len(self.raw.get(ATTR_LIGHT_CONTROL, "")) > 0 or
len(self.raw.get(ROOT_SWITCH, "")) > 0)
)
len(self.raw.get(ATTR_LIGHT_CONTROL, "")) > 0)

@property
def light_control(self):
return DeviceControl(self)
return LightControl(self)

def __repr__(self):
return "<{} - {} ({})>".format(self.id, self.name,
Expand Down Expand Up @@ -151,16 +140,16 @@ def raw(self):
return self._device.raw[ATTR_DEVICE_INFO]


class DeviceControl:
"""Class to control devices."""
class LightControl:
"""Class to control the lights."""

def __init__(self, device):
self._device = device

@property
def raw(self):
"""Return raw data that it represents."""
return self._device.raw[device_type(self._device.raw)]
return self._device.raw[ATTR_LIGHT_CONTROL]

@property
def lights(self):
Expand Down Expand Up @@ -274,14 +263,14 @@ def set_values(self, values, *, index=0):
'Only devices with 1 light supported'

return Command('put', self._device.path, {
device_type(self._device.raw): [
ATTR_LIGHT_CONTROL: [
values
]
})

def __repr__(self):
return '<DeviceControl for {} ({} lights)>'.format(self._device.name,
len(self.raw))
return '<LightControl for {} ({} lights)>'.format(self._device.name,
len(self.raw))


class Light:
Expand Down Expand Up @@ -335,10 +324,7 @@ def hsb_xy_color(self):
@property
def raw(self):
"""Return raw data that it represents."""
if ATTR_LIGHT_CONTROL in self.device.raw:
return self.device.raw[device_type(self.device.raw)][self.index]
elif ROOT_SWITCH in self.device.raw:
return self.device.raw[device_type(self.device.raw)][self.index]
return self.device.raw[ATTR_LIGHT_CONTROL][self.index]

def __repr__(self):
state = "on" if self.state else "off"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()

VERSION = "5.3.0"
VERSION = "5.3.1"
DOWNLOAD_URL = \
'https://github.com/ggravlingen/pytradfri/archive/{}.zip'.format(VERSION)

Expand Down
30 changes: 0 additions & 30 deletions tests/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,36 +123,6 @@
'9054': 0,
}

# Retrieved from Gateway running on 1.3.002
LIGHT_GU10 = {
"9001": "mhlund #1",
"5750": 0,
"9002": 1515435391,
"9020": 1516008285,
"9003": 65540,
"9054": 0,
"9019": 1,
"3": {
"0": "IKEA of Sweden",
"1": "TRADFRI bulb GU10 WS 400lm",
"2": "",
"3": "1.2.217",
"6": 1
},
"15009": [
{
"5851": 10,
"5850": 0,
"5717": 0,
"5711": 450,
"5709": 30138,
"5710": 26909,
"5706": "efd275",
"9003": 0
}
]
}

# Retrieved from Gateway running on 1.2.42
REMOTE_CONTROL = {
'3': {'0': 'IKEA of Sweden',
Expand Down
29 changes: 27 additions & 2 deletions tests/test_device.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from datetime import datetime
import pytest

from pytradfri.const import ROOT_DEVICES, ATTR_NAME, ATTR_LIGHT_CONTROL
from pytradfri.const import (
ROOT_DEVICES, ATTR_NAME, ATTR_LIGHT_CONTROL, ATTR_LAST_SEEN)
from pytradfri.device import Device
from devices import LIGHT_WS, LIGHT_CWS
from devices import (
LIGHT_W, LIGHT_WS, LIGHT_CWS, REMOTE_CONTROL, MOTION_SENSOR)


def test_device_properties():
Expand Down Expand Up @@ -145,6 +147,29 @@ def test_set_hex_color():
assert len(data) is 1


def test_has_light_control():
dev = Device(LIGHT_WS)
assert dev.has_light_control is True
dev = Device(LIGHT_CWS)
assert dev.has_light_control is True
dev = Device(LIGHT_W)
assert dev.has_light_control is True
dev = Device(REMOTE_CONTROL)
assert dev.has_light_control is False
dev = Device(MOTION_SENSOR)
assert dev.has_light_control is False


def test_last_seen():
dev = Device(LIGHT_WS)
assert dev.last_seen is not None

tmp = LIGHT_WS
del tmp[ATTR_LAST_SEEN]
dev = Device(tmp)
assert dev.last_seen is None


def test_value_validate():
dev = Device(LIGHT_WS)
rnge = (10, 100)
Expand Down
10 changes: 1 addition & 9 deletions tests/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,14 @@
LIGHT_WS,
LIGHT_WS_CUSTOM_COLOR,
LIGHT_CWS,
LIGHT_CWS_CUSTOM_COLOR,
LIGHT_GU10
LIGHT_CWS_CUSTOM_COLOR
)


def light(raw):
return Device(raw).light_control.lights[0]


def test_gu10():
bulb = light(LIGHT_GU10)

assert bulb.hex_color == 'efd275'
assert bulb.xy_color == (30138, 26909)


def test_white_bulb():
bulb = light(LIGHT_W)

Expand Down

0 comments on commit f2a11b2

Please sign in to comment.