示例#1
0
文件: __init__.py 项目: jbouwh/core
 def connection_config(self) -> ConnectionConfig:
     """Return the connection_config."""
     _conn_type: str = self.entry.data[CONF_KNX_CONNECTION_TYPE]
     if _conn_type == CONF_KNX_ROUTING:
         return ConnectionConfig(
             connection_type=ConnectionType.ROUTING,
             local_ip=self.entry.data.get(CONF_KNX_LOCAL_IP),
             auto_reconnect=True,
             threaded=True,
         )
     if _conn_type == CONF_KNX_TUNNELING:
         return ConnectionConfig(
             connection_type=ConnectionType.TUNNELING,
             gateway_ip=self.entry.data[CONF_HOST],
             gateway_port=self.entry.data[CONF_PORT],
             local_ip=self.entry.data.get(CONF_KNX_LOCAL_IP),
             route_back=self.entry.data.get(CONF_KNX_ROUTE_BACK, False),
             auto_reconnect=True,
             threaded=True,
         )
     if _conn_type == CONF_KNX_TUNNELING_TCP:
         return ConnectionConfig(
             connection_type=ConnectionType.TUNNELING_TCP,
             gateway_ip=self.entry.data[CONF_HOST],
             gateway_port=self.entry.data[CONF_PORT],
             auto_reconnect=True,
             threaded=True,
         )
     if _conn_type == CONF_KNX_TUNNELING_TCP_SECURE:
         knxkeys_file: str | None = (self.hass.config.path(
             STORAGE_DIR,
             self.entry.data[CONF_KNX_KNXKEY_FILENAME],
         ) if self.entry.data.get(CONF_KNX_KNXKEY_FILENAME) is not None else
                                     None)
         return ConnectionConfig(
             connection_type=ConnectionType.TUNNELING_TCP_SECURE,
             gateway_ip=self.entry.data[CONF_HOST],
             gateway_port=self.entry.data[CONF_PORT],
             secure_config=SecureConfig(
                 user_id=self.entry.data.get(CONF_KNX_SECURE_USER_ID),
                 user_password=self.entry.data.get(
                     CONF_KNX_SECURE_USER_PASSWORD),
                 device_authentication_password=self.entry.data.get(
                     CONF_KNX_SECURE_DEVICE_AUTHENTICATION),
                 knxkeys_password=self.entry.data.get(
                     CONF_KNX_KNXKEY_PASSWORD),
                 knxkeys_file_path=knxkeys_file,
             ),
             auto_reconnect=True,
             threaded=True,
         )
     return ConnectionConfig(
         auto_reconnect=True,
         threaded=True,
     )
示例#2
0
 async def test_invalid_user_password(self):
     """Test ip secure."""
     gateway_ip = "192.168.1.1"
     connection_config = ConnectionConfig(
         connection_type=ConnectionType.TUNNELING_TCP_SECURE,
         gateway_ip=gateway_ip,
         secure_config=SecureConfig(user_id=1, ),
     )
     with pytest.raises(InvalidSecureConfiguration):
         interface = knx_interface_factory(self.xknx, connection_config)
         await interface.start()
示例#3
0
 async def test_invalid_user_id_secure_error(self):
     """Test ip secure."""
     gateway_ip = "192.168.1.1"
     knxkeys_file = os.path.join(os.path.dirname(__file__),
                                 "resources/testcase.knxkeys")
     connection_config = ConnectionConfig(
         connection_type=ConnectionType.TUNNELING_TCP_SECURE,
         gateway_ip=gateway_ip,
         secure_config=SecureConfig(user_id=12,
                                    knxkeys_file_path=knxkeys_file,
                                    knxkeys_password="******"),
     )
     with pytest.raises(InterfaceWithUserIdNotFound):
         interface = knx_interface_factory(self.xknx, connection_config)
         await interface.start()
示例#4
0
 async def test_start_secure_with_manual_passwords(self):
     """Test starting a secure connection from manual passwords."""
     gateway_ip = "192.168.1.1"
     connection_config = ConnectionConfig(
         connection_type=ConnectionType.TUNNELING_TCP_SECURE,
         gateway_ip=gateway_ip,
         secure_config=SecureConfig(
             user_id=3,
             device_authentication_password="******",
             user_password="******",
         ),
     )
     with patch("xknx.io.KNXIPInterface._start_secure_tunnelling_tcp"
                ) as start_secure_tunnel:
         interface = knx_interface_factory(self.xknx, connection_config)
         await interface.start()
         start_secure_tunnel.assert_called_once_with(
             gateway_ip="192.168.1.1",
             gateway_port=3671,
             user_id=3,
             user_password="******",
             device_authentication_password="******",
         )
     with patch("xknx.io.tunnel.SecureTunnel.connect") as connect_secure:
         interface = knx_interface_factory(self.xknx, connection_config)
         await interface.start()
         assert isinstance(interface._interface, SecureTunnel)
         assert interface._interface.gateway_ip == gateway_ip
         assert interface._interface.gateway_port == 3671
         assert interface._interface.auto_reconnect is True
         assert interface._interface.auto_reconnect_wait == 3
         assert interface._interface._user_id == 3
         assert interface._interface._user_password == "user1"
         assert (interface._interface._device_authentication_password ==
                 "authenticationcode")
         assert (  # pylint: disable=comparison-with-callable
             interface._interface.telegram_received_callback ==
             interface.telegram_received)
         connect_secure.assert_called_once_with()
