Beispiel #1
0
    async def test_process_group_value_response(self):
        """Test precess of GroupValueResponse telegrams."""
        xknx = XKNX()
        switch = BinarySensor(
            xknx,
            "TestInput",
            group_address_state="1/2/3",
            ignore_internal_state=True,
        )
        async_after_update_callback = AsyncMock()

        switch.register_device_updated_cb(async_after_update_callback)

        write_telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        response_telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueResponse(DPTBinary(1), ),
        )
        assert switch.state is None
        # initial GroupValueResponse changes state and runs callback
        await switch.process(response_telegram)
        assert switch.state
        async_after_update_callback.assert_called_once_with(switch)
        # GroupValueWrite with same payload runs callback because of `ignore_internal_state`
        async_after_update_callback.reset_mock()
        await switch.process(write_telegram)
        assert switch.state
        async_after_update_callback.assert_called_once_with(switch)
        # GroupValueResponse should not run callback when state has not changed
        async_after_update_callback.reset_mock()
        await switch.process(response_telegram)
        async_after_update_callback.assert_not_called()
Beispiel #2
0
    def test_len(self):
        """Test len() function."""
        xknx = XKNX(loop=self.loop)
        devices = Devices()
        self.assertEqual(len(devices), 0)

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

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

        sensor2 = BinarySensor(xknx,
                               'DiningRoom.Motion.Sensor',
                               group_address_state='3/0/1',
                               significant_bit=3)
        devices.add(sensor2)
        self.assertEqual(len(devices), 3)

        light2 = Light(xknx,
                       'Living-Room.Light_2',
                       group_address_switch='1/6/8')
        devices.add(light2)
        self.assertEqual(len(devices), 4)
Beispiel #3
0
    def test_device_by_group_address(self):
        """Test get devices by group address."""
        xknx = XKNX()
        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_state="3/0/1")
        devices.add(sensor1)

        sensor2 = BinarySensor(xknx,
                               "DiningRoom.Motion.Sensor",
                               group_address_state="3/0/1")
        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),
        )
Beispiel #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_state='3/0/1')
        devices.add(sensor1)

        sensor2 = BinarySensor(xknx,
                               'DiningRoom.Motion.Sensor',
                               group_address_state='3/0/1')
        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))
Beispiel #5
0
 def test_process_wrong_payload(self):
     """Test process wrong telegram (wrong payload type)."""
     xknx = XKNX(loop=self.loop)
     binary_sensor = BinarySensor(xknx, 'Warning', group_address_state='1/2/3')
     telegram = Telegram(GroupAddress('1/2/3'), payload=DPTArray((0x1, 0x2, 0x3)))
     with self.assertRaises(CouldNotParseTelegram):
         self.loop.run_until_complete(asyncio.Task(binary_sensor.process(telegram)))
Beispiel #6
0
    def test_iter(self):
        """Test __iter__() function."""
        xknx = XKNX()
        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_state="3/0/1")
        devices.add(sensor1)

        sensor2 = BinarySensor(xknx,
                               "DiningRoom.Motion.Sensor",
                               group_address_state="3/0/1")
        devices.add(sensor2)

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

        devices.add(light2)

        self.assertEqual(tuple(devices.__iter__()),
                         (light1, sensor1, sensor2, light2))
Beispiel #7
0
    async def test_process(self):
        """Test process / reading telegrams from telegram queue."""
        xknx = XKNX()
        binaryinput = BinarySensor(xknx, "TestInput", "1/2/3")

        assert binaryinput.state is None

        telegram_on = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        await binaryinput.process(telegram_on)
        assert binaryinput.state is True

        telegram_off = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(0)),
        )
        await binaryinput.process(telegram_off)
        assert binaryinput.state is False

        binaryinput2 = BinarySensor(xknx, "TestInput", "1/2/4")
        assert binaryinput2.state is None

        telegram_off2 = Telegram(
            destination_address=GroupAddress("1/2/4"),
            payload=GroupValueWrite(DPTBinary(0)),
        )
        await binaryinput2.process(telegram_off2)
        assert binaryinput2.last_telegram == telegram_off2
        assert binaryinput2.state is False
