示例#1
0
文件: devices_test.py 项目: vosc/xknx
    def test_len(self):
        """Test len() function."""
        xknx = XKNX(loop=self.loop)
        devices = Devices()
        self.assertEqual(len(devices), 0)

        light1 = Light(xknx,
                       'Living-Room.Light_1',
                       group_address_switch='1/6/7')
        devices.add(light1)
        self.assertEqual(len(devices), 1)

        sensor1 = BinarySensor(xknx,
                               'DiningRoom.Motion.Sensor',
                               group_address_state='3/0/1',
                               significant_bit=2)
        devices.add(sensor1)
        self.assertEqual(len(devices), 2)

        sensor2 = BinarySensor(xknx,
                               'DiningRoom.Motion.Sensor',
                               group_address_state='3/0/1',
                               significant_bit=3)
        devices.add(sensor2)
        self.assertEqual(len(devices), 3)

        light2 = Light(xknx,
                       'Living-Room.Light_2',
                       group_address_switch='1/6/8')
        devices.add(light2)
        self.assertEqual(len(devices), 4)
示例#2
0
文件: xknx.py 项目: qvistgaard/xknx
    def __init__(self,
                 config=None,
                 loop=None,
                 own_address=Address(DEFAULT_ADDRESS),
                 address_format=AddressFormat.LEVEL3,
                 telegram_received_cb=None,
                 device_updated_cb=None):
        """Initialize XKNX class."""
        # pylint: disable=too-many-arguments
        self.devices = Devices()
        self.telegrams = asyncio.Queue()
        self.loop = loop or asyncio.get_event_loop()
        self.sigint_received = asyncio.Event()
        self.telegram_queue = TelegramQueue(self)
        self.state_updater = None
        self.knxip_interface = None
        self.started = False
        self.address_format = address_format
        self.own_address = own_address
        self.logger = logging.getLogger('xknx.log')
        self.knx_logger = logging.getLogger('xknx.knx')
        self.telegram_logger = logging.getLogger('xknx.telegram')

        if config is not None:
            Config(self).read(config)

        if telegram_received_cb is not None:
            self.telegram_queue.register_telegram_received_cb(
                telegram_received_cb)

        if device_updated_cb is not None:
            self.devices.register_device_updated_cb(device_updated_cb)
示例#3
0
文件: devices_test.py 项目: vosc/xknx
    def test_iter(self):
        """Test __iter__() function."""
        xknx = XKNX(loop=self.loop)
        devices = Devices()

        light1 = Light(xknx,
                       'Living-Room.Light_1',
                       group_address_switch='1/6/7')
        devices.add(light1)

        sensor1 = BinarySensor(xknx,
                               'DiningRoom.Motion.Sensor',
                               group_address_state='3/0/1',
                               significant_bit=2)
        devices.add(sensor1)

        sensor2 = BinarySensor(xknx,
                               'DiningRoom.Motion.Sensor',
                               group_address_state='3/0/1',
                               significant_bit=3)
        devices.add(sensor2)

        light2 = Light(xknx,
                       'Living-Room.Light_2',
                       group_address_switch='1/6/8')

        devices.add(light2)

        self.assertEqual(
            tuple(devices.__iter__()),
            (light1, sensor1, sensor2, light2))
示例#4
0
    def test_iter(self):
        """Test __iter__() function."""
        xknx = XKNX()
        devices = Devices()

        light1 = Light(xknx,
                       "Living-Room.Light_1",
                       group_address_switch="1/6/7")
        devices.add(light1)

        sensor1 = BinarySensor(xknx,
                               "DiningRoom.Motion.Sensor",
                               group_address_state="3/0/1")
        devices.add(sensor1)

        sensor2 = BinarySensor(xknx,
                               "DiningRoom.Motion.Sensor",
                               group_address_state="3/0/1")
        devices.add(sensor2)

        light2 = Light(xknx,
                       "Living-Room.Light_2",
                       group_address_switch="1/6/8")

        devices.add(light2)

        self.assertEqual(tuple(devices.__iter__()),
                         (light1, sensor1, sensor2, light2))
