Ejemplo n.º 1
0
    def test_set_invert(self):
        """Test switching on/off inverted switch."""
        xknx = XKNX()
        switch = Switch(xknx, "TestOutlet", group_address="1/2/3", invert=True)

        self.loop.run_until_complete(switch.set_on())
        self.assertEqual(xknx.telegrams.qsize(), 1)
        telegram = xknx.telegrams.get_nowait()
        self.assertEqual(
            telegram,
            Telegram(
                destination_address=GroupAddress("1/2/3"),
                payload=GroupValueWrite(DPTBinary(0)),
            ),
        )

        self.loop.run_until_complete(switch.set_off())
        self.assertEqual(xknx.telegrams.qsize(), 1)
        telegram = xknx.telegrams.get_nowait()
        self.assertEqual(
            telegram,
            Telegram(
                destination_address=GroupAddress("1/2/3"),
                payload=GroupValueWrite(DPTBinary(1)),
            ),
        )
class KnxSwitch(Accessory):
    """KNX Switch"""

    category = CATEGORY_SWITCH

    def __init__(self, *args, xknx, objname, group_address, **kwargs):
        super().__init__(*args, **kwargs)

        serv_switch = self.add_preload_service('Switch')
        self.char_switch = serv_switch.configure_char(
            'On', setter_callback=self.set_switch)

        self.switch = Switch(xknx, name=objname, group_address=group_address)

    # @Accessory.run_at_interval(3)
    # async def run(self):
    #     self.char_temp.set_value(random.randint(18, 26))

    def __setstate__(self, state):
        self.__dict__.update(state)
        #self._gpio_setup(self.pin)

    def set_switch(self, value):

        if value:
            logging.debug("set_switch: %s", value)
            self.driver.add_job(self.switch.set_on())

        else:
            logging.debug("set_switch: %s", value)

            self.driver.add_job(self.switch.set_off())
Ejemplo n.º 3
0
 def test_set_off(self):
     """Test switching off switch."""
     xknx = XKNX(loop=self.loop)
     switch = Switch(xknx, 'TestOutlet', group_address='1/2/3')
     self.loop.run_until_complete(asyncio.Task(switch.set_off()))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(telegram,
                      Telegram(GroupAddress('1/2/3'), payload=DPTBinary(0)))
Ejemplo n.º 4
0
 def test_set_off(self):
     """Test switching off switch."""
     xknx = XKNX(loop=self.loop)
     switch = Switch(xknx, 'TestOutlet', group_address='1/2/3')
     self.loop.run_until_complete(asyncio.Task(switch.set_off()))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(telegram,
                      Telegram(GroupAddress('1/2/3'), payload=DPTBinary(0)))
Ejemplo n.º 5
0
 def test_set_off(self):
     """Test switching off switch."""
     xknx = XKNX()
     switch = Switch(xknx, "TestOutlet", group_address="1/2/3")
     self.loop.run_until_complete(switch.set_off())
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram, Telegram(GroupAddress("1/2/3"), payload=DPTBinary(0))
     )
Ejemplo n.º 6
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)
Ejemplo n.º 7
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="5/5/5")

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

        self.assertEqual(binary_sensor.state, None)
        self.assertEqual(switch.state, False)

        telegram_on = Telegram(group_address=GroupAddress("1/2/3"),
                               payload=DPTBinary(1))

        with patch("time.time") as mock_time, patch(
                "asyncio.sleep", new_callable=AsyncMock) as mock_sleep:
            mock_time.return_value = 1599076123.0
            self.loop.run_until_complete(binary_sensor.process(telegram_on))

            self.loop.run_until_complete(
                xknx.devices.process(xknx.telegrams.get_nowait()))

            self.assertEqual(binary_sensor.state, True)
            self.assertEqual(switch.state, True)

            self.loop.run_until_complete(switch.set_off())
            self.loop.run_until_complete(
                xknx.devices.process(xknx.telegrams.get_nowait()))

            self.assertEqual(switch.state, False)
            self.assertEqual(binary_sensor.state, True)
            # add a second to the time to avoid double tap behaviour here
            mock_time.return_value += BinarySensor.DEFAULT_CONTEXT_TIMEOUT
            self.loop.run_until_complete(binary_sensor.process(telegram_on))

            self.loop.run_until_complete(
                xknx.devices.process(xknx.telegrams.get_nowait()))

            self.assertEqual(binary_sensor.state, True)
            self.assertEqual(switch.state, True)