Beispiel #8
0
    async def test_process_callback_ignore_internal_state_no_counter(self):
        """Test after_update_callback after state of switch was changed."""
        xknx = XKNX()
        switch = BinarySensor(
            xknx,
            "TestInput",
            group_address_state="1/2/3",
            ignore_internal_state=True,
            context_timeout=0,
        )
        async_after_update_callback = AsyncMock()

        switch.register_device_updated_cb(async_after_update_callback)

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        await switch.process(telegram)
        # no _context_task started because context_timeout is False
        assert switch._context_task is None
        async_after_update_callback.assert_called_once_with(switch)

        async_after_update_callback.reset_mock()
        # send same telegram again
        await switch.process(telegram)
        async_after_update_callback.assert_called_once_with(switch)
Beispiel #9
0
 def test_process_wrong_payload(self):
     """Test process wrong telegram (wrong payload type)."""
     xknx = XKNX(loop=self.loop)
     binary_sensor = BinarySensor(xknx, 'Warning', group_address_state='1/2/3')
     telegram = Telegram(GroupAddress('1/2/3'), payload=DPTArray((0x1, 0x2, 0x3)))
     with self.assertRaises(CouldNotParseTelegram):
         self.loop.run_until_complete(asyncio.Task(binary_sensor.process(telegram)))
Beispiel #10
0
    def test_process_reset_after(self):
        """Test process / reading telegrams from telegram queue."""
        xknx = XKNX()
        reset_after_sec = 0.001
        async_after_update_callback = AsyncMock()
        binaryinput = BinarySensor(
            xknx,
            "TestInput",
            "1/2/3",
            reset_after=reset_after_sec,
            device_updated_cb=async_after_update_callback,
        )
        telegram_on = Telegram(group_address=GroupAddress("1/2/3"),
                               payload=DPTBinary(1))

        self.loop.run_until_complete(binaryinput.process(telegram_on))
        self.assertTrue(binaryinput.state)
        self.loop.run_until_complete(asyncio.sleep(reset_after_sec * 1.1))
        self.assertFalse(binaryinput.state)
        # once for 'on' and once for 'off'
        self.assertEqual(async_after_update_callback.call_count, 2)

        async_after_update_callback.reset_mock()
        # multiple telegrams during reset_after time period shall reset timer
        self.loop.run_until_complete(binaryinput.process(telegram_on))
        async_after_update_callback.assert_called_once()
        self.loop.run_until_complete(binaryinput.process(telegram_on))
        self.loop.run_until_complete(binaryinput.process(telegram_on))
        # second and third telegram resets timer but doesn't run callback
        async_after_update_callback.assert_called_once()
        self.assertTrue(binaryinput.state)
        self.loop.run_until_complete(asyncio.sleep(reset_after_sec * 1.1))
        self.assertFalse(binaryinput.state)
        # once for 'on' and once for 'off'
        self.assertEqual(async_after_update_callback.call_count, 2)
Beispiel #11
0
 def test_process_reset_after(self):
     """Test process / reading telegrams from telegram queue."""
     xknx = XKNX(loop=self.loop)
     binaryinput = BinarySensor(xknx, 'TestInput', '1/2/3', reset_after=0.01)
     telegram_on = Telegram(payload=DPTBinary(1))
     self.loop.run_until_complete(asyncio.Task(binaryinput.process(telegram_on)))
     self.assertEqual(binaryinput.state, BinarySensorState.OFF)
Beispiel #12
0
 def test_process_reset_after(self):
     """Test process / reading telegrams from telegram queue."""
     xknx = XKNX(loop=self.loop)
     binaryinput = BinarySensor(xknx, 'TestInput', '1/2/3', reset_after=0.01)
     telegram_on = Telegram(payload=DPTBinary(1))
     self.loop.run_until_complete(asyncio.Task(binaryinput.process(telegram_on)))
     self.assertEqual(binaryinput.state, BinarySensorState.OFF)