示例#5
0
    def test_device_by_group_address(self):
        """Test get devices by group address."""
        xknx = XKNX(loop=self.loop)
        devices = Devices()

        light1 = Light(xknx,
                       'Living-Room.Light_1',
                       group_address_switch='1/6/7')
        devices.add(light1)

        sensor1 = BinarySensor(xknx,
                               'DiningRoom.Motion.Sensor',
                               group_address_state='3/0/1')
        devices.add(sensor1)

        sensor2 = BinarySensor(xknx,
                               'DiningRoom.Motion.Sensor',
                               group_address_state='3/0/1')
        devices.add(sensor2)

        light2 = Light(xknx,
                       'Living-Room.Light_2',
                       group_address_switch='1/6/8')
        devices.add(light2)

        self.assertEqual(
            tuple(devices.devices_by_group_address(GroupAddress('1/6/7'))),
            (light1, ))
        self.assertEqual(
            tuple(devices.devices_by_group_address(GroupAddress('1/6/8'))),
            (light2, ))
        self.assertEqual(
            tuple(devices.devices_by_group_address(GroupAddress('3/0/1'))),
            (sensor1, sensor2))
示例#6
0
    def test_device_by_group_address(self):
        """Test get devices by group address."""
        xknx = XKNX()
        devices = Devices()

        light1 = Light(xknx,
                       "Living-Room.Light_1",
                       group_address_switch="1/6/7")
        devices.add(light1)

        sensor1 = BinarySensor(xknx,
                               "DiningRoom.Motion.Sensor",
                               group_address_state="3/0/1")
        devices.add(sensor1)

        sensor2 = BinarySensor(xknx,
                               "DiningRoom.Motion.Sensor",
                               group_address_state="3/0/1")
        devices.add(sensor2)

        light2 = Light(xknx,
                       "Living-Room.Light_2",
                       group_address_switch="1/6/8")
        devices.add(light2)

        self.assertEqual(
            tuple(devices.devices_by_group_address(GroupAddress("1/6/7"))),
            (light1, ))
        self.assertEqual(
            tuple(devices.devices_by_group_address(GroupAddress("1/6/8"))),
            (light2, ))
        self.assertEqual(
            tuple(devices.devices_by_group_address(GroupAddress("3/0/1"))),
            (sensor1, sensor2),
        )
示例#7
0
文件: xknx.py 项目: XKNX/xknx
    def __init__(
            self,
            own_address: str | IndividualAddress = DEFAULT_ADDRESS,
            address_format: GroupAddressType = GroupAddressType.LONG,
            telegram_received_cb: Callable[[Telegram], Awaitable[None]]
        | None = None,
            device_updated_cb: Callable[[Device], Awaitable[None]]
        | None = None,
            connection_state_changed_cb: Callable[[XknxConnectionState],
                                                  Awaitable[None]]
        | None = None,
            rate_limit: int = DEFAULT_RATE_LIMIT,
            multicast_group: str = DEFAULT_MCAST_GRP,
            multicast_port: int = DEFAULT_MCAST_PORT,
            log_directory: str | None = None,
            state_updater: TrackerOptionType = False,
            daemon_mode: bool = False,
            connection_config: ConnectionConfig = ConnectionConfig(),
    ) -> None:
        """Initialize XKNX class."""
        self.connection_manager = ConnectionManager()
        self.devices = Devices()
        self.knxip_interface = knx_interface_factory(
            self, connection_config=connection_config)
        self.management = Management(self)
        self.telegrams: asyncio.Queue[Telegram | None] = asyncio.Queue()
        self.telegram_queue = TelegramQueue(self)
        self.state_updater = StateUpdater(self,
                                          default_tracker_option=state_updater)
        self.task_registry = TaskRegistry(self)

        self.current_address = IndividualAddress(0)
        self.daemon_mode = daemon_mode
        self.multicast_group = multicast_group
        self.multicast_port = multicast_port
        self.own_address = IndividualAddress(own_address)
        self.rate_limit = rate_limit
        self.sigint_received = asyncio.Event()
        self.started = asyncio.Event()
        self.version = VERSION

        GroupAddress.address_format = address_format  # for global string representation
        if log_directory is not None:
            self.setup_logging(log_directory)

        if telegram_received_cb is not None:
            self.telegram_queue.register_telegram_received_cb(
                telegram_received_cb)

        if device_updated_cb is not None:
            self.devices.register_device_updated_cb(device_updated_cb)

        if connection_state_changed_cb is not None:
            self.connection_manager.register_connection_state_changed_cb(
                connection_state_changed_cb)
示例#8
0
文件: devices_test.py 项目: XKNX/xknx
    def test_iter(self):
        """Test __iter__() function."""
        xknx = XKNX()
        devices = Devices()

        light1 = Light(xknx, "Livingroom", group_address_switch="1/6/7")
        sensor1 = BinarySensor(xknx, "Diningroom", group_address_state="3/0/1")
        sensor2 = BinarySensor(xknx, "Diningroom", group_address_state="3/0/1")
        light2 = Light(xknx, "Livingroom", group_address_switch="1/6/8")
        devices.add(light1)
        devices.add(sensor1)
        devices.add(sensor2)
        devices.add(light2)

        assert tuple(devices.__iter__()) == (light1, sensor1, sensor2, light2)
