From ecab334d9cdc50a150dc126edcf5fde618abde2a Mon Sep 17 00:00:00 2001 From: "ulrich.huettinger@tridonic.com" Date: Mon, 23 Nov 2026 18:27:50 +0100 Subject: [PATCH] Allow commands to ask for EnableDeviceType(0) Device type 0 is fluorescent lamp control gear, IEC 62386-201, and its application extended commands must be preceded by EnableDeviceType(0) just like those of any other device type. Command.devicetype could not express that: 0 was the value meaning "send this command on its own", so EnableDeviceType was documented as unusable for device type 0, and part 201 could not be implemented at all. Use None for "no EnableDeviceType needed" instead, which leaves every value the standard allows for the command parameter, 0 to 254, free to mean the device type it names. The devicetype parameter of from_frame follows suit, so a frame that was not preceded by EnableDeviceType no longer decodes as one that was preceded by EnableDeviceType(0). Add dali.gear.fluorescent with the one application extended command part 201 defines, QueryExtendedVersionNumber; opcodes 224 to 254 are reserved there and control gear shall not react to them. Co-Authored-By: Claude Opus 5 --- CHANGES.rst | 12 ++++++++++++ dali/command.py | 11 +++++++---- dali/device/general.py | 18 +++++++++--------- dali/gear/__init__.py | 1 + dali/gear/fluorescent.py | 26 ++++++++++++++++++++++++++ dali/gear/general.py | 26 ++++++++++++++++---------- dali/tests/test_command.py | 29 +++++++++++++++-------------- 7 files changed, 86 insertions(+), 37 deletions(-) create mode 100644 dali/gear/fluorescent.py diff --git a/CHANGES.rst b/CHANGES.rst index 0b4e2211..3464e2d6 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -215,6 +215,18 @@ Detailed changes operate on the underling raw int; the others operate on the scaled value) +- ``Command.devicetype`` now uses ``None`` rather than ``0`` to mean + "no ``EnableDeviceType`` needed", which frees 0 to name the device + type it is, fluorescent lamp control gear. The ``devicetype`` + parameter of ``Command.from_frame()`` follows suit, so a frame that + was not preceded by ``EnableDeviceType`` no longer decodes as one + that was preceded by ``EnableDeviceType(0)``. Code that passes + ``devicetype=0`` to say "none" must pass ``None`` instead. + +- New module ``dali.gear.fluorescent`` with the commands from IEC + 62386-201, fluorescent lamp control gear. Part 201 defines a single + application extended command, ``QueryExtendedVersionNumber``. + .. _dali-drivers: https://github.com/sde1000/dali-drivers 0.16 (2026-10-20) diff --git a/dali/command.py b/dali/command.py index 39355724..20390ac0 100644 --- a/dali/command.py +++ b/dali/command.py @@ -269,8 +269,11 @@ class Command: # 16-bit frames may be interpreted differently if they are # preceded by the EnableDeviceType command. If a command needs # EnableDeviceType(foo) to be sent first, override devicetype to - # foo. This parameter is ignored for all other frame lengths. - devicetype = 0 + # foo; None means the command must not be preceded by one. Device + # type 0 is fluorescent lamp control gear, IEC 62386-201, so zero + # is a device type like any other. This parameter is ignored for + # all other frame lengths. + devicetype: Optional[int] = None _commands: set[type[Command]] = set() _supported_devicetypes: set[int] = set() @@ -279,7 +282,7 @@ class Command: def __init_subclass__(cls) -> None: if cls.__name__[0] != '_': cls._commands.add(cls) - if cls.devicetype != 0: + if cls.devicetype is not None: cls._supported_devicetypes.add(cls.devicetype) subs = cls._framesizes.setdefault(cls._framesize, list()) subs.append(cls) @@ -294,7 +297,7 @@ def __init__(self, f: frame.ForwardFrame) -> None: @classmethod def from_frame( - cls, f: frame.ForwardFrame, devicetype: int = 0, + cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[DITM] = None ) -> Optional[Command]: """Return a Command instance corresponding to the supplied frame. diff --git a/dali/device/general.py b/dali/device/general.py index eed8f4e0..077fb52c 100644 --- a/dali/device/general.py +++ b/dali/device/general.py @@ -66,7 +66,7 @@ def __init_subclass__(cls) -> None: cls._devicecommands.append(cls) @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None ) -> Optional[_DeviceCommand]: # In 24-bit frames, bit 16 is 1 for "commands" (as opposed to "events") @@ -103,7 +103,7 @@ class UnknownDeviceCommand(_DeviceCommand): """An unknown command addressed to a control device. """ @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None) -> None: return None @@ -167,7 +167,7 @@ def __init_subclass__(cls) -> None: cls._opcodes[cls._opcode] = cls @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None ) -> Optional[_DeviceCommand]: if f[16:8] != 0x1FE: @@ -561,7 +561,7 @@ def __init_subclass__(cls) -> None: cls._opcodes[cls._opcode] = cls @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None ) -> Optional[_DeviceCommand]: # In 24-bit frames, bit 16 is 1 for "commands" (as opposed to "events") @@ -803,7 +803,7 @@ def __init__(self) -> None: ) @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None ) -> Optional[_SpecialDeviceCommand]: if cls == _SpecialDeviceCommand: @@ -827,7 +827,7 @@ def __init__(self, param: int) -> None: super().__init__() @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None ) -> Optional[_SpecialDeviceCommandOneParam]: if cls == _SpecialDeviceCommandOneParam: @@ -855,7 +855,7 @@ def __init__(self, a: int, b: int) -> None: super().__init__() @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None ) -> Optional[_SpecialDeviceCommandTwoParam]: if cls == _SpecialDeviceCommandTwoParam: @@ -1212,7 +1212,7 @@ class could be the same for multiple data. @classmethod def from_frame( - cls, f: frame.ForwardFrame, devicetype: int = 0, + cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None ) -> Optional[_Event]: # In 24-bit frames, bit 16 is 0 for "events" @@ -1346,7 +1346,7 @@ def event_data(self) -> int: return self._unhandled_data @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None) -> None: return diff --git a/dali/gear/__init__.py b/dali/gear/__init__.py index 2c04b79a..b2459508 100644 --- a/dali/gear/__init__.py +++ b/dali/gear/__init__.py @@ -5,6 +5,7 @@ extensions and control gear specific features). """ import dali.gear.general +import dali.gear.fluorescent import dali.gear.emergency import dali.gear.incandescent import dali.gear.converter diff --git a/dali/gear/fluorescent.py b/dali/gear/fluorescent.py new file mode 100644 index 00000000..865ec0f4 --- /dev/null +++ b/dali/gear/fluorescent.py @@ -0,0 +1,26 @@ +"""Commands and responses from IEC 62386 part 201.""" + +from dali.gear.general import _StandardCommand, QueryExtendedVersionNumberMixin + + +class _FluorescentCommand(_StandardCommand): + devicetype = 0 + + +############################################################################### +# Commands from IEC 62386-201 section 11.3.1 +############################################################################### + + +class QueryExtendedVersionNumber(QueryExtendedVersionNumberMixin, + _FluorescentCommand): + """Query Extended Version Number + + Returns the version number of IEC 62386-201 implemented by the + control gear. + + This is the only application extended command defined by part 201: + everything fluorescent lamp control gear can be asked to do is in + part 102. Opcodes 224 to 254 are reserved and shall be ignored. + """ + pass diff --git a/dali/gear/general.py b/dali/gear/general.py index 3b961bb0..295e69c4 100644 --- a/dali/gear/general.py +++ b/dali/gear/general.py @@ -26,7 +26,7 @@ def __init_subclass__(cls) -> None: cls._gearcommands.append(cls) @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None ) -> Optional[_GearCommand]: for gc in cls._gearcommands: @@ -62,7 +62,7 @@ class UnknownGearCommand(_GearCommand): """An unknown command addressed to control gear. """ @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None) -> None: return None @@ -125,7 +125,8 @@ def __init__(self, destination: address.GearAddress, super().__init__(f) # (devicetype, opcode) -> commandclass - _opcodes: dict[tuple[int, int], type[_StandardCommand]] = {} + _opcodes: dict[tuple[Optional[int], int], + type[_StandardCommand]] = {} def __init_subclass__(cls) -> None: super().__init_subclass__() @@ -140,7 +141,7 @@ def __init_subclass__(cls) -> None: cls._opcodes[(cls.devicetype, cls._cmdval)] = cls @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None ) -> Optional[_GearCommand]: if not f[8]: @@ -212,7 +213,7 @@ def __init__(self, destination: address.GearAddress, super().__init__(f) @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None ) -> Optional[_GearCommand]: if f[8]: @@ -1052,7 +1053,11 @@ class QueryExtendedVersionNumber(QueryExtendedVersionNumberMixin, _StandardCommand): """Query Extended Version Number - For device type 0, this command is ignored. + Not preceded by an EnableDeviceType command, so control gear will + ignore it. This exists to decode such a frame when it is seen on + the bus, and to ask about a device type chosen at run time by + sending EnableDeviceType explicitly beforehand; to query a device + type known in advance, use its own implementation instead. """ pass @@ -1102,7 +1107,7 @@ def __init_subclass__(cls) -> None: cls._opcodes[cls._cmdval] = cls @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None ) -> Optional[_GearCommand]: opcode = f[15:8] @@ -1143,7 +1148,7 @@ def __init__(self, address: int | Literal["MASK"]) -> None: super().__init__((self.address << 1) | 1) @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None ) -> Optional[_ShortAddrSpecialCommand]: if cls == _ShortAddrSpecialCommand: @@ -1212,7 +1217,7 @@ def __init__(self, broadcast: bool = False, super().__init__(b) @classmethod - def from_frame(cls, f: frame.ForwardFrame, devicetype: int = 0, + def from_frame(cls, f: frame.ForwardFrame, devicetype: Optional[int] = None, dev_inst_map: Optional[command.DITM] = None ) -> Optional[_SpecialCommand]: if f[15:8] == cls._cmdval: @@ -1333,7 +1338,8 @@ class QueryShortAddress(_SpecialCommand): class EnableDeviceType(_SpecialCommand): """This command shall be sent before an application extended command. This command can be processed without the use of the Initialise() - command. This command shall not be used for device type 0. + command. The valid range of the parameter is 0 to 254; device type + 0 is fluorescent lamp control gear, described in IEC 62386-201. """ _cmdval = 0xc1 _hasparam = True diff --git a/dali/tests/test_command.py b/dali/tests/test_command.py index e471d379..3fb75c52 100644 --- a/dali/tests/test_command.py +++ b/dali/tests/test_command.py @@ -8,11 +8,12 @@ from dali.gear import general as generalgear -def _test_pattern() -> Iterable[tuple[int, int | Iterable[int], int]]: - # Control gear, device type zero +def _test_pattern() -> Iterable[ + tuple[int, int | Iterable[int], int | None]]: + # Control gear, no preceding EnableDeviceType for d in range(0, 0x10000): - yield 16, d, 0 - # Control gear, device type non-zero, application extended commands only + yield 16, d, None + # Control gear, application extended commands only for dt in command.Command._supported_devicetypes: for a in range(1, 0x100, 2): for b in range(0xe0, 0x100): @@ -20,24 +21,24 @@ def _test_pattern() -> Iterable[tuple[int, int | Iterable[int], int]]: # Control devices, device commands only for a in range(0, 0x100): for b in range(0, 0x100): - yield 24, (a, 0xfe, b), 0 + yield 24, (a, 0xfe, b), None # Control devices, instance commands addressed to broadcast device addr for d in range(0, 0x10000): - yield 24, 0xff0000 | d, 0 + yield 24, 0xff0000 | d, None # Control devices, special commands for c in range(0, 0x100): - yield 24, (0xc1, c, 0x00), 0 + yield 24, (0xc1, c, 0x00), None for c in (0xc5, 0xc7, 0xc9): - yield 24, (c, 0x00, 0x00), 0 + yield 24, (c, 0x00, 0x00), None # Control devices, event messages for push buttons (part 301) for e in (0, 1, 2, 5, 9, 11, 12, 14, 15): - yield 24, (0xc2, 0x04, e), 0 + yield 24, (0xc2, 0x04, e), None # Control devices, QueryEventFilter for o in (0x90, 0x91, 0x92): - yield 24, (0x03, 0x00, o), 0 + yield 24, (0x03, 0x00, o), None # Control devices, events for occupancy sensors (part 303) for e in range(16): - yield 24, (0x86, 0xFC, e), 0 + yield 24, (0x86, 0xFC, e), None class TrialEnum(enum.IntEnum): @@ -398,9 +399,9 @@ def test_response(self) -> None: def test_queryextendedversionnumber(self) -> None: """all gear types implement QueryExtendedVersionNumber""" # dali.gear.general.QueryExtendedVersionNumber is an oddity: - # it is only used when preceded by EnableDeviceType with - # devicetype != 0. Ensure that there is an implementation of - # this command for all supported device types. + # it is only used when preceded by EnableDeviceType. Ensure + # that there is an implementation of this command for all + # supported device types. qevn_frame = generalgear.QueryExtendedVersionNumber( address.Broadcast()).frame for devicetype in command.Command._supported_devicetypes: