Example #1
0
 def test_to_knx(self):
     """Testing date time object."""
     xknx = XKNX()
     rv_datetime = RemoteValueDateTime(xknx, value_type="datetime")
     array = rv_datetime.to_knx(
         time.strptime("2017-11-28 23:7:24", "%Y-%m-%d %H:%M:%S"))
     assert array.value == (0x75, 0x0B, 0x1C, 0x57, 0x07, 0x18, 0x20, 0x80)
Example #2
0
 def test_payload_invalid(self):
     """Testing KNX/Byte representation of DPTDateTime object."""
     xknx = XKNX()
     rv = RemoteValueDateTime(xknx, value_type="datetime")
     self.assertFalse(
         rv.payload_valid(
             DPTArray((0x0B, 0x1C, 0x57, 0x07, 0x18, 0x20, 0x80))))
Example #3
0
 def test_from_knx(self):
     """Test parsing of RV with datetime object."""
     xknx = XKNX()
     rv_datetime = RemoteValueDateTime(xknx, value_type="datetime")
     assert rv_datetime.from_knx(
         DPTArray((0x75, 0x0B, 0x1C, 0x17, 0x07, 0x18, 0x20, 0x80))
     ) == time.strptime("2017-11-28 23:7:24", "%Y-%m-%d %H:%M:%S")
Example #4
0
 def test_payload_invalid(self):
     """Testing KNX/Byte representation of DPTDateTime object."""
     xknx = XKNX()
     rv_datetime = RemoteValueDateTime(xknx, value_type="datetime")
     with pytest.raises(CouldNotParseTelegram):
         rv_datetime.payload_valid(
             DPTArray((0x0B, 0x1C, 0x57, 0x07, 0x18, 0x20, 0x80))
         )
Example #5
0
 def __init__(self,
              xknx,
              name,
              broadcast_type='TIME',
              localtime=True,
              group_address=None,
              device_updated_cb=None):
     """Initialize DateTime class."""
     super().__init__(xknx, name, device_updated_cb)
     self.localtime = localtime
     self._broadcast_type = broadcast_type.upper()
     self._remote_value = RemoteValueDateTime(
         xknx,
         group_address=group_address,
         sync_state=False,
         value_type=broadcast_type,
         device_name=name,
         after_update_cb=self.after_update)
Example #6
0
    def __init__(
        self,
        xknx,
        name,
        group_address_datetime=None,
        group_address_date=None,
        group_address_time=None,
        device_updated_cb=None,
        # datetime_updated_cb=None,
        date_updated_cb=None,
        time_updated_cb=None,
    ):
        """Initialize System class."""
        # pylint: disable=too-many-arguments
        super().__init__(xknx, name, device_updated_cb)

        # self.datetime = RemoteValueDateTime(
        #     xknx,
        #     group_address_datetime,
        #     device_name=self.name,
        #     after_update_cb=datetime_updated_cb,
        #     datetime_type=DateTimeType.DATETIME,
        # )

        self.date = RemoteValueDateTime(
            xknx,
            group_address_date,
            device_name=self.name,
            after_update_cb=date_updated_cb,
            datetime_type=DateTimeType.DATE,
        )

        self.time = RemoteValueDateTime(
            xknx,
            group_address_time,
            device_name=self.name,
            after_update_cb=time_updated_cb,
            datetime_type=DateTimeType.TIME,
        )
Example #7
0
 def __init__(
     self,
     xknx,
     name: str,
     broadcast_type: str = "TIME",
     localtime: bool = True,
     group_address=None,
     device_updated_cb=None,
 ):
     """Initialize DateTime class."""
     super().__init__(xknx, name, device_updated_cb)
     self.localtime = localtime
     self._broadcast_type = broadcast_type.upper()
     self._remote_value = RemoteValueDateTime(
         xknx,
         group_address=group_address,
         sync_state=False,
         value_type=broadcast_type,
         device_name=name,
         after_update_cb=self.after_update,
     )
     self._broadcast_task = self._create_broadcast_task(minutes=60)
Example #8
0
 def test_init_raises_keyerror(self):
     """Test init raises KeyError if not supported."""
     xknx = XKNX()
     with pytest.raises(ConversionError):
         RemoteValueDateTime(xknx, value_type="trees_are_important")