示例#9
0
文件: devices_test.py 项目: vosc/xknx
    def test_contains(self):
        """Test __contains__() function."""
        xknx = XKNX(loop=self.loop)
        devices = Devices()

        light1 = Light(xknx,
                       'Living-Room.Light_1',
                       group_address_switch='1/6/7')
        devices.add(light1)
        light2 = Light(xknx,
                       'Living-Room.Light_2',
                       group_address_switch='1/6/8')
        devices.add(light2)
        self.assertTrue('Living-Room.Light_1' in devices)
        self.assertTrue('Living-Room.Light_2' in devices)
        self.assertFalse('Living-Room.Light_3' in devices)
示例#10
0
文件: devices_test.py 项目: vosc/xknx
 def test_modification_of_device(self):
     """Test if devices object does store references and not copies of objects."""
     xknx = XKNX(loop=self.loop)
     devices = Devices()
     light1 = Light(xknx,
                    'Living-Room.Light_1',
                    group_address_switch='1/6/7')
     devices.add(light1)
     for device in devices:
         self.loop.run_until_complete(asyncio.Task(device.set_on()))
     self.assertTrue(light1.state)
     device2 = devices["Living-Room.Light_1"]
     self.loop.run_until_complete(asyncio.Task(device2.set_off()))
     self.assertFalse(light1.state)
     for device in devices.devices_by_group_address(GroupAddress('1/6/7')):
         self.loop.run_until_complete(asyncio.Task(device.set_on()))
     self.assertTrue(light1.state)
示例#11
0
文件: xknx.py 项目: mielune/xknx
    def __init__(
            self,
            config=None,
            own_address=DEFAULT_ADDRESS,
            address_format=GroupAddressType.LONG,
            telegram_received_cb=None,
            device_updated_cb=None,
            rate_limit=DEFAULT_RATE_LIMIT,
            multicast_group=DEFAULT_MCAST_GRP,
            multicast_port=DEFAULT_MCAST_PORT,
            log_directory=None,
            state_updater=False,
            daemon_mode=False,
            connection_config=ConnectionConfig(),
    ):
        """Initialize XKNX class."""
        # pylint: disable=too-many-arguments
        self.devices = Devices()
        self.telegrams = asyncio.Queue()
        self.sigint_received = asyncio.Event()
        self.telegram_queue = TelegramQueue(self)
        self.state_updater = StateUpdater(self)
        self.knxip_interface = None
        self.started = asyncio.Event()
        self.address_format = address_format
        self.own_address = PhysicalAddress(own_address)
        self.rate_limit = rate_limit
        self.multicast_group = multicast_group
        self.multicast_port = multicast_port
        self.connection_config = connection_config
        self.start_state_updater = state_updater
        self.daemon_mode = daemon_mode
        self.version = VERSION

        if log_directory is not None:
            self.setup_logging(log_directory)

        if config is not None:
            Config(self).read(config)

        if telegram_received_cb is not None:
            self.telegram_queue.register_telegram_received_cb(
                telegram_received_cb)

        if device_updated_cb is not None:
            self.devices.register_device_updated_cb(device_updated_cb)
示例#12
0
文件: xknx.py 项目: spacegaier/xknx
    def __init__(
        self,
        config: Optional[str] = None,
        own_address: Union[str, IndividualAddress] = DEFAULT_ADDRESS,
        address_format: GroupAddressType = GroupAddressType.LONG,
        telegram_received_cb: Optional[Callable[[Telegram], Awaitable[None]]] = None,
        device_updated_cb: Optional[Callable[[Device], Awaitable[None]]] = None,
        rate_limit: int = DEFAULT_RATE_LIMIT,
        multicast_group: str = DEFAULT_MCAST_GRP,
        multicast_port: int = DEFAULT_MCAST_PORT,
        log_directory: Optional[str] = None,
        state_updater: bool = False,
        daemon_mode: bool = False,
        connection_config: ConnectionConfig = ConnectionConfig(),
    ) -> None:
        """Initialize XKNX class."""
        # pylint: disable=too-many-arguments
        self.devices = Devices()
        self.telegrams: asyncio.Queue[Optional[Telegram]] = asyncio.Queue()
        self.sigint_received = asyncio.Event()
        self.telegram_queue = TelegramQueue(self)
        self.state_updater = StateUpdater(self)
        self.knxip_interface: Optional[KNXIPInterface] = None
        self.started = asyncio.Event()
        self.connected = asyncio.Event()
        self.address_format = address_format
        self.own_address = IndividualAddress(own_address)
        self.rate_limit = rate_limit
        self.multicast_group = multicast_group
        self.multicast_port = multicast_port
        self.connection_config = connection_config
        self.start_state_updater = state_updater
        self.daemon_mode = daemon_mode
        self.version = VERSION

        if log_directory is not None:
            self.setup_logging(log_directory)

        if config is not None:
            Config(self).read(config)

        if telegram_received_cb is not None:
            self.telegram_queue.register_telegram_received_cb(telegram_received_cb)

        if device_updated_cb is not None:
            self.devices.register_device_updated_cb(device_updated_cb)
