Ejemplo n.º 1
0
 def test_set_color(self):
     """Test setting the color of a Light."""
     xknx = XKNX(loop=self.loop)
     light = Light(xknx,
                   name="TestLight",
                   group_address_switch='1/2/3',
                   group_address_color='1/2/5')
     self.loop.run_until_complete(
         asyncio.Task(light.set_color((23, 24, 25))))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(GroupAddress('1/2/5'), payload=DPTArray((23, 24, 25))))
     self.assertEqual(light.current_color, (23, 24, 25))
Ejemplo n.º 2
0
 def test_execute_action(self):
     """Test if execute method of Action calls correct do method of device."""
     xknx = XKNX(loop=self.loop)
     light = Light(
         xknx,
         'Light1',
         group_address_switch='1/6/4')
     xknx.devices.add(light)
     action = Action(xknx, target='Light1', method='on')
     with patch('xknx.devices.Light.do') as mock_do:
         fut = asyncio.Future()
         fut.set_result(None)
         mock_do.return_value = fut
         self.loop.run_until_complete(asyncio.Task(action.execute()))
         mock_do.assert_called_with('on')
Ejemplo n.º 3
0
    def test_get_item(self):
        """Test get item by name or by index."""
        xknx = XKNX(loop=self.loop)
        devices = Devices()

        light1 = Light(xknx,
                       'Living-Room.Light_1',
                       group_address_switch='1/6/7')
        devices.add(light1)

        switch1 = Switch(xknx, "TestOutlet_1", group_address='1/2/3')
        devices.add(switch1)

        light2 = Light(xknx,
                       'Living-Room.Light_2',
                       group_address_switch='1/6/8')
        devices.add(light2)

        switch2 = Switch(xknx, "TestOutlet_2", group_address='1/2/4')
        devices.add(switch2)

        self.assertEqual(devices["Living-Room.Light_1"], light1)
        self.assertEqual(devices["TestOutlet_1"], switch1)
        self.assertEqual(devices["Living-Room.Light_2"], light2)
        self.assertEqual(devices["TestOutlet_2"], switch2)
        with self.assertRaises(KeyError):
            # pylint: disable=pointless-statement
            devices["TestOutlet_X"]

        self.assertEqual(devices[0], light1)
        self.assertEqual(devices[1], switch1)
        self.assertEqual(devices[2], light2)
        self.assertEqual(devices[3], switch2)
        with self.assertRaises(IndexError):
            # pylint: disable=pointless-statement
            devices[4]
Ejemplo n.º 4
0
    def test_device_by_group_address(self):
        """Test get devices by group address."""
        xknx = XKNX(loop=self.loop)
        devices = Devices()

        light1 = Light(xknx,
                       'Living-Room.Light_1',
                       group_address_switch='1/6/7')
        devices.add(light1)

        sensor1 = BinarySensor(xknx,
                               'DiningRoom.Motion.Sensor',
                               group_address='3/0/1',
                               significant_bit=2)
        devices.add(sensor1)

        sensor2 = BinarySensor(xknx,
                               'DiningRoom.Motion.Sensor',
                               group_address='3/0/1',
                               significant_bit=3)
        devices.add(sensor2)

        light2 = Light(xknx,
                       'Living-Room.Light_2',
                       group_address_switch='1/6/8')
        devices.add(light2)

        self.assertEqual(
            tuple(devices.devices_by_group_address(GroupAddress('1/6/7'))),
            (light1, ))
        self.assertEqual(
            tuple(devices.devices_by_group_address(GroupAddress('1/6/8'))),
            (light2, ))
        self.assertEqual(
            tuple(devices.devices_by_group_address(GroupAddress('3/0/1'))),
            (sensor1, sensor2))
Ejemplo n.º 5
0
 def test_config_light_color(self):
     """Test reading Light with with dimming and color address."""
     self.assertEqual(
         TestConfig.xknx.devices["Diningroom.Light_1"],
         Light(
             TestConfig.xknx,
             "Diningroom.Light_1",
             group_address_switch="1/6/4",
             group_address_brightness="1/6/6",
             group_address_color="1/6/7",
             group_address_color_state="1/6/8",
             min_kelvin=2700,
             max_kelvin=6000,
         ),
     )
Ejemplo n.º 6
0
 def test_config_light_state(self):
     """Test reading Light with dimming address from config file."""
     self.assertEqual(
         TestConfig.xknx.devices["Office.Light_1"],
         Light(
             TestConfig.xknx,
             "Office.Light_1",
             group_address_switch="1/7/4",
             group_address_switch_state="1/7/5",
             group_address_brightness="1/7/6",
             group_address_brightness_state="1/7/7",
             min_kelvin=2700,
             max_kelvin=6000,
         ),
     )