Example #9
0
class System(Device):
    """Class for managing the system."""

    # pylint: disable=too-many-instance-attributes
    # pylint: disable=too-many-public-methods

    def __init__(
        self,
        xknx,
        name,
        group_address_datetime=None,
        group_address_date=None,
        group_address_time=None,
        device_updated_cb=None,
        # datetime_updated_cb=None,
        date_updated_cb=None,
        time_updated_cb=None,
    ):
        """Initialize System class."""
        # pylint: disable=too-many-arguments
        super().__init__(xknx, name, device_updated_cb)

        # self.datetime = RemoteValueDateTime(
        #     xknx,
        #     group_address_datetime,
        #     device_name=self.name,
        #     after_update_cb=datetime_updated_cb,
        #     datetime_type=DateTimeType.DATETIME,
        # )

        self.date = RemoteValueDateTime(
            xknx,
            group_address_date,
            device_name=self.name,
            after_update_cb=date_updated_cb,
            datetime_type=DateTimeType.DATE,
        )

        self.time = RemoteValueDateTime(
            xknx,
            group_address_time,
            device_name=self.name,
            after_update_cb=time_updated_cb,
            datetime_type=DateTimeType.TIME,
        )

    def has_group_address(self, group_address):
        """Test if device has given group address."""
        return (
            # self.datetime.has_group_address(group_address)
            self.date.has_group_address(group_address)
            or self.time.has_group_address(group_address)  # noqa W503
        )

    def __str__(self):
        """Return object as readable string."""
        return f"""<System name={self.name}, date={self.date.group_addr_str()}, time={self.time.group_addr_str()}>"""

    def state_addresses(self):
        """Return group addresses which should be requested to sync state."""
        state_addresses = []
        # state_addresses.extend(self.speed.state_addresses())
        return state_addresses

    async def process_group_write(self, telegram):
        """Process incoming GROUP WRITE telegram."""
        # await self.datetime.process(telegram)
        await self.date.process(telegram)
        await self.time.process(telegram)

    def __eq__(self, other):
        """Equal operator."""
        return self.__dict__ == other.__dict__
Example #10
0
class DateTime(Device):
    """Class for virtual date/time device."""

    # pylint: disable=too-many-arguments
    def __init__(
        self,
        xknx,
        name: str,
        broadcast_type: str = "TIME",
        localtime: bool = True,
        group_address=None,
        device_updated_cb=None,
    ):
        """Initialize DateTime class."""
        super().__init__(xknx, name, device_updated_cb)
        self.localtime = localtime
        self._broadcast_type = broadcast_type.upper()
        self._remote_value = RemoteValueDateTime(
            xknx,
            group_address=group_address,
            sync_state=False,
            value_type=broadcast_type,
            device_name=name,
            after_update_cb=self.after_update,
        )
        self._broadcast_task = self._create_broadcast_task(minutes=60)

    def __del__(self):
        """Destructor. Cleaning up if this was not done before."""
        if self._broadcast_task:
            self._broadcast_task.cancel()

    def _iter_remote_values(self):
        """Iterate the devices RemoteValue classes."""
        yield self._remote_value

    @classmethod
    def from_config(cls, xknx, name, config):
        """Initialize object from configuration structure."""
        broadcast_type = config.get("broadcast_type", "time").upper()
        group_address = config.get("group_address")
        return cls(xknx,
                   name,
                   broadcast_type=broadcast_type,
                   group_address=group_address)

    def _create_broadcast_task(self, minutes: int = 60):
        """Create an asyncio.Task for broadcasting local time periodically if `localtime` is set."""
        async def broadcast_loop(self, minutes: int):
            """Endless loop for broadcasting local time."""
            while True:
                await self.broadcast_localtime()
                await asyncio.sleep(minutes * 60)

        if self.localtime:
            return self.xknx.loop.create_task(
                broadcast_loop(self, minutes=minutes))
        return None

    async def broadcast_localtime(self, response=False):
        """Broadcast the local time to KNX bus."""
        await self._remote_value.set(time.localtime(), response=response)

    async def set(self, struct_time: time.struct_time):
        """Set time and send to KNX bus."""
        await self._remote_value.set(struct_time)

    async def process_group_read(self, telegram):
        """Process incoming GROUP READ telegram."""
        if self.localtime:
            await self.broadcast_localtime(True)
        else:
            await self._remote_value.respond()

    async def sync(self, wait_for_result=False):
        """Read state of device from KNX bus. Used here to broadcast time to KNX bus."""
        if self.localtime:
            await self.broadcast_localtime(response=False)

    def __str__(self):
        """Return object as readable string."""
        return '<DateTime name="{}" group_address="{}" broadcast_type="{}" />'.format(
            self.name, self._remote_value.group_addr_str(),
            self._broadcast_type)