示例#13
0
文件: xknx.py 项目: cian/xknx
    def __init__(
        self,
        config=None,
        loop=None,
        own_address=DEFAULT_ADDRESS,
        address_format=GroupAddressType.LONG,
        telegram_received_cb=None,
        device_updated_cb=None,
        rate_limit=DEFAULT_RATE_LIMIT,
        multicast_group=DEFAULT_MCAST_GRP,
        multicast_port=DEFAULT_MCAST_PORT,
    ):
        """Initialize XKNX class."""
        # pylint: disable=too-many-arguments
        self.devices = Devices()
        self.telegrams = asyncio.Queue()
        self.loop = loop or asyncio.get_event_loop()
        self.sigint_received = asyncio.Event()
        self.telegram_queue = TelegramQueue(self)
        self.state_updater = StateUpdater(self)
        self.knxip_interface = None
        self.started = asyncio.Event()
        self.address_format = address_format
        self.own_address = PhysicalAddress(own_address)
        self.rate_limit = rate_limit
        self.multicast_group = multicast_group
        self.multicast_port = multicast_port
        self.logger = logging.getLogger("xknx.log")
        self.knx_logger = logging.getLogger("xknx.knx")
        self.telegram_logger = logging.getLogger("xknx.telegram")
        self.raw_socket_logger = logging.getLogger("xknx.raw_socket")
        self.connection_config = None
        self.version = VERSION

        if config is not None:
            Config(self).read(config)

        if telegram_received_cb is not None:
            self.telegram_queue.register_telegram_received_cb(
                telegram_received_cb)

        if device_updated_cb is not None:
            self.devices.register_device_updated_cb(device_updated_cb)
示例#14
0
文件: devices_test.py 项目: vosc/xknx
    def test_get_item(self):
        """Test get item by name or by index."""
        xknx = XKNX(loop=self.loop)
        devices = Devices()

        light1 = Light(xknx,
                       'Living-Room.Light_1',
                       group_address_switch='1/6/7')
        devices.add(light1)

        switch1 = Switch(xknx,
                         "TestOutlet_1",
                         group_address='1/2/3')
        devices.add(switch1)

        light2 = Light(xknx,
                       'Living-Room.Light_2',
                       group_address_switch='1/6/8')
        devices.add(light2)

        switch2 = Switch(xknx,
                         "TestOutlet_2",
                         group_address='1/2/4')
        devices.add(switch2)

        self.assertEqual(devices["Living-Room.Light_1"], light1)
        self.assertEqual(devices["TestOutlet_1"], switch1)
        self.assertEqual(devices["Living-Room.Light_2"], light2)
        self.assertEqual(devices["TestOutlet_2"], switch2)
        with self.assertRaises(KeyError):
            # pylint: disable=pointless-statement
            devices["TestOutlet_X"]

        self.assertEqual(devices[0], light1)
        self.assertEqual(devices[1], switch1)
        self.assertEqual(devices[2], light2)
        self.assertEqual(devices[3], switch2)
        with self.assertRaises(IndexError):
            # pylint: disable=pointless-statement
            devices[4]
示例#15
0
文件: devices_test.py 项目: XKNX/xknx
    def test_device_by_group_address(self):
        """Test get devices by group address."""
        xknx = XKNX()
        devices = Devices()

        light1 = Light(xknx, "Livingroom", group_address_switch="1/6/7")
        sensor1 = BinarySensor(xknx, "Diningroom", group_address_state="3/0/1")
        sensor2 = BinarySensor(xknx, "Diningroom", group_address_state="3/0/1")
        light2 = Light(xknx, "Livingroom", group_address_switch="1/6/8")
        devices.add(light1)
        devices.add(sensor1)
        devices.add(sensor2)
        devices.add(light2)

        assert tuple(devices.devices_by_group_address(
            GroupAddress("1/6/7"))) == (light1, )
        assert tuple(devices.devices_by_group_address(
            GroupAddress("1/6/8"))) == (light2, )
        assert tuple(devices.devices_by_group_address(
            GroupAddress("3/0/1"))) == (
                sensor1,
                sensor2,
            )