Ejemplo n.º 7
0
 def test_light_color(self):
     """Test string representation of dimmable light object."""
     xknx = XKNX(loop=self.loop)
     light = Light(
         xknx,
         name='Licht',
         group_address_switch='1/2/3',
         group_address_switch_state='1/2/4',
         group_address_color='1/2/5',
         group_address_color_state='1/2/6')
     self.assertEqual(
         str(light),
         '<Light name="Licht" '
         'switch="GroupAddress("1/2/3")/GroupAddress("1/2/4")/None/None" '
         'color="GroupAddress("1/2/5")/GroupAddress("1/2/6")/None/None" />')
Ejemplo n.º 8
0
 def test_config_tunable_white(self):
     """Test reading Light with with dimming and tunable white address."""
     self.assertEqual(
         TestConfig.xknx.devices['Living-Room.Light_TW'],
         Light(TestConfig.xknx,
               'Living-Room.Light_TW',
               group_address_switch='1/6/21',
               group_address_switch_state='1/6/20',
               group_address_brightness='1/6/22',
               group_address_brightness_state='1/6/23',
               group_address_tunable_white='1/6/24',
               group_address_tunable_white_state='1/6/25',
               min_kelvin=2700,
               max_kelvin=6000,
               device_updated_cb=TestConfig.xknx.devices.device_updated))
Ejemplo n.º 9
0
 def test_config_color_temperature(self):
     """Test reading Light with with dimming and color temperature address."""
     self.assertEqual(
         TestConfig.xknx.devices['Living-Room.Light_CT'],
         Light(TestConfig.xknx,
               'Living-Room.Light_CT',
               group_address_switch='1/6/11',
               group_address_switch_state='1/6/10',
               group_address_brightness='1/6/12',
               group_address_brightness_state='1/6/13',
               group_address_color_temperature='1/6/14',
               group_address_color_temperature_state='1/6/15',
               min_kelvin=2700,
               max_kelvin=6000,
               device_updated_cb=TestConfig.xknx.devices.device_updated))
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    def test_process_switch(self):
        """Test process / reading telegrams from telegram queue. Test if switch position is processed correctly."""
        xknx = XKNX(loop=self.loop)
        light = Light(xknx,
                      name="TestLight",
                      group_address_switch='1/2/3',
                      group_address_brightness='1/2/5')
        self.assertEqual(light.state, False)

        telegram = Telegram(Address('1/2/3'), payload=DPTBinary(1))
        self.loop.run_until_complete(asyncio.Task(light.process(telegram)))
        self.assertEqual(light.state, True)

        telegram = Telegram(Address('1/2/3'), payload=DPTBinary(0))
        self.loop.run_until_complete(asyncio.Task(light.process(telegram)))
        self.assertEqual(light.state, False)
Ejemplo n.º 12
0
 def test_light_color(self):
     """Test string representation of dimmable light object."""
     xknx = XKNX()
     light = Light(
         xknx,
         name="Licht",
         group_address_switch="1/2/3",
         group_address_switch_state="1/2/4",
         group_address_color="1/2/5",
         group_address_color_state="1/2/6",
     )
     assert (
         str(light) == '<Light name="Licht" '
         "switch=<1/2/3, 1/2/4, [], None /> "
         "color=<1/2/5, 1/2/6, [], None /> />"
     )
Ejemplo n.º 13
0
    def test_set_color_rgbw_not_possible(self):
        """Test setting RGBW value of a non light without color."""
        # pylint: disable=invalid-name
        xknx = XKNX()
        light = Light(
            xknx,
            name="TestLight",
            group_address_switch="1/2/3",
            group_address_color="1/2/4",
        )
        with patch("logging.Logger.warning") as mock_warn:
            self.loop.run_until_complete(light.set_color((23, 24, 25), 26))

            self.assertEqual(xknx.telegrams.qsize(), 0)
            mock_warn.assert_called_with("RGBW not supported for device %s",
                                         "TestLight")
Ejemplo n.º 14
0
 def test_light_dimmable(self):
     """Test string representation of dimmable light object."""
     xknx = XKNX()
     light = Light(
         xknx,
         name="Licht",
         group_address_switch="1/2/3",
         group_address_switch_state="1/2/4",
         group_address_brightness="1/2/5",
         group_address_brightness_state="1/2/6",
     )
     self.assertEqual(
         str(light),
         '<Light name="Licht" switch="GroupAddress("1/2/3")/GroupAddress("1/2/4")/None/None" '
         'brightness="GroupAddress("1/2/5")/GroupAddress("1/2/6")/None/None" />',
     )
Ejemplo n.º 15
0
async def main():
    """Connect to KNX/IP bus and read the state of a Light device."""
    xknx = XKNX()
    await xknx.start()

    light = Light(xknx,
                  name='TestLight2',
                  group_address_switch='1/0/12',
                  group_address_brightness='1/0/14')
    await light.set_brightness(128)

    # Will do a group read of both addresses
    await light.sync()

    print(light)

    await xknx.stop()
