Skip to content

Commit

Permalink
Merge e12db5e into c89f50b
Browse files Browse the repository at this point in the history
  • Loading branch information
farmio committed Oct 7, 2018
2 parents c89f50b + e12db5e commit f96ea72
Show file tree
Hide file tree
Showing 10 changed files with 462 additions and 23 deletions.
216 changes: 209 additions & 7 deletions test/light_test.py
Expand Up @@ -65,6 +65,46 @@ def test_supports_color_false(self):
group_address_switch='1/6/4')
self.assertFalse(light.supports_color)

#
# TEST SUPPORT TUNABLE WHITE
#
def test_supports_tw_yes(self):
"""Test supports_tw attribute with a light with tunable white function."""
xknx = XKNX(loop=self.loop)
light = Light(xknx,
'Diningroom.Light_1',
group_address_switch='1/6/4',
group_address_tunable_white='1/6/6')
self.assertTrue(light.supports_tunable_white)

def test_supports_tw_no(self):
"""Test supports_tw attribute with a Light without tunable white function."""
xknx = XKNX(loop=self.loop)
light = Light(xknx,
'Diningroom.Light_1',
group_address_switch='1/6/4')
self.assertFalse(light.supports_tunable_white)

#
# TEST SUPPORT COLOR TEMPERATURE
#
def test_supports_color_temp_true(self):
"""Test supports_color_temp attribute with a light with color temperature function."""
xknx = XKNX(loop=self.loop)
light = Light(xknx,
'Diningroom.Light_1',
group_address_switch='1/6/4',
group_address_color_temperature='1/6/6')
self.assertTrue(light.supports_color_temperature)

def test_supports_color_temp_false(self):
"""Test supports_color_temp attribute with a Light without color temperature function."""
xknx = XKNX(loop=self.loop)
light = Light(xknx,
'Diningroom.Light_1',
group_address_switch='1/6/4')
self.assertFalse(light.supports_color_temperature)

#
# SYNC
#
Expand All @@ -75,10 +115,12 @@ def test_sync(self):
name="TestLight",
group_address_switch_state='1/2/3',
group_address_brightness_state='1/2/5',
group_address_color_state='1/2/6')
group_address_color_state='1/2/6',
group_address_tunable_white_state='1/2/7',
group_address_color_temperature_state='1/2/8')
self.loop.run_until_complete(asyncio.Task(light.sync(False)))

self.assertEqual(xknx.telegrams.qsize(), 3)
self.assertEqual(xknx.telegrams.qsize(), 5)

telegram1 = xknx.telegrams.get_nowait()
self.assertEqual(telegram1,
Expand All @@ -92,6 +134,14 @@ def test_sync(self):
self.assertEqual(telegram3,
Telegram(GroupAddress('1/2/5'), TelegramType.GROUP_READ))

telegram4 = xknx.telegrams.get_nowait()
self.assertEqual(telegram4,
Telegram(GroupAddress('1/2/7'), TelegramType.GROUP_READ))

telegram5 = xknx.telegrams.get_nowait()
self.assertEqual(telegram5,
Telegram(GroupAddress('1/2/8'), TelegramType.GROUP_READ))

#
# SYNC WITH STATE ADDRESS
#
Expand All @@ -105,10 +155,14 @@ def test_sync_state_address(self):
group_address_brightness='1/2/5',
group_address_brightness_state='1/2/6',
group_address_color='1/2/7',
group_address_color_state='1/2/8')
group_address_color_state='1/2/8',
group_address_tunable_white='1/2/9',
group_address_tunable_white_state='1/2/10',
group_address_color_temperature='1/2/11',
group_address_color_temperature_state='1/2/12')
self.loop.run_until_complete(asyncio.Task(light.sync(False)))

self.assertEqual(xknx.telegrams.qsize(), 3)
self.assertEqual(xknx.telegrams.qsize(), 5)