示例#5
0
 async def test_start_secure_connection_knx_keys_first_interface(self):
     """Test starting a secure connection from a knxkeys file."""
     gateway_ip = "192.168.1.1"
     knxkeys_file = os.path.join(os.path.dirname(__file__),
                                 "resources/testcase.knxkeys")
     connection_config = ConnectionConfig(
         connection_type=ConnectionType.TUNNELING_TCP_SECURE,
         gateway_ip=gateway_ip,
         secure_config=SecureConfig(knxkeys_file_path=knxkeys_file,
                                    knxkeys_password="******"),
     )
     with patch("xknx.io.KNXIPInterface._start_secure_tunnelling_tcp"
                ) as start_secure_tunnel:
         interface = knx_interface_factory(self.xknx, connection_config)
         await interface.start()
         start_secure_tunnel.assert_called_once_with(
             gateway_ip="192.168.1.1",
             gateway_port=3671,
             user_id=3,
             user_password="******",
             device_authentication_password="******",
         )
     with patch("xknx.io.tunnel.SecureTunnel.connect") as connect_secure:
         interface = knx_interface_factory(self.xknx, connection_config)
         await interface.start()
         assert isinstance(interface._interface, SecureTunnel)
         assert interface._interface.gateway_ip == gateway_ip
         assert interface._interface.gateway_port == 3671
         assert interface._interface.auto_reconnect is True
         assert interface._interface.auto_reconnect_wait == 3
         assert interface._interface._user_id == 3
         assert interface._interface._user_password == "user1"
         assert (interface._interface._device_authentication_password ==
                 "authenticationcode")
         assert (  # pylint: disable=comparison-with-callable
             interface._interface.telegram_received_callback ==
             interface.telegram_received)
         connect_secure.assert_called_once_with()
示例#6
0
async def main() -> None:
    """Test connection with secure."""

    connection_config = ConnectionConfig(
        connection_type=ConnectionType.TUNNELING_TCP_SECURE,
        gateway_ip="192.168.1.188",
        secure_config=SecureConfig(
            user_id=4,
            knxkeys_file_path="/home/marvin/testcase.knxkeys",
            knxkeys_password="******",
        ),
    )
    xknx = XKNX(connection_config=connection_config)
    await xknx.start()

    await xknx.knxip_interface.send_telegram(
        Telegram(
            GroupAddress("1/0/32"),
            TelegramDirection.OUTGOING,
            GroupValueWrite(DPTBinary(1)),
        ))
    await asyncio.sleep(5)
    print("Tunnel connected")
    await xknx.stop()
示例#7
0
         CONF_HOST: "192.168.0.2",
         CONF_PORT: 3675,
         CONF_KNX_RATE_LIMIT: CONF_KNX_DEFAULT_RATE_LIMIT,
         CONF_KNX_STATE_UPDATER: CONF_KNX_DEFAULT_STATE_UPDATER,
         CONF_KNX_MCAST_PORT: DEFAULT_MCAST_PORT,
         CONF_KNX_MCAST_GRP: DEFAULT_MCAST_GRP,
         CONF_KNX_INDIVIDUAL_ADDRESS: XKNX.DEFAULT_ADDRESS,
         CONF_KNX_KNXKEY_FILENAME: "knx/testcase.knxkeys",
         CONF_KNX_KNXKEY_PASSWORD: "******",
     },
     ConnectionConfig(
         connection_type=ConnectionType.TUNNELING_TCP_SECURE,
         gateway_ip="192.168.0.2",
         gateway_port=3675,
         secure_config=SecureConfig(
             knxkeys_file_path="testcase.knxkeys",
             knxkeys_password="******"),
         auto_reconnect=True,
         threaded=True,
     ),
 ),
 (
     {
         CONF_KNX_CONNECTION_TYPE: CONF_KNX_TUNNELING_TCP_SECURE,
         CONF_HOST: "192.168.0.2",
         CONF_PORT: 3675,
         CONF_KNX_RATE_LIMIT: CONF_KNX_DEFAULT_RATE_LIMIT,
         CONF_KNX_STATE_UPDATER: CONF_KNX_DEFAULT_STATE_UPDATER,
         CONF_KNX_MCAST_PORT: DEFAULT_MCAST_PORT,
         CONF_KNX_MCAST_GRP: DEFAULT_MCAST_GRP,
         CONF_KNX_INDIVIDUAL_ADDRESS: XKNX.DEFAULT_ADDRESS,