Beispiel #13
0
    def test_process_significant_bit(self):
        """Test process / reading telegrams from telegram queue with specific significant bit set."""
        xknx = XKNX(loop=self.loop)
        binaryinput = BinarySensor(xknx,
                                   'TestInput',
                                   '1/2/3',
                                   significant_bit=3)

        self.assertEqual(binaryinput.state, BinarySensorState.OFF)

        # Wrong significant bit: 0000 1011 = 11
        telegram_on = Telegram()
        telegram_on.payload = DPTBinary(11)
        self.loop.run_until_complete(
            asyncio.Task(binaryinput.process(telegram_on)))
        self.assertEqual(binaryinput.state, BinarySensorState.OFF)

        # Correct significant bit: 0000 1101 = 13
        telegram_on = Telegram()
        telegram_on.payload = DPTBinary(13)
        self.loop.run_until_complete(
            asyncio.Task(binaryinput.process(telegram_on)))
        self.assertEqual(binaryinput.state, BinarySensorState.ON)

        # Resetting, significant bit: 0000 0011 = 3
        telegram_off = Telegram()
        telegram_off.payload = DPTBinary(3)
        self.loop.run_until_complete(
            asyncio.Task(binaryinput.process(telegram_off)))
        self.assertEqual(binaryinput.state, BinarySensorState.OFF)
Beispiel #14
0
    def test_iter(self):
        """Test __iter__() function."""
        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_state='3/0/1',
                               significant_bit=2)
        devices.add(sensor1)

        sensor2 = BinarySensor(xknx,
                               'DiningRoom.Motion.Sensor',
                               group_address_state='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.__iter__()),
            (light1, sensor1, sensor2, light2))
Beispiel #15
0
 def test_is_off(self):
     """Test is_on() and is_off() of a BinarySensor with state 'off'."""
     xknx = XKNX(loop=self.loop)
     binaryinput = BinarySensor(xknx, 'TestInput', '1/2/3')
     # pylint: disable=protected-access
     self.loop.run_until_complete(asyncio.Task(binaryinput._set_internal_state(BinarySensorState.OFF)))
     self.assertFalse(binaryinput.is_on())
     self.assertTrue(binaryinput.is_off())
Beispiel #16
0
    async def test_is_off(self):
        """Test is_on() and is_off() of a BinarySensor with state 'off'."""
        xknx = XKNX()
        binaryinput = BinarySensor(xknx, "TestInput", "1/2/3")
        await binaryinput._set_internal_state(False)

        assert not binaryinput.is_on()
        assert binaryinput.is_off()
Beispiel #17
0
    def test_state_addresses(self):
        """Test binary sensor returns state address as list."""
        xknx = XKNX(loop=self.loop)
        binary_sensor = BinarySensor(xknx, 'TestInput', group_address_state='1/2/4')
        self.assertEqual(binary_sensor.state_addresses(), [GroupAddress('1/2/4')])

        binary_sensor2 = BinarySensor(xknx, 'TestInput')
        self.assertEqual(binary_sensor2.state_addresses(), [])
Beispiel #18
0
 def test_is_off(self):
     """Test is_on() and is_off() of a BinarySensor with state 'off'."""
     xknx = XKNX(loop=self.loop)
     binaryinput = BinarySensor(xknx, 'TestInput', '1/2/3')
     # pylint: disable=protected-access
     self.loop.run_until_complete(asyncio.Task(binaryinput._set_internal_state(BinarySensorState.OFF)))
     self.assertFalse(binaryinput.is_on())
     self.assertTrue(binaryinput.is_off())
Beispiel #19
0
    def test_is_off(self):
        """Test is_on() and is_off() of a BinarySensor with state 'off'."""
        xknx = XKNX()
        binaryinput = BinarySensor(xknx, "TestInput", "1/2/3")
        # pylint: disable=protected-access
        self.loop.run_until_complete(binaryinput._set_internal_state(False))

        self.assertFalse(binaryinput.is_on())
        self.assertTrue(binaryinput.is_off())
