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())
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)), ), )
def test_set_on(self): """Test switching on switch.""" xknx = XKNX(loop=self.loop) switch = Switch(xknx, 'TestOutlet', group_address='1/2/3') self.loop.run_until_complete(asyncio.Task(switch.set_on())) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual(telegram, Telegram(GroupAddress('1/2/3'), payload=DPTBinary(1)))
def test_set_on(self): """Test switching on switch.""" xknx = XKNX() switch = Switch(xknx, "TestOutlet", group_address="1/2/3") self.loop.run_until_complete(switch.set_on()) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual( telegram, Telegram(GroupAddress("1/2/3"), payload=DPTBinary(1)) )