Skip to content

Commit

Permalink
Refactor DateTime Device and DPT definitions (XKNX#1535)
Browse files Browse the repository at this point in the history
* DPTTime

* DPTDate

* DPTDateTime

* Refactor DateTime device to separate devices

* Fix tests, examples and docs

* move DateTimeDevice.value to property

* more tests
  • Loading branch information
farmio authored Jul 31, 2024
1 parent 68d23ea commit fbdcdee
Show file tree
Hide file tree
Showing 20 changed files with 947 additions and 523 deletions.
6 changes: 5 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,24 @@ nav_order: 2
- Refactor `ClimateMode` device
- Rename `ClimateMode` argument `group_address_operation_mode_night` to `group_address_operation_mode_economy`
- Remove DPT 3 special handling `stepwise_*` and `startstop_*` from Sensor device
- Remove `DateTime` device in favour of `DateDevice`, `TimeDevice` and `DateTimeDevice` using `datetime` objects instead of `time.struct_time`

### DPT

- DPTComplex: Common interface for DPT transcoders with multi-value data. Resulting dataclasses can be converted to and from a dict with DPT specific properties to be JSON compatible.
- Added or refactored complex DPTs and dataclasses:
- 3.007 - DPTControlDimming
- 3.008 - DPTControlBlinds
- 10.001 - DPTTime - KNXTime
- 11.001 - DPTDate - KNXDate
- 18.001 - DPTSceneControl
- 19.001 - DPTDateTime - KNXDateTime
- 232.600 - DPTColorRGB - RGBColor
- 235.001 - DPTTariffActiveEnergy - TariffActiveEnergy
- 242.600 - DPTColorXYY - XYYColor
- 251.600 - DPTColorRGBW - RGBWColor
- 20.60102 - DPTHVACStatus - HVACStatus (removed DPTControllerStatus in favour of this)
- DPTEnum: Common interface for DPT transcoders with enumueration values. Transcoders accept Enum, string or raw integer values for encoding.
- DPTEnum: Common interface for DPT trath enumueration values. Transcoders accept Enum, string or raw integer values for encoding.
- 1.007 - DPTStep
- 1.008 - DPTUpDown
- 1.100 - DPTHeatCool
Expand Down
26 changes: 12 additions & 14 deletions docs/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ nav_order: 10

## [](#header-2)Overview

XKNX provides the possibility to send the local time, date or both combined to the KNX bus in regular intervals. This can be used to display the time within [KNX touch sensors](https://katalog.gira.de/en/datenblatt.html?id=638294) or for KNX automation schemes programmed with the ETS.
XKNX provides the possibility to send the local time, date or both combined to the KNX bus in regular intervals with `TimeDevice`, `DateDevice` or `DateTimeDevice`.

## [](#header-2)Example

```python
time_device = DateTime(
time_device = TimeDevice(
xknx, 'TimeTest',
group_address='1/2/3',
broadcast_type='TIME',
localtime=True
)
xknx.devices.async_add(time_device)
Expand All @@ -29,24 +28,23 @@ await xknx.devices['TimeTest'].sync()
* `xknx` is the XKNX object.
* `name` is the name of the object.
* `group_address` is the KNX group address of the sensor device.
* `broadcast_type` defines the value type that will be sent to the KNX bus. Valid attributes are: 'time', 'date' and 'datetime'. Default: `time`
* `localtime` If set `True` sync() and GroupValueRead requests always return the current local time. On `False` the set value will be sent. Default: `True`
* `localtime` If set `True` sync() and GroupValueRead requests always return the current local time and it is also sent every 60 minutes. On `False` the set value will be sent. Default: `True`
* `device_updated_cb` Callback for each update.

## [](#header-2)Daemon mode
## [](#header-2)Local time

When XKNX is started in [daemon mode](/xknx), with START_STATE_UPDATER enabled, XKNX will automatically send the time to the KNX bus with the `sync_state`-loop.
When XKNX is started, a DateDevice, DateTimeDevice or TimeDevice will automatically send the time to the KNX bus every hour. This can be disabled by setting `localtime=False`.

```python
import asyncio
from xknx import XKNX
from xknx.devices import DateTime
from xknx.devices import DateTimeDevice

async def main():
async with XKNX(daemon_mode=True) as xknx:
time = DateTime(xknx, 'TimeTest', group_address='1/2/3')
xknx.devices.async_add(time)
print("Sending time to KNX bus every hour")
dt_device = DateTimeDevice(xknx, 'TimeTest', group_address='1/2/3')
xknx.devices.async_add(dt_device)
print("Sending datetime object to KNX bus every hour")

asyncio.run(main())
```
Expand All @@ -55,11 +53,11 @@ asyncio.run(main())

```python
from xknx import XKNX
from xknx.devices import DateTime
from xknx.devices import DateDevice

xknx = XKNX()
time_device = DateTime(xknx, 'TimeTest', group_address='1/2/3', broadcast_type='time')
xknx.devices.async_add(time_device)
date_device = DateDevice(xknx, 'TimeTest', group_address='1/2/3')
xknx.devices.async_add(date_device)

await xknx.start()
# Sending Time to KNX bus
Expand Down
8 changes: 3 additions & 5 deletions examples/example_datetime.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
"""Example for DateTime device."""
"""Example for Date and Time devices."""

import asyncio

from xknx import XKNX
from xknx.devices import DateTime
from xknx.devices import TimeDevice


async def main() -> None:
"""Connect to KNX/IP device and broadcast time."""
xknx = XKNX(daemon_mode=True)
xknx.devices.async_add(
DateTime(xknx, "TimeTest", group_address="1/2/3", broadcast_type="time")
)
xknx.devices.async_add(TimeDevice(xknx, "TimeTest", group_address="1/2/3"))
print("Sending time to KNX bus every hour")
await xknx.start()
await xknx.stop()
Expand Down
2 changes: 1 addition & 1 deletion examples/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
|Example|Description|
|-|-|
|[Climate Control](./example_climate.py)|Example for controlling climate device|
|[Datetime](./example_datetime.py)|Example for DateTime device|
|[Datetime](./example_datetime.py)|Example for Date and Time devices|
|[Devices](./example_devices.py)|Example for internal devices storage|
|[Dimm light](./example_light_dimm.py)|Example for switching a light on and off|
|[State of light](./example_light_state.py)|Example for reading the state from the KNX bus|
Expand Down
3 changes: 2 additions & 1 deletion requirements/testing.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
-r production.txt
freezegun==1.5.1
mypy==1.11.0
pre-commit==3.8.0
pylint==3.2.6
pytest==8.3.2
Expand All @@ -9,4 +11,3 @@ ruff==0.5.5
setuptools==72.0.0
tox==4.16.0
tox-gh-actions==3.2.0
mypy==1.11.0
Loading

0 comments on commit fbdcdee

Please sign in to comment.