Ejemplo n.º 16
0
async def main():
    """Connect to KNX/IP bus, slowly dimm on light, set it off again afterwards."""
    xknx = XKNX()
    await xknx.start()

    light = Light(xknx,
                  name='TestLight2',
                  group_address_switch='1/0/12',
                  group_address_brightness='1/0/14')

    for i in [0, 31, 63, 95, 127, 159, 191, 223, 255]:
        await light.set_brightness(i)
        await asyncio.sleep(1)

    await light.set_off()

    await xknx.stop()
Ejemplo n.º 17
0
 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,
         ))))
Ejemplo n.º 18
0
    def test_state_updater(self):
        """Test State updater."""
        xknx = XKNX(loop=self.loop)
        light = Light(xknx, name='TestLight', group_address_switch='1/0/9')
        xknx.devices.add(light)

        state_updater = StateUpdater(xknx, timeout=0, start_timeout=0)
        state_updater.run_forever = False

        with patch('xknx.devices.Device.sync') as mock_sync:
            fut = asyncio.Future()
            fut.set_result(None)
            mock_sync.return_value = fut

            self.loop.run_until_complete(asyncio.Task(state_updater.start()))
            self.loop.run_until_complete(state_updater.run_task)
            mock_sync.assert_called_with()
Ejemplo n.º 19
0
 def test_modification_of_device(self):
     """Test if devices object does store references and not copies of objects."""
     xknx = XKNX(loop=self.loop)
     devices = Devices()
     light1 = Light(xknx,
                    'Living-Room.Light_1',
                    group_address_switch='1/6/7')
     devices.add(light1)
     for device in devices:
         self.loop.run_until_complete(asyncio.Task(device.set_on()))
     self.assertTrue(light1.state)
     device2 = devices["Living-Room.Light_1"]
     self.loop.run_until_complete(asyncio.Task(device2.set_off()))
     self.assertFalse(light1.state)
     for device in devices.devices_by_group_address(GroupAddress('1/6/7')):
         self.loop.run_until_complete(asyncio.Task(device.set_on()))
     self.assertTrue(light1.state)
Ejemplo n.º 20
0
 def test_set_color(self):
     """Test setting the color of a Light."""
     xknx = XKNX()
     light = Light(
         xknx,
         name="TestLight",
         group_address_switch="1/2/3",
         group_address_color="1/2/5",
     )
     self.loop.run_until_complete(light.set_color((23, 24, 25)))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(GroupAddress("1/2/5"), payload=DPTArray((23, 24, 25))))
     self.loop.run_until_complete(xknx.devices.process(telegram))
     self.assertEqual(light.current_color, ((23, 24, 25), None))
Ejemplo n.º 21
0
 def test_config_tunable_white(self):
     """Test reading Light with with dimming and tunable white address."""
     self.assertEqual(
         TestConfig.xknx.devices["Living-Room.Light_TW"],
         Light(
             TestConfig.xknx,
             "Living-Room.Light_TW",
             group_address_switch="1/6/21",
             group_address_switch_state="1/6/20",
             group_address_brightness="1/6/22",
             group_address_brightness_state="1/6/23",
             group_address_tunable_white="1/6/24",
             group_address_tunable_white_state="1/6/25",
             min_kelvin=2700,
             max_kelvin=6000,
         ),
     )
Ejemplo n.º 22
0
 def test_config_color_temperature(self):
     """Test reading Light with with dimming and color temperature address."""
     self.assertEqual(
         TestConfig.xknx.devices["Living-Room.Light_CT"],
         Light(
             TestConfig.xknx,
             "Living-Room.Light_CT",
             group_address_switch="1/6/11",
             group_address_switch_state="1/6/10",
             group_address_brightness="1/6/12",
             group_address_brightness_state="1/6/13",
             group_address_color_temperature="1/6/14",
             group_address_color_temperature_state="1/6/15",
             min_kelvin=2700,
             max_kelvin=6000,
         ),
     )
Ejemplo n.º 23
0
    def test_sync_state_address(self):
        """Test sync function / sending group reads to KNX bus. Testing with a Light with dimm functionality."""
        xknx = XKNX(loop=self.loop)
        light = Light(xknx,
                      name="TestLight",
                      group_address_switch='1/2/3',
                      group_address_switch_state='1/2/4',
                      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_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',
                      group_address_rgbw='1/2/13',
                      group_address_rgbw_state='1/2/14')
        self.loop.run_until_complete(asyncio.Task(light.sync(False)))

        self.assertEqual(xknx.telegrams.qsize(), 6)

        telegram1 = xknx.telegrams.get_nowait()
        self.assertEqual(
            telegram1, Telegram(GroupAddress('1/2/4'),
                                TelegramType.GROUP_READ))
        telegram2 = xknx.telegrams.get_nowait()
        self.assertEqual(
            telegram2, Telegram(GroupAddress('1/2/8'),
                                TelegramType.GROUP_READ))
        telegram6 = xknx.telegrams.get_nowait()
        self.assertEqual(
            telegram6, Telegram(GroupAddress('1/2/14'),
                                TelegramType.GROUP_READ))
        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))
