def test_connectionstate_request(self): """Test string representation of KNX/IP ConnectionStateRequest.""" xknx = XKNX(loop=self.loop) connectionstate_request = ConnectionStateRequest(xknx) connectionstate_request.communication_channel_id = 23 connectionstate_request.control_endpoint = HPAI(ip_addr='192.168.42.1', port=33941) self.assertEqual( str(connectionstate_request), '<ConnectionStateRequest CommunicationChannelID="23", control_endpoint="<HPAI 192.168.42.1:33941 />" />')
def test_connectionstate_request(self): """Test string representation of KNX/IP ConnectionStateRequest.""" xknx = XKNX(loop=self.loop) connectionstate_request = ConnectionStateRequest(xknx) connectionstate_request.communication_channel_id = 23 connectionstate_request.control_endpoint = HPAI(ip_addr='192.168.42.1', port=33941) self.assertEqual( str(connectionstate_request), '<ConnectionStateRequest CommunicationChannelID="23", control_endpoint="<HPAI 192.168.42.1:33941 />" />')
def test_connectionstate_request(self): """Test string representation of KNX/IP ConnectionStateRequest.""" connectionstate_request = ConnectionStateRequest() connectionstate_request.communication_channel_id = 23 connectionstate_request.control_endpoint = HPAI(ip_addr="192.168.42.1", port=33941) assert ( str(connectionstate_request) == '<ConnectionStateRequest communication_channel_id="23", control_endpoint="192.168.42.1:33941/udp" />' )
def create_knxipframe(self) -> KNXIPFrame: """Create KNX/IP Frame object to be sent to device.""" connectionstate_request = ConnectionStateRequest( communication_channel_id=self.communication_channel_id, control_endpoint=self.local_hpai, ) return KNXIPFrame.init_from_body(connectionstate_request)
def create_knxipframe(self) -> KNXIPFrame: """Create KNX/IP Frame object to be sent to device.""" (local_addr, local_port) = self.udpclient.getsockname() connectionstate_request = ConnectionStateRequest( self.xknx, communication_channel_id=self.communication_channel_id, control_endpoint=HPAI(ip_addr=local_addr, port=local_port), ) return KNXIPFrame.init_from_body(connectionstate_request)
def test_connectionstate(self): """Test connectionstateing from KNX bus.""" xknx = XKNX() communication_channel_id = 23 udp_client = UDPClient(xknx, ("192.168.1.1", 0), ("192.168.1.2", 1234)) connectionstate = ConnectionState(xknx, udp_client, communication_channel_id, route_back=False) connectionstate.timeout_in_seconds = 0 self.assertEqual(connectionstate.awaited_response_class, ConnectionStateResponse) self.assertEqual(connectionstate.communication_channel_id, communication_channel_id) # Expected KNX/IP-Frame: exp_knxipframe = KNXIPFrame.init_from_body( ConnectionStateRequest( xknx, communication_channel_id=communication_channel_id, control_endpoint=HPAI(ip_addr="192.168.1.3", port=4321), )) with patch("xknx.io.UDPClient.send") as mock_udp_send, patch( "xknx.io.UDPClient.getsockname") as mock_udp_getsockname: mock_udp_getsockname.return_value = ("192.168.1.3", 4321) self.loop.run_until_complete(connectionstate.start()) mock_udp_send.assert_called_with(exp_knxipframe) # Response KNX/IP-Frame with wrong type wrong_knxipframe = KNXIPFrame(xknx) wrong_knxipframe.init(KNXIPServiceType.CONNECTIONSTATE_REQUEST) with patch("logging.Logger.warning") as mock_warning: connectionstate.response_rec_callback(wrong_knxipframe, None) mock_warning.assert_called_with("Could not understand knxipframe") # Response KNX/IP-Frame with error: err_knxipframe = KNXIPFrame(xknx) err_knxipframe.init(KNXIPServiceType.CONNECTIONSTATE_RESPONSE) err_knxipframe.body.status_code = ErrorCode.E_CONNECTION_ID with patch("logging.Logger.debug") as mock_warning: connectionstate.response_rec_callback(err_knxipframe, None) mock_warning.assert_called_with( "Error: KNX bus responded to request of type '%s' with error in '%s': %s", type(connectionstate).__name__, type(err_knxipframe.body).__name__, ErrorCode.E_CONNECTION_ID, ) # Correct Response KNX/IP-Frame: res_knxipframe = KNXIPFrame(xknx) res_knxipframe.init(KNXIPServiceType.CONNECTIONSTATE_RESPONSE) connectionstate.response_rec_callback(res_knxipframe, None) self.assertTrue(connectionstate.success)
async def test_connectionstate(self): """Test connectionstateing from KNX bus.""" communication_channel_id = 23 udp_transport = UDPTransport(("192.168.1.1", 0), ("192.168.1.2", 1234)) local_hpai = HPAI(ip_addr="192.168.1.3", port=4321) connectionstate = ConnectionState(udp_transport, communication_channel_id, local_hpai=local_hpai) connectionstate.timeout_in_seconds = 0 assert connectionstate.awaited_response_class == ConnectionStateResponse assert connectionstate.communication_channel_id == communication_channel_id # Expected KNX/IP-Frame: exp_knxipframe = KNXIPFrame.init_from_body( ConnectionStateRequest( communication_channel_id=communication_channel_id, control_endpoint=local_hpai, )) with patch( "xknx.io.transport.UDPTransport.send") as mock_udp_send, patch( "xknx.io.transport.UDPTransport.getsockname" ) as mock_udp_getsockname: mock_udp_getsockname.return_value = ("192.168.1.3", 4321) await connectionstate.start() mock_udp_send.assert_called_with(exp_knxipframe) # Response KNX/IP-Frame with wrong type wrong_knxipframe = KNXIPFrame() wrong_knxipframe.init(KNXIPServiceType.CONNECTIONSTATE_REQUEST) with patch("logging.Logger.warning") as mock_warning: connectionstate.response_rec_callback(wrong_knxipframe, HPAI(), None) mock_warning.assert_called_with("Could not understand knxipframe") # Response KNX/IP-Frame with error: err_knxipframe = KNXIPFrame() err_knxipframe.init(KNXIPServiceType.CONNECTIONSTATE_RESPONSE) err_knxipframe.body.status_code = ErrorCode.E_CONNECTION_ID with patch("logging.Logger.debug") as mock_warning: connectionstate.response_rec_callback(err_knxipframe, HPAI(), None) mock_warning.assert_called_with( "Error: KNX bus responded to request of type '%s' with error in '%s': %s", type(connectionstate).__name__, type(err_knxipframe.body).__name__, ErrorCode.E_CONNECTION_ID, ) # Correct Response KNX/IP-Frame: res_knxipframe = KNXIPFrame() res_knxipframe.init(KNXIPServiceType.CONNECTIONSTATE_RESPONSE) connectionstate.response_rec_callback(res_knxipframe, HPAI(), None) assert connectionstate.success
def test_connection_state_request(self): """Test parsing and streaming connection state request KNX/IP packet.""" raw = bytes.fromhex("06 10 02 07 00 10 15 00 08 01 C0 A8 C8 0C C3 B4") knxipframe = KNXIPFrame() knxipframe.from_knx(raw) assert isinstance(knxipframe.body, ConnectionStateRequest) assert knxipframe.body.communication_channel_id == 21 assert knxipframe.body.control_endpoint == HPAI( ip_addr="192.168.200.12", port=50100) connectionstate_request = ConnectionStateRequest( communication_channel_id=21, control_endpoint=HPAI(ip_addr="192.168.200.12", port=50100), ) knxipframe2 = KNXIPFrame.init_from_body(connectionstate_request) assert knxipframe2.to_knx() == raw
def test_connection_state_request(self): """Test parsing and streaming connection state request KNX/IP packet.""" raw = ( 0x06, 0x10, 0x02, 0x07, 0x00, 0x10, 0x15, 0x00, 0x08, 0x01, 0xC0, 0xA8, 0xC8, 0x0C, 0xC3, 0xB4, ) xknx = XKNX() knxipframe = KNXIPFrame(xknx) knxipframe.from_knx(raw) self.assertTrue(isinstance(knxipframe.body, ConnectionStateRequest)) self.assertEqual(knxipframe.body.communication_channel_id, 21) self.assertEqual(knxipframe.body.control_endpoint, HPAI(ip_addr="192.168.200.12", port=50100)) connectionstate_request = ConnectionStateRequest( xknx, communication_channel_id=21, control_endpoint=HPAI(ip_addr="192.168.200.12", port=50100), ) knxipframe2 = KNXIPFrame.init_from_body(connectionstate_request) self.assertEqual(knxipframe2.to_knx(), list(raw))