Beispiel #20
0
 def test_state_addresses(self):
     """Test expose sensor returns empty list as state addresses."""
     xknx = XKNX(loop=self.loop)
     binary_sensor = BinarySensor(xknx,
                                  'TestInput',
                                  group_address='1/2/3',
                                  group_address_state='1/2/4')
     self.assertEqual(binary_sensor.state_addresses(),
                      [GroupAddress('1/2/4')])
Beispiel #21
0
    def test_counter(self):
        """Test counter functionality."""
        xknx = XKNX(loop=self.loop)
        switch = BinarySensor(xknx, "TestInput", group_address_state="1/2/3")
        with patch("time.time") as mock_time:
            mock_time.return_value = 1517000000.0
            self.assertEqual(switch.bump_and_get_counter(True), 1)

            mock_time.return_value = 1517000000.1
            self.assertEqual(switch.bump_and_get_counter(True), 2)

            mock_time.return_value = 1517000000.2
            self.assertEqual(switch.bump_and_get_counter(False), 1)

            mock_time.return_value = 1517000000.3
            self.assertEqual(switch.bump_and_get_counter(True), 3)

            mock_time.return_value = 1517000000.4
            self.assertEqual(switch.bump_and_get_counter(False), 2)

            mock_time.return_value = 1517000002.0  # TIME OUT ...
            self.assertEqual(switch.bump_and_get_counter(True), 1)

            mock_time.return_value = 1517000004.1  # TIME OUT ...
            self.assertEqual(switch.bump_and_get_counter(False), 1)
Beispiel #22
0
    def test_counter(self):
        """Test counter functionality."""
        xknx = XKNX()
        switch = BinarySensor(
            xknx, "TestInput", group_address_state="1/2/3", context_timeout=1
        )
        with patch("time.time") as mock_time:
            mock_time.return_value = 1517000000.0
            assert switch.bump_and_get_counter(True) == 1

            mock_time.return_value = 1517000000.1
            assert switch.bump_and_get_counter(True) == 2

            mock_time.return_value = 1517000000.2
            assert switch.bump_and_get_counter(False) == 1

            mock_time.return_value = 1517000000.3
            assert switch.bump_and_get_counter(True) == 3

            mock_time.return_value = 1517000000.4
            assert switch.bump_and_get_counter(False) == 2

            mock_time.return_value = 1517000002.0  # TIME OUT ...
            assert switch.bump_and_get_counter(True) == 1

            mock_time.return_value = 1517000004.1  # TIME OUT ...
            assert switch.bump_and_get_counter(False) == 1
Beispiel #23
0
    def test_counter(self):
        """Test counter functionality."""
        xknx = XKNX(loop=self.loop)
        switch = BinarySensor(xknx, 'TestInput', group_address='1/2/3')
        with patch('time.time') as mock_time:
            mock_time.return_value = 1517000000.0
            self.assertEqual(switch.bump_and_get_counter(BinarySensorState.ON),
                             1)

            mock_time.return_value = 1517000000.1
            self.assertEqual(switch.bump_and_get_counter(BinarySensorState.ON),
                             2)

            mock_time.return_value = 1517000000.2
            self.assertEqual(
                switch.bump_and_get_counter(BinarySensorState.OFF), 1)

            mock_time.return_value = 1517000000.3
            self.assertEqual(switch.bump_and_get_counter(BinarySensorState.ON),
                             3)

            mock_time.return_value = 1517000000.4
            self.assertEqual(
                switch.bump_and_get_counter(BinarySensorState.OFF), 2)

            mock_time.return_value = 1517000002.0  # TIME OUT ...
            self.assertEqual(switch.bump_and_get_counter(BinarySensorState.ON),
                             1)

            mock_time.return_value = 1517000004.1  # TIME OUT ...
            self.assertEqual(
                switch.bump_and_get_counter(BinarySensorState.OFF), 1)