telegram1 = xknx.telegrams.get_nowait()
self.assertEqual(telegram1,
Expand All @@ -119,6 +173,12 @@ def test_sync_state_address(self):
telegram3 = xknx.telegrams.get_nowait()
self.assertEqual(telegram3,
Telegram(GroupAddress('1/2/6'), TelegramType.GROUP_READ))
telegram4 = xknx.telegrams.get_nowait()
self.assertEqual(telegram4,
Telegram(GroupAddress('1/2/10'), TelegramType.GROUP_READ))
telegram5 = xknx.telegrams.get_nowait()
self.assertEqual(telegram5,
Telegram(GroupAddress('1/2/12'), TelegramType.GROUP_READ))

#
# TEST SET ON
Expand Down Expand Up @@ -209,6 +269,62 @@ def test_set_color_not_possible(self):
self.assertEqual(xknx.telegrams.qsize(), 0)
mock_warn.assert_called_with('Colors not supported for device %s', 'TestLight')

#
# TEST SET TUNABLE WHITE
#
def test_set_tw(self):
"""Test setting the tunable white value of a Light."""
xknx = XKNX(loop=self.loop)
light = Light(xknx,
name="TestLight",
group_address_switch='1/2/3',
group_address_tunable_white='1/2/5')
self.loop.run_until_complete(asyncio.Task(light.set_tunable_white(23)))
self.assertEqual(xknx.telegrams.qsize(), 1)
telegram = xknx.telegrams.get_nowait()
self.assertEqual(telegram,
Telegram(GroupAddress('1/2/5'), payload=DPTArray(23)))

def test_set_tw_unsupported(self):
"""Test setting the tunable white value of a non tw Light."""
# pylint: disable=invalid-name
xknx = XKNX(loop=self.loop)
light = Light(xknx,
name="TestLight",
group_address_switch='1/2/3')
with patch('logging.Logger.warning') as mock_warn:
self.loop.run_until_complete(asyncio.Task(light.set_tunable_white(23)))
self.assertEqual(xknx.telegrams.qsize(), 0)
mock_warn.assert_called_with('Tunable white not supported for device %s', 'TestLight')

#
# TEST SET COLOR TEMPERATURE
#
def test_set_color_temp(self):
"""Test setting the color temperature value of a Light."""
xknx = XKNX(loop=self.loop)
light = Light(xknx,
name="TestLight",
group_address_switch='1/2/3',
group_address_color_temperature='1/2/5')
self.loop.run_until_complete(asyncio.Task(light.set_color_temperature(4000)))
self.assertEqual(xknx.telegrams.qsize(), 1)
telegram = xknx.telegrams.get_nowait()
self.assertEqual(telegram,
Telegram(GroupAddress('1/2/5'), payload=DPTArray((0x0F, 0xA0, ))))

def test_set_color_temp_unsupported(self):
"""Test setting the color temperature value of an unsupported Light."""
# pylint: disable=invalid-name
xknx = XKNX(loop=self.loop)
light = Light(xknx,
name="TestLight",
group_address_switch='1/2/3')
with patch('logging.Logger.warning') as mock_warn:
self.loop.run_until_complete(asyncio.Task(light.set_color_temperature(4000)))
self.assertEqual(xknx.telegrams.qsize(), 0)
mock_warn.assert_called_with('Absolute Color Temperature not supported for device %s', 'TestLight')

#
# TEST PROCESS
#
Expand Down Expand Up @@ -299,6 +415,78 @@ def test_process_color(self):
self.loop.run_until_complete(asyncio.Task(light.process(telegram)))
self.assertEqual(light.current_color, (23, 24, 25))

def test_process_tunable_white(self):
"""Test process / reading telegrams from telegram queue. Test if tunable white is processed."""
xknx = XKNX(loop=self.loop)
light = Light(xknx,
name="TestLight",
group_address_switch='1/2/3',
group_address_tunable_white='1/2/5')
self.assertEqual(light.current_tunable_white, None)

telegram = Telegram(GroupAddress('1/2/5'), payload=DPTArray(23))
self.loop.run_until_complete(asyncio.Task(light.process(telegram)))
self.assertEqual(light.current_tunable_white, 23)

def test_process_tunable_white_wrong_payload(self):
"""Test process wrong telegrams. (wrong payload type)."""
xknx = XKNX(loop=self.loop)
light = Light(xknx,
name="TestLight",
group_address_switch='1/2/3',
group_address_tunable_white='1/2/5')
telegram = Telegram(GroupAddress('1/2/5'), payload=DPTBinary(1))
with self.assertRaises(CouldNotParseTelegram):
self.loop.run_until_complete(asyncio.Task(light.process(telegram)))

def test_process_tunable_white_payload_invalid_length(self):
"""Test process wrong telegrams. (wrong payload length)."""
# pylint: disable=invalid-name
xknx = XKNX(loop=self.loop)
light = Light(xknx,
name="TestLight",
group_address_switch='1/2/3',
group_address_tunable_white='1/2/5')
telegram = Telegram(GroupAddress('1/2/5'), payload=DPTArray((23, 24)))
with self.assertRaises(CouldNotParseTelegram):
self.loop.run_until_complete(asyncio.Task(light.process(telegram)))

def test_process_color_temperature(self):
"""Test process / reading telegrams from telegram queue. Test if color temperature is processed."""
xknx = XKNX(loop=self.loop)
light = Light(xknx,
name="TestLight",
group_address_switch='1/2/3',
group_address_color_temperature='1/2/5')
self.assertEqual(light.current_color_temperature, None)

telegram = Telegram(GroupAddress('1/2/5'), payload=DPTArray((0x0F, 0xA0, )))
self.loop.run_until_complete(asyncio.Task(light.process(telegram)))
self.assertEqual(light.current_color_temperature, 4000)

def test_process_color_temperature_wrong_payload(self):
"""Test process wrong telegrams. (wrong payload type)."""
xknx = XKNX(loop=self.loop)
light = Light(xknx,
name="TestLight",
group_address_switch='1/2/3',
group_address_color_temperature='1/2/5')
telegram = Telegram(GroupAddress('1/2/5'), payload=DPTBinary(1))
with self.assertRaises(CouldNotParseTelegram):
self.loop.run_until_complete(asyncio.Task(light.process(telegram)))

def test_process_color_temperature_payload_invalid_length(self):
"""Test process wrong telegrams. (wrong payload length)."""
# pylint: disable=invalid-name
xknx = XKNX(loop=self.loop)
light = Light(xknx,
name="TestLight",
group_address_switch='1/2/3',
group_address_color_temperature='1/2/5')
telegram = Telegram(GroupAddress('1/2/5'), payload=DPTArray((23)))
with self.assertRaises(CouldNotParseTelegram):
self.loop.run_until_complete(asyncio.Task(light.process(telegram)))

#
# TEST DO
#
Expand All @@ -308,11 +496,17 @@ def test_do(self):
light = Light(xknx,
name="TestLight",
group_address_switch='1/2/3',
group_address_brightness='1/2/5')
group_address_brightness='1/2/5',
group_address_tunable_white='1/2/9',
group_address_color_temperature='1/2/11')
self.loop.run_until_complete(asyncio.Task(light.do("on")))
self.assertTrue(light.state)
self.loop.run_until_complete(asyncio.Task(light.do("brightness:80")))
self.assertEqual(light.current_brightness, 80)
self.loop.run_until_complete(asyncio.Task(light.do("tunable_white:80")))
self.assertEqual(light.current_tunable_white, 80)
self.loop.run_until_complete(asyncio.Task(light.do("color_temperature:3750")))
self.assertEqual(light.current_color_temperature, 3750)
self.loop.run_until_complete(asyncio.Task(light.do("off")))
self.assertFalse(light.state)

