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)
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)
def test_telegram_set(self): """Test parsing and streaming CEMIFrame KNX/IP packet with DPTArray/DPTTime as payload.""" xknx = XKNX(loop=self.loop) knxipframe = KNXIPFrame(xknx) knxipframe.init(KNXIPServiceType.ROUTING_INDICATION) knxipframe.body.src_addr = PhysicalAddress("1.2.2") telegram = Telegram() telegram.group_address = GroupAddress(337) telegram.payload = DPTArray(DPTTime().to_knx({ 'hours': 13, 'minutes': 23, 'seconds': 42 })) knxipframe.body.telegram = telegram knxipframe.body.set_hops(5) knxipframe.normalize() raw = ((0x06, 0x10, 0x05, 0x30, 0x00, 0x14, 0x29, 0x00, 0xbc, 0xd0, 0x12, 0x02, 0x01, 0x51, 0x04, 0x00, 0x80, 13, 23, 42)) self.assertEqual(knxipframe.header.to_knx(), list(raw[0:6])) self.assertEqual(knxipframe.body.to_knx(), list(raw[6:])) self.assertEqual(knxipframe.to_knx(), list(raw))
async def service_send_to_knx_bus(self, call): """Service for sending an arbitrary KNX message to the KNX bus.""" attr_payload = call.data.get(SERVICE_KNX_ATTR_PAYLOAD) attr_address = call.data.get(SERVICE_KNX_ATTR_ADDRESS) attr_type = call.data.get(SERVICE_KNX_ATTR_TYPE) def calculate_payload(attr_payload): """Calculate payload depending on type of attribute.""" if attr_type is not None: transcoder = DPTBase.parse_transcoder(attr_type) if transcoder is None: raise ValueError( f"Invalid type for knx.send service: {attr_type}") return DPTArray(transcoder.to_knx(attr_payload)) if isinstance(attr_payload, int): return DPTBinary(attr_payload) return DPTArray(attr_payload) payload = calculate_payload(attr_payload) address = GroupAddress(attr_address) telegram = Telegram() telegram.payload = payload telegram.group_address = address await self.xknx.telegrams.put(telegram)
async def send(self, response=False): """Send payload as telegram to KNX bus.""" telegram = Telegram() telegram.group_address = self.group_address telegram.telegramtype = TelegramType.GROUP_RESPONSE \ if response else TelegramType.GROUP_WRITE telegram.payload = self.payload await self.xknx.telegrams.put(telegram)
def test_process(self): """Test process / reading telegrams from telegram queue.""" xknx = XKNX(loop=self.loop) binaryinput = BinarySensor(xknx, 'TestInput', '1/2/3') self.assertEqual(binaryinput.state, BinarySensorState.OFF) telegram_on = Telegram() telegram_on.payload = DPTBinary(1) self.loop.run_until_complete(asyncio.Task(binaryinput.process(telegram_on))) self.assertEqual(binaryinput.state, BinarySensorState.ON) telegram_off = Telegram() telegram_off.payload = DPTBinary(0) self.loop.run_until_complete(asyncio.Task(binaryinput.process(telegram_off))) self.assertEqual(binaryinput.state, BinarySensorState.OFF)
def test_process_power_status(self): """Test process / reading telegrams from telegram queue. Test if DPT20.105 controller mode is set correctly.""" xknx = XKNX(loop=self.loop) climate = Climate(xknx, 'TestClimate', group_address_on_off='1/2/2') telegram = Telegram(GroupAddress('1/2/2')) telegram.payload = DPTBinary(1) self.loop.run_until_complete(asyncio.Task(climate.process(telegram))) self.assertEqual(climate.is_on, True) climate_inv = Climate(xknx, 'TestClimate', group_address_on_off='1/2/2', on_off_invert=True) telegram = Telegram(GroupAddress('1/2/2')) telegram.payload = DPTBinary(1) self.loop.run_until_complete( asyncio.Task(climate_inv.process(telegram))) self.assertEqual(climate_inv.is_on, False)
async def send(self, response=False): """Send payload as telegram to KNX bus.""" telegram = Telegram() # always use the first group address to send telegram.group_address = self.group_address[0] telegram.telegramtype = ( TelegramType.GROUP_RESPONSE if response else TelegramType.GROUP_WRITE ) telegram.payload = self.payload # print("telegram", telegram) await self.xknx.telegrams.put(telegram)
def test_process_temperature(self): """Test process / reading telegrams from telegram queue. Test if temperature is processed correctly.""" xknx = XKNX(loop=self.loop) climate = Climate(xknx, 'TestClimate', group_address_temperature='1/2/3') telegram = Telegram(GroupAddress('1/2/3')) telegram.payload = DPTArray(DPTTemperature().to_knx(21.34)) self.loop.run_until_complete(asyncio.Task(climate.process(telegram))) self.assertEqual(climate.temperature.value, 21.34)
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)
def test_process(self): """Test process / reading telegrams from telegram queue. Test if device was updated.""" xknx = XKNX(loop=self.loop) switch = Switch(xknx, 'TestOutlet', group_address='1/2/3') self.assertEqual(switch.state, False) telegram_on = Telegram() telegram_on.group_address = GroupAddress('1/2/3') telegram_on.payload = DPTBinary(1) self.loop.run_until_complete(asyncio.Task(switch.process(telegram_on))) self.assertEqual(switch.state, True) telegram_off = Telegram() telegram_off.group_address = GroupAddress('1/2/3') telegram_off.payload = DPTBinary(0) self.loop.run_until_complete(asyncio.Task( switch.process(telegram_off))) self.assertEqual(switch.state, False)
def test_process_controller_mode(self): """Test process / reading telegrams from telegram queue. Test if DPT20.105 controller mode is set correctly.""" xknx = XKNX(loop=self.loop) climate_mode = ClimateMode(xknx, 'TestClimate', group_address_controller_mode='1/2/5') for _, operation_mode in DPTHVACContrMode.SUPPORTED_MODES.items(): telegram = Telegram(GroupAddress('1/2/5')) telegram.payload = DPTArray( DPTHVACContrMode.to_knx(operation_mode)) self.loop.run_until_complete( asyncio.Task(climate_mode.process(telegram))) self.assertEqual(climate_mode.operation_mode, operation_mode)
def test_process(self): """Test process / reading telegrams from telegram queue.""" xknx = XKNX(loop=self.loop) sensor = Sensor(xknx, 'TestSensor', value_type='temperature', group_address_state='1/2/3') telegram = Telegram(GroupAddress('1/2/3')) telegram.payload = DPTArray((0x06, 0xa0)) self.loop.run_until_complete(asyncio.Task(sensor.process(telegram))) self.assertEqual(sensor.sensor_value.payload, DPTArray((0x06, 0xa0))) self.assertEqual(sensor.resolve_state(), 16.96)
def test_process_operation_mode(self): """Test process / reading telegrams from telegram queue. Test if setpoint is processed correctly.""" xknx = XKNX(loop=self.loop) climate_mode = ClimateMode(xknx, 'TestClimate', group_address_operation_mode='1/2/5', group_address_controller_status='1/2/3') for operation_mode in DPT_20102_MODES: telegram = Telegram(GroupAddress('1/2/5')) telegram.payload = DPTArray(DPTHVACMode.to_knx(operation_mode)) self.loop.run_until_complete( asyncio.Task(climate_mode.process(telegram))) self.assertEqual(climate_mode.operation_mode, operation_mode) for operation_mode in DPT_20102_MODES: if operation_mode == HVACOperationMode.AUTO: continue telegram = Telegram(GroupAddress('1/2/3')) telegram.payload = DPTArray( DPTControllerStatus.to_knx(operation_mode)) self.loop.run_until_complete( asyncio.Task(climate_mode.process(telegram))) self.assertEqual(climate_mode.operation_mode, operation_mode)
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') xknx.devices.add(switch) 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) 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) self.loop.run_until_complete(asyncio.Task(switch.set_off())) self.assertEqual( xknx.devices['TestOutlet'].state, False) self.assertEqual( xknx.devices['TestInput'].state, True) 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, True) self.assertEqual( xknx.devices['TestOutlet'].state, True)
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)
async def service_send_to_knx_bus(self, call): """Service for sending an arbitrary KNX message to the KNX bus.""" attr_payload = call.data.get(SERVICE_KNX_ATTR_PAYLOAD) attr_address = call.data.get(SERVICE_KNX_ATTR_ADDRESS) def calculate_payload(attr_payload): """Calculate payload depending on type of attribute.""" if isinstance(attr_payload, int): return DPTBinary(attr_payload) return DPTArray(attr_payload) payload = calculate_payload(attr_payload) address = GroupAddress(attr_address) telegram = Telegram() telegram.payload = payload telegram.group_address = address await self.xknx.telegrams.put(telegram)
def test_process_callback_temp(self): """Test process / reading telegrams from telegram queue. Test if callback is executed when receiving temperature.""" # pylint: disable=no-self-use xknx = XKNX(loop=self.loop) climate = Climate(xknx, 'TestClimate', group_address_temperature='1/2/3') after_update_callback = Mock() async def async_after_update_callback(device): """Async callback.""" after_update_callback(device) climate.register_device_updated_cb(async_after_update_callback) telegram = Telegram(GroupAddress('1/2/3')) telegram.payload = DPTArray(DPTTemperature().to_knx(21.34)) self.loop.run_until_complete(asyncio.Task(climate.process(telegram))) after_update_callback.assert_called_with(climate)
def telegram(self): """Return telegram.""" telegram = Telegram() telegram.payload = self.payload telegram.group_address = self.dst_addr def resolve_telegram_type(cmd): """Return telegram type from APCI Command.""" if cmd == APCICommand.GROUP_WRITE: return TelegramType.GROUP_WRITE if cmd == APCICommand.GROUP_READ: return TelegramType.GROUP_READ if cmd == APCICommand.GROUP_RESPONSE: return TelegramType.GROUP_RESPONSE raise ConversionError("Telegram not implemented for {0}".format(self.cmd)) telegram.telegramtype = resolve_telegram_type(self.cmd) # TODO: Set telegram.direction [additional flag within KNXIP] return telegram
def test_process_callback(self): """Test process / reading telegrams from telegram queue. Test if callback is called.""" # pylint: disable=no-self-use xknx = XKNX(loop=self.loop) sensor = Sensor(xknx, 'TestSensor', group_address_state='1/2/3', value_type="temperature") after_update_callback = Mock() async def async_after_update_callback(device): """Async callback.""" after_update_callback(device) sensor.register_device_updated_cb(async_after_update_callback) telegram = Telegram(GroupAddress('1/2/3')) telegram.payload = DPTArray((0x01, 0x02)) self.loop.run_until_complete(asyncio.Task(sensor.process(telegram))) after_update_callback.assert_called_with(sensor)
def test_maximum_apci(self): """Test parsing and streaming CEMIFrame KNX/IP packet, testing maximum APCI.""" telegram = Telegram() telegram.group_address = GroupAddress(337) telegram.payload = DPTBinary(DPTBinary.APCI_MAX_VALUE) xknx = XKNX(loop=self.loop) knxipframe = KNXIPFrame(xknx) knxipframe.init(KNXIPServiceType.ROUTING_INDICATION) knxipframe.body.cemi.src_addr = PhysicalAddress("1.3.1") knxipframe.body.cemi.telegram = telegram knxipframe.body.cemi.set_hops(5) knxipframe.normalize() raw = ((0x06, 0x10, 0x05, 0x30, 0x00, 0x11, 0x29, 0x00, 0xbc, 0xd0, 0x13, 0x01, 0x01, 0x51, 0x01, 0x00, 0xbf)) self.assertEqual(knxipframe.to_knx(), list(raw)) knxipframe2 = KNXIPFrame(xknx) knxipframe2.init(KNXIPServiceType.ROUTING_INDICATION) knxipframe2.from_knx(knxipframe.to_knx()) self.assertEqual(knxipframe2.body.cemi.telegram, telegram)
def test_process_callback(self): """Test process / reading telegrams from telegram queue. Test if callback was called.""" # pylint: disable=no-self-use xknx = XKNX(loop=self.loop) switch = Switch(xknx, 'TestOutlet', group_address='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.group_address = GroupAddress('1/2/3') telegram.payload = DPTBinary(1) self.loop.run_until_complete(asyncio.Task(switch.process(telegram))) after_update_callback.assert_called_with(switch)