Beispiel #24
0
    def test_process_reset_after(self):
        """Test process / reading telegrams from telegram queue."""
        xknx = XKNX(loop=self.loop)
        reset_after_ms = 0.01
        binaryinput = BinarySensor(xknx, 'TestInput', '1/2/3', reset_after=reset_after_ms)
        telegram_on = Telegram(group_address=GroupAddress('1/2/3'))
        telegram_on.payload = DPTBinary(1)

        self.loop.run_until_complete(asyncio.Task(binaryinput.process(telegram_on)))
        self.loop.run_until_complete(asyncio.sleep(reset_after_ms*2/1000))
        self.assertEqual(binaryinput.state, False)
Beispiel #25
0
 def test_process_wrong_payload(self):
     """Test process wrong telegram (wrong payload type)."""
     xknx = XKNX()
     binary_sensor = BinarySensor(xknx,
                                  "Warning",
                                  group_address_state="1/2/3")
     telegram = Telegram(
         destination_address=GroupAddress("1/2/3"),
         payload=GroupValueWrite(DPTArray((0x1, 0x2, 0x3))),
     )
     with self.assertRaises(CouldNotParseTelegram):
         self.loop.run_until_complete(binary_sensor.process(telegram))
Beispiel #26
0
    def test_process_action_ignore_internal_state(self):
        """Test process / reading telegrams from telegram queue. Test if action is executed."""
        xknx = XKNX(loop=self.loop)
        switch = Switch(xknx, 'TestOutlet', group_address='1/2/3')
        xknx.devices.add(switch)

        binary_sensor = BinarySensor(xknx, 'TestInput', group_address_state='1/2/5', ignore_internal_state=True)
        action_on = Action(
            xknx,
            hook='on',
            target='TestOutlet',
            method='on')
        binary_sensor.actions.append(action_on)
        xknx.devices.add(binary_sensor)

        self.assertEqual(
            xknx.devices['TestInput'].state,
            BinarySensorState.OFF)
        self.assertEqual(
            xknx.devices['TestOutlet'].state,
            False)

        telegram_on = Telegram()
        telegram_on.payload = DPTBinary(1)
        self.loop.run_until_complete(asyncio.Task(binary_sensor.process(telegram_on)))

        self.assertEqual(
            xknx.devices['TestInput'].state,
            BinarySensorState.ON)
        self.assertEqual(
            xknx.devices['TestOutlet'].state,
            True)

        self.loop.run_until_complete(asyncio.Task(switch.set_off()))
        self.assertEqual(
            xknx.devices['TestOutlet'].state,
            False)
        self.assertEqual(
            xknx.devices['TestInput'].state,
            BinarySensorState.ON)

        self.loop.run_until_complete(asyncio.sleep(1))
        self.loop.run_until_complete(asyncio.Task(binary_sensor.process(telegram_on)))

        self.assertEqual(
            xknx.devices['TestInput'].state,
            BinarySensorState.ON)
        self.assertEqual(
            xknx.devices['TestOutlet'].state,
            True)
Beispiel #27
0
    def test_counter(self):
        """Test counter functionality."""
        xknx = XKNX(loop=self.loop)
        switch = BinarySensor(xknx, 'TestInput', group_address_state='1/2/3')
        with patch('time.time') as mock_time:
            mock_time.return_value = 1517000000.0
            self.assertEqual(switch.bump_and_get_counter(BinarySensorState.ON), 1)

            mock_time.return_value = 1517000000.1
            self.assertEqual(switch.bump_and_get_counter(BinarySensorState.ON), 2)

            mock_time.return_value = 1517000000.2
            self.assertEqual(switch.bump_and_get_counter(BinarySensorState.OFF), 1)

            mock_time.return_value = 1517000000.3
            self.assertEqual(switch.bump_and_get_counter(BinarySensorState.ON), 3)

            mock_time.return_value = 1517000000.4
            self.assertEqual(switch.bump_and_get_counter(BinarySensorState.OFF), 2)

            mock_time.return_value = 1517000002.0  # TIME OUT ...
            self.assertEqual(switch.bump_and_get_counter(BinarySensorState.ON), 1)

            mock_time.return_value = 1517000004.1  # TIME OUT ...
            self.assertEqual(switch.bump_and_get_counter(BinarySensorState.OFF), 1)