Expand All @@ -339,11 +533,19 @@ def test_has_group_address(self):
group_address_brightness='1/7/3',
group_address_brightness_state='1/7/4',
group_address_color='1/7/5',
group_address_color_state='1/7/6')
group_address_color_state='1/7/6',
group_address_tunable_white='1/7/7',
group_address_tunable_white_state='1/7/8',
group_address_color_temperature='1/7/9',
group_address_color_temperature_state='1/7/10')
self.assertTrue(light.has_group_address(GroupAddress('1/7/1')))
self.assertTrue(light.has_group_address(GroupAddress('1/7/2')))
self.assertTrue(light.has_group_address(GroupAddress('1/7/3')))
self.assertTrue(light.has_group_address(GroupAddress('1/7/4')))
self.assertTrue(light.has_group_address(GroupAddress('1/7/5')))
self.assertTrue(light.has_group_address(GroupAddress('1/7/6')))
self.assertFalse(light.has_group_address(GroupAddress('1/7/7')))
self.assertTrue(light.has_group_address(GroupAddress('1/7/7')))
self.assertTrue(light.has_group_address(GroupAddress('1/7/8')))
self.assertTrue(light.has_group_address(GroupAddress('1/7/9')))
self.assertTrue(light.has_group_address(GroupAddress('1/7/10')))
self.assertFalse(light.has_group_address(GroupAddress('1/7/11')))
99 changes: 99 additions & 0 deletions test/remote_value_dpt_2_byte_unsigned_test.py
@@ -0,0 +1,99 @@
"""Unit test for RemoteValueDpt2ByteUnsigned objects."""
import asyncio
import unittest