Ejemplo n.º 24
0
 def test_has_group_address(self):
     """Test has_group_address."""
     xknx = XKNX(config='xknx.yaml', loop=self.loop)
     light = Light(xknx,
                   'Office.Light_1',
                   group_address_switch='1/7/1',
                   group_address_switch_state='1/7/2',
                   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')
     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')))
Ejemplo n.º 25
0
    def test_sync(self):
        """Test sync function / sending group reads to KNX bus. Testing with a Light without dimm functionality."""
        xknx = XKNX(loop=self.loop)
        light = Light(xknx,
                      name="TestLight",
                      group_address_switch='1/2/3',
                      group_address_brightness='1/2/5')
        self.loop.run_until_complete(asyncio.Task(light.sync(False)))

        self.assertEqual(xknx.telegrams.qsize(), 2)

        telegram1 = xknx.telegrams.get_nowait()
        self.assertEqual(telegram1,
                         Telegram(Address('1/2/3'), TelegramType.GROUP_READ))

        telegram2 = xknx.telegrams.get_nowait()
        self.assertEqual(telegram2,
                         Telegram(Address('1/2/5'), TelegramType.GROUP_READ))
Ejemplo n.º 26
0
 def test_do(self):
     """Test 'do' functionality."""
     xknx = XKNX(loop=self.loop)
     light = Light(xknx,
                   name="TestLight",
                   group_address_switch='1/2/3',
                   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)
Ejemplo n.º 27
0
async def main():
    """Connect to KNX/IP bus and read the state of a Light device."""
    xknx = XKNX()
    await xknx.start()

    light = Light(
        xknx,
        name="TestLight2",
        group_address_switch="1/0/12",
        group_address_brightness="1/0/14",
    )
    await light.set_brightness(128)

    # Will do a GroupValueRead for both addresses and block until a result is received
    await light.sync(wait_for_result=True)

    print(light)

    await xknx.stop()
Ejemplo n.º 28
0
async def main() -> None:
    """Connect to KNX/IP bus and read the state of a Light device."""
    xknx = XKNX(
        connection_config=ConnectionConfig(
            gateway_ip="10.1.0.41",
            connection_type=ConnectionType.TUNNELING_TCP,
        ),
    )
    await xknx.start()
    light = Light(
        xknx,
        name="TestLight",
        group_address_switch="1/1/45",
        group_address_switch_state="1/0/45",
        device_updated_cb=light_callback,
    )
    # turn on light and listen for 10 seconds for changes
    await light.set_on()
    await asyncio.sleep(10)
    await xknx.stop()
Ejemplo n.º 29
0
 def test_modification_of_device(self):
     """Test if devices object does store references and not copies of objects."""
     xknx = XKNX()
     light1 = Light(xknx, "Living-Room.Light_1", group_address_switch="1/6/7")
     for device in xknx.devices:
         self.loop.run_until_complete(device.set_on())
         self.loop.run_until_complete(
             xknx.devices.process(xknx.telegrams.get_nowait())
         )
     self.assertTrue(light1.state)
     device2 = xknx.devices["Living-Room.Light_1"]
     self.loop.run_until_complete(device2.set_off())
     self.loop.run_until_complete(xknx.devices.process(xknx.telegrams.get_nowait()))
     self.assertFalse(light1.state)
     for device in xknx.devices.devices_by_group_address(GroupAddress("1/6/7")):
         self.loop.run_until_complete(device.set_on())
         self.loop.run_until_complete(
             xknx.devices.process(xknx.telegrams.get_nowait())
         )
     self.assertTrue(light1.state)
Ejemplo n.º 30
0
 def test_set_color_rgbw(self):
     """Test setting RGBW value of a Light."""
     xknx = XKNX(loop=self.loop)
     light = Light(
         xknx,
         name="TestLight",
         group_address_switch="1/2/3",
         group_address_color="1/2/4",
         group_address_rgbw="1/2/5",
     )
     self.loop.run_until_complete(light.set_color((23, 24, 25), 26))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(GroupAddress("1/2/5"),
                  payload=DPTArray((23, 24, 25, 26, 0, 15))),
     )
     self.loop.run_until_complete(xknx.devices.process(telegram))
     self.assertEqual(light.current_color, ([23, 24, 25], 26))