Beispiel #28
0
    def test_process_reset_after(self):
        """Test process / reading telegrams from telegram queue."""
        xknx = XKNX()
        reset_after_sec = 0.001
        binaryinput = BinarySensor(xknx,
                                   "TestInput",
                                   "1/2/3",
                                   reset_after=reset_after_sec)
        telegram_on = Telegram(group_address=GroupAddress("1/2/3"),
                               payload=DPTBinary(1))

        self.loop.run_until_complete(binaryinput.process(telegram_on))
        self.loop.run_until_complete(asyncio.sleep(reset_after_sec * 2))
        self.assertEqual(binaryinput.state, False)
Beispiel #29
0
    def test_process_action(self):
        """Test process / reading telegrams from telegram queue. Test if action is executed."""
        xknx = XKNX(loop=self.loop)
        switch = Switch(xknx, 'TestOutlet', group_address='1/2/3')
        xknx.devices.add(switch)

        binary_sensor = BinarySensor(xknx, 'TestInput', group_address_state='1/2/3')
        action_on = Action(
            xknx,
            hook='on',
            target='TestOutlet',
            method='on')
        binary_sensor.actions.append(action_on)
        action_off = Action(
            xknx,
            hook='off',
            target='TestOutlet',
            method='off')
        binary_sensor.actions.append(action_off)
        xknx.devices.add(binary_sensor)

        self.assertEqual(
            xknx.devices['TestInput'].state,
            False)
        self.assertEqual(
            xknx.devices['TestOutlet'].state,
            False)

        telegram_on = Telegram(group_address=GroupAddress('1/2/3'))
        telegram_on.payload = DPTBinary(1)
        self.loop.run_until_complete(asyncio.Task(binary_sensor.process(telegram_on)))

        self.assertEqual(
            xknx.devices['TestInput'].state,
            True)
        self.assertEqual(
            xknx.devices['TestOutlet'].state,
            True)

        telegram_off = Telegram(group_address=GroupAddress('1/2/3'))
        telegram_off.payload = DPTBinary(0)
        self.loop.run_until_complete(asyncio.Task(binary_sensor.process(telegram_off)))

        self.assertEqual(
            xknx.devices['TestInput'].state,
            False)
        self.assertEqual(
            xknx.devices['TestOutlet'].state,
            False)
Beispiel #30
0
    def test_process_action(self):
        """Test process / reading telegrams from telegram queue. Test if action is executed."""
        xknx = XKNX(loop=self.loop)
        switch = Switch(xknx, 'TestOutlet', group_address='1/2/3')
        xknx.devices.add(switch)

        binary_sensor = BinarySensor(xknx, 'TestInput', group_address_state='1/2/3')
        action_on = Action(
            xknx,
            hook='on',
            target='TestOutlet',
            method='on')
        binary_sensor.actions.append(action_on)
        action_off = Action(
            xknx,
            hook='off',
            target='TestOutlet',
            method='off')
        binary_sensor.actions.append(action_off)
        xknx.devices.add(binary_sensor)

        self.assertEqual(
            xknx.devices['TestInput'].state,
            BinarySensorState.OFF)
        self.assertEqual(
            xknx.devices['TestOutlet'].state,
            False)

        telegram_on = Telegram()
        telegram_on.payload = DPTBinary(1)
        self.loop.run_until_complete(asyncio.Task(binary_sensor.process(telegram_on)))

        self.assertEqual(
            xknx.devices['TestInput'].state,
            BinarySensorState.ON)
        self.assertEqual(
            xknx.devices['TestOutlet'].state,
            True)

        telegram_off = Telegram()
        telegram_off.payload = DPTBinary(0)
        self.loop.run_until_complete(asyncio.Task(binary_sensor.process(telegram_off)))

        self.assertEqual(
            xknx.devices['TestInput'].state,
            BinarySensorState.OFF)
        self.assertEqual(
            xknx.devices['TestOutlet'].state,
            False)