from xknx import XKNX
from xknx.knx import DPTArray, DPTBinary, Telegram, GroupAddress
from xknx.exceptions import ConversionError, CouldNotParseTelegram
from xknx.devices import RemoteValueDpt2ByteUnsigned


class TestRemoteValueDptValue1Ucount(unittest.TestCase):
"""Test class for RemoteValueDpt2ByteUnsigned objects."""

def setUp(self):
"""Set up test class."""
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)

def tearDown(self):
"""Tear down test class."""
self.loop.close()

def test_to_knx(self):
"""Test to_knx function with normal operation."""
xknx = XKNX(loop=self.loop)
remote_value = RemoteValueDpt2ByteUnsigned(xknx)
self.assertEqual(remote_value.to_knx(2571), DPTArray((0x0A, 0x0B)))

def test_from_knx(self):
"""Test from_knx function with normal operation."""
xknx = XKNX(loop=self.loop)
remote_value = RemoteValueDpt2ByteUnsigned(xknx)
self.assertEqual(remote_value.from_knx(DPTArray((0x0A, 0x0B))), 2571)

def test_to_knx_error(self):
"""Test to_knx function with wrong parametern."""
xknx = XKNX(loop=self.loop)
remote_value = RemoteValueDpt2ByteUnsigned(xknx)
with self.assertRaises(ConversionError):
remote_value.to_knx(65536)
with self.assertRaises(ConversionError):
remote_value.to_knx("256")

def test_set(self):
"""Test setting value."""
xknx = XKNX(loop=self.loop)
remote_value = RemoteValueDpt2ByteUnsigned(
xknx,
group_address=GroupAddress("1/2/3"))
self.loop.run_until_complete(asyncio.Task(remote_value.set(2571)))
self.assertEqual(xknx.telegrams.qsize(), 1)
telegram = xknx.telegrams.get_nowait()
self.assertEqual(
telegram,
Telegram(
GroupAddress('1/2/3'),
payload=DPTArray((0x0A, 0x0B))))
self.loop.run_until_complete(asyncio.Task(remote_value.set(5500)))
self.assertEqual(xknx.telegrams.qsize(), 1)
telegram = xknx.telegrams.get_nowait()
self.assertEqual(
telegram,
Telegram(
GroupAddress('1/2/3'),
payload=DPTArray((0x15, 0x7C, ))))

def test_process(self):
"""Test process telegram."""
xknx = XKNX(loop=self.loop)
remote_value = RemoteValueDpt2ByteUnsigned(
xknx,
group_address=GroupAddress("1/2/3"))
telegram = Telegram(
group_address=GroupAddress("1/2/3"),
payload=DPTArray((0x0A, 0x0B)))
self.loop.run_until_complete(asyncio.Task(remote_value.process(telegram)))
self.assertEqual(remote_value.value, 2571)

def test_to_process_error(self):
"""Test process errornous telegram."""
xknx = XKNX(loop=self.loop)
remote_value = RemoteValueDpt2ByteUnsigned(
xknx,
group_address=GroupAddress("1/2/3"))
with self.assertRaises(CouldNotParseTelegram):
telegram = Telegram(
group_address=GroupAddress("1/2/3"),
payload=DPTBinary(1))
self.loop.run_until_complete(asyncio.Task(remote_value.process(telegram)))
with self.assertRaises(CouldNotParseTelegram):
telegram = Telegram(
group_address=GroupAddress("1/2/3"),
payload=DPTArray((0x64, )))
self.loop.run_until_complete(asyncio.Task(remote_value.process(telegram)))
with self.assertRaises(CouldNotParseTelegram):
telegram = Telegram(
group_address=GroupAddress("1/2/3"),
payload=DPTArray((0x64, 0x53, 0x42, )))
self.loop.run_until_complete(asyncio.Task(remote_value.process(telegram)))
1 change: 1 addition & 0 deletions tox.ini
Expand Up @@ -8,6 +8,7 @@ setenv =
PYTHONPATH = {toxinidir}
commands =
py.test --cov --cov-report= {posargs}
whitelist_externals = make
deps = -rrequirements/testing.txt

[testenv:flake8]
Expand Down

0 comments on commit f96ea72

Please sign in to comment.