Example #11
0
class DateTime(Device):
    """Class for virtual date/time device."""
    def __init__(
        self,
        xknx: XKNX,
        name: str,
        broadcast_type: str = "TIME",
        localtime: bool = True,
        group_address: GroupAddressesType | None = None,
        device_updated_cb: DeviceCallbackType | None = None,
    ):
        """Initialize DateTime class."""
        super().__init__(xknx, name, device_updated_cb)
        self.localtime = localtime
        self._broadcast_type = broadcast_type.upper()
        self._remote_value = RemoteValueDateTime(
            xknx,
            group_address=group_address,
            sync_state=False,
            value_type=broadcast_type,
            device_name=name,
            after_update_cb=self.after_update,
        )
        self._broadcast_task = self._create_broadcast_task(minutes=60)

    def __del__(self) -> None:
        """Destructor. Cleaning up if this was not done before."""
        if self._broadcast_task:
            try:
                self._broadcast_task.cancel()
            except RuntimeError:
                pass
        super().__del__()

    def _iter_remote_values(self) -> Iterator[RemoteValueDateTime]:
        """Iterate the devices RemoteValue classes."""
        yield self._remote_value

    def _create_broadcast_task(self,
                               minutes: int = 60) -> asyncio.Task[None] | None:
        """Create an asyncio.Task for broadcasting local time periodically if `localtime` is set."""
        async def broadcast_loop(self: "DateTime", minutes: int) -> None:
            """Endless loop for broadcasting local time."""
            while True:
                await self.broadcast_localtime()
                await asyncio.sleep(minutes * 60)

        if self.localtime:
            loop = asyncio.get_event_loop()
            return loop.create_task(broadcast_loop(self, minutes=minutes))
        return None

    async def broadcast_localtime(self, response: bool = False) -> None:
        """Broadcast the local time to KNX bus."""
        await self._remote_value.set(time.localtime(), response=response)

    async def set(self, struct_time: time.struct_time) -> None:
        """Set time and send to KNX bus."""
        await self._remote_value.set(struct_time)

    async def process_group_read(self, telegram: "Telegram") -> None:
        """Process incoming GROUP READ telegram."""
        if self.localtime:
            await self.broadcast_localtime(True)
        else:
            await self._remote_value.respond()

    async def sync(self, wait_for_result: bool = False) -> None:
        """Read state of device from KNX bus. Used here to broadcast time to KNX bus."""
        if self.localtime:
            await self.broadcast_localtime(response=False)

    def __str__(self) -> str:
        """Return object as readable string."""
        return '<DateTime name="{}" _remote_value={} broadcast_type="{}" />'.format(
            self.name, self._remote_value.group_addr_str(),
            self._broadcast_type)
Example #12
0
class DateTime(Device):
    """Class for virtual date/time device."""

    # pylint: disable=too-many-arguments
    def __init__(self,
                 xknx,
                 name,
                 broadcast_type='TIME',
                 localtime=True,
                 group_address=None,
                 device_updated_cb=None):
        """Initialize DateTime class."""
        super().__init__(xknx, name, device_updated_cb)
        self.localtime = localtime
        self._broadcast_type = broadcast_type.upper()
        self._remote_value = RemoteValueDateTime(
            xknx,
            group_address=group_address,
            sync_state=False,
            value_type=broadcast_type,
            device_name=name,
            after_update_cb=self.after_update)

    def _iter_remote_values(self):
        """Iterate the devices RemoteValue classes."""
        yield self._remote_value

    @classmethod
    def from_config(cls, xknx, name, config):
        """Initialize object from configuration structure."""
        broadcast_type = config.get('broadcast_type', 'time').upper()
        group_address = config.get('group_address')
        return cls(xknx,
                   name,
                   broadcast_type=broadcast_type,
                   group_address=group_address)

    async def broadcast_localtime(self, response=False):
        """Broadcast the local time to KNX bus."""
        await self._remote_value.set(time.localtime(), response=response)

    async def set(self, struct_time: time.struct_time):
        """Set time and send to KNX bus."""
        await self._remote_value.set(struct_time)

    async def process_group_read(self, telegram):
        """Process incoming GROUP READ telegram."""
        if self.localtime:
            await self.broadcast_localtime(True)
        else:
            await self._remote_value.send(response=True)

    async def sync(self):
        """Read state of device from KNX bus. Used here to broadcast time to KNX bus."""
        if self.localtime:
            await self.broadcast_localtime(response=False)

    def __str__(self):
        """Return object as readable string."""
        return '<DateTime name="{0}" group_address="{1}" broadcast_type="{2}" />' \
            .format(self.name,
                    self._remote_value.group_addr_str(),
                    self._broadcast_type)

    def __eq__(self, other):
        """Equal operator."""
        return self.__dict__ == other.__dict__