Beispiel #31
0
    def test_iter(self):
        """Test __iter__() function."""
        xknx = XKNX()
        devices = Devices()

        light1 = Light(xknx, "Livingroom", group_address_switch="1/6/7")
        sensor1 = BinarySensor(xknx, "Diningroom", group_address_state="3/0/1")
        sensor2 = BinarySensor(xknx, "Diningroom", group_address_state="3/0/1")
        light2 = Light(xknx, "Livingroom", group_address_switch="1/6/8")
        devices.add(light1)
        devices.add(sensor1)
        devices.add(sensor2)
        devices.add(light2)

        assert tuple(devices.__iter__()) == (light1, sensor1, sensor2, light2)
Beispiel #32
0
    def test_len(self):
        """Test len() function."""
        xknx = XKNX()
        self.assertEqual(len(xknx.devices), 0)

        Light(xknx, "Living-Room.Light_1", group_address_switch="1/6/7")
        self.assertEqual(len(xknx.devices), 1)

        BinarySensor(xknx, "DiningRoom.Motion.Sensor", group_address_state="3/0/1")
        self.assertEqual(len(xknx.devices), 2)

        BinarySensor(xknx, "DiningRoom.Motion.Sensor", group_address_state="3/0/1")
        self.assertEqual(len(xknx.devices), 3)

        Light(xknx, "Living-Room.Light_2", group_address_switch="1/6/8")
        self.assertEqual(len(xknx.devices), 4)
Beispiel #33
0
 def test_unique_id(self):
     """Test unique id functionality."""
     xknx = XKNX()
     switch = BinarySensor(
         xknx, "TestInput", group_address_state="1/2/3", context_timeout=1
     )
     assert switch.unique_id == "1/2/3"
Beispiel #34
0
def async_add_entities_config(hass, config, async_add_entities):
    """Set up binary senor for KNX platform configured within platform."""
    name = config[CONF_NAME]

    binary_sensor = BinarySensor(
        hass.data[DATA_KNX].xknx,
        name=name,
        group_address_state=config[CONF_STATE_ADDRESS],
        sync_state=config[CONF_SYNC_STATE],
        device_class=config.get(CONF_DEVICE_CLASS),
        significant_bit=config[CONF_SIGNIFICANT_BIT],
        reset_after=config.get(CONF_RESET_AFTER),
    )
    hass.data[DATA_KNX].xknx.devices.add(binary_sensor)

    entity = KNXBinarySensor(binary_sensor)
    automations = config.get(CONF_AUTOMATION)
    if automations is not None:
        for automation in automations:
            counter = automation[CONF_COUNTER]
            hook = automation[CONF_HOOK]
            action = automation[CONF_ACTION]
            entity.automations.append(
                KNXAutomation(
                    hass=hass,
                    device=binary_sensor,
                    hook=hook,
                    action=action,
                    counter=counter,
                ))
    async_add_entities([entity])
Beispiel #35
0
async def main():
    """Connect to KNX/IP device and read the value of a temperature and a motion sensor."""
    xknx = XKNX()
    await xknx.start()

    sensor1 = BinarySensor(
        xknx,
        "DiningRoom.Motion.Sensor",
        group_address_state="6/0/2",
        device_class="motion",
    )
    await sensor1.sync()
    print(sensor1)

    sensor2 = Sensor(
        xknx,
        "DiningRoom.Temperature.Sensor",
        group_address_state="6/2/1",
        value_type="temperature",
    )

    await sensor2.sync()
    print(sensor2)

    await xknx.stop()
Beispiel #36
0
    def test_process_callback(self):
        """Test after_update_callback after state of switch was changed."""
        # pylint: disable=no-self-use
        xknx = XKNX(loop=self.loop)
        switch = BinarySensor(xknx, 'TestInput', group_address_state='1/2/3')

        after_update_callback = Mock()

        async def async_after_update_callback(device):
            """Async callback."""
            after_update_callback(device)
        switch.register_device_updated_cb(async_after_update_callback)

        telegram = Telegram()
        telegram.payload = DPTBinary(1)
        self.loop.run_until_complete(asyncio.Task(switch.process(telegram)))
        after_update_callback.assert_called_with(switch)
Beispiel #37
0
    def test_process_callback(self):
        """Test after_update_callback after state of switch was changed."""
        # pylint: disable=no-self-use
        xknx = XKNX(loop=self.loop)
        switch = BinarySensor(xknx, 'TestInput', group_address_state='1/2/3')

        after_update_callback = Mock()

        async def async_after_update_callback(device):
            """Async callback."""
            after_update_callback(device)
        switch.register_device_updated_cb(async_after_update_callback)

        telegram = Telegram()
        telegram.payload = DPTBinary(1)
        self.loop.run_until_complete(asyncio.Task(switch.process(telegram)))
        after_update_callback.assert_called_with(switch)
Beispiel #38
0
 def parse_group_binary_sensor(self, entries):
     """Parse a binary_sensor section of xknx.yaml."""
     for entry in entries:
         binary_sensor = BinarySensor.from_config(
             self.xknx,
             entry,
             entries[entry])
         self.xknx.devices.add(binary_sensor)
Beispiel #39
0
    def test_binary_sensor_loop(self):
        """Test binary_sensor and expose_sensor with binary values."""
        test_cases = [
            ('binary', DPTBinary(0), False),
            ('binary', DPTBinary(1), True),
        ]

        for value_type, test_payload, test_value in test_cases:
            with self.subTest(value_type=value_type):
                xknx = XKNX(loop=self.loop)
                sensor = BinarySensor(
                    xknx,
                    'TestSensor_%s' % value_type,
                    group_address_state='1/1/1'
                )
                expose = ExposeSensor(
                    xknx,
                    'TestExpose_%s' % value_type,
                    group_address='2/2/2',
                    value_type=value_type
                )

                incoming_telegram = Telegram(GroupAddress('1/1/1'),
                                             TelegramType.GROUP_WRITE,
                                             direction=TelegramDirection.INCOMING,
                                             payload=test_payload)
                self.loop.run_until_complete(asyncio.Task(sensor.process(incoming_telegram)))
                incoming_value = sensor.is_on()
                self.assertEqual(incoming_value, test_value)

                self.loop.run_until_complete(asyncio.Task(expose.set(test_value)))
                self.assertEqual(xknx.telegrams.qsize(), 1)
                outgoing_telegram = xknx.telegrams.get_nowait()
                self.assertEqual(
                    outgoing_telegram,
                    Telegram(
                        GroupAddress('2/2/2'),
                        TelegramType.GROUP_WRITE,
                        direction=TelegramDirection.OUTGOING,
                        payload=test_payload))
Beispiel #40
0
    def test_process_significant_bit(self):
        """Test process / reading telegrams from telegram queue with specific significant bit set."""
        xknx = XKNX(loop=self.loop)
        binaryinput = BinarySensor(xknx, 'TestInput', '1/2/3', significant_bit=3)

        self.assertEqual(binaryinput.state, BinarySensorState.OFF)

        # Wrong significant bit: 0000 1011 = 11
        telegram_on = Telegram()
        telegram_on.payload = DPTBinary(11)
        self.loop.run_until_complete(asyncio.Task(binaryinput.process(telegram_on)))
        self.assertEqual(binaryinput.state, BinarySensorState.OFF)

        # Correct significant bit: 0000 1101 = 13
        telegram_on = Telegram()
        telegram_on.payload = DPTBinary(13)
        self.loop.run_until_complete(asyncio.Task(binaryinput.process(telegram_on)))
        self.assertEqual(binaryinput.state, BinarySensorState.ON)

        # Resetting, significant bit: 0000 0011 = 3
        telegram_off = Telegram()
        telegram_off.payload = DPTBinary(3)
        self.loop.run_until_complete(asyncio.Task(binaryinput.process(telegram_off)))
        self.assertEqual(binaryinput.state, BinarySensorState.OFF)