diff --git a/insteon_mqtt/device/base/Base.py b/insteon_mqtt/device/base/Base.py index aa11013c..b3b49909 100644 --- a/insteon_mqtt/device/base/Base.py +++ b/insteon_mqtt/device/base/Base.py @@ -588,6 +588,10 @@ def addRefreshData(self, seq, force=False): if self.db.desc is None or self.db.firmware is None or force: seq.add(self.get_model) + # If the engine is not known, or force true, run get_engine + if self.db.engine is None or force: + seq.add(self.get_engine) + #----------------------------------------------------------------------- def get_flags(self, on_done=None): """Get the Insteon operational flags field from the device. @@ -649,6 +653,23 @@ def get_model(self, on_done=None): on_done) self.send(msg, msg_handler) + def _set_operating_flag_msg(self, cmd2): + """Creates a set operating flags message + + This will create the appropriate message based on the device's + engine (extended for i2cs, else standard) + + Args: + cmd2 (int): the set operating flags option (i.e. 0x04 to enable + resume dim) + """ + cmd1 = Msg.CmdType.SET_OPERATING_FLAGS + if self.db.engine is not None and self.db.engine < 2: + return Msg.OutStandard.direct(self.addr, cmd1, cmd2) + else: + return Msg.OutExtended.direct(self.addr, cmd1, cmd2, + bytes([0x00] * 14), crc_type='D14') + #----------------------------------------------------------------------- def sync(self, dry_run=True, refresh=True, sequence=None, on_done=None): """Syncs the links on the device. diff --git a/insteon_mqtt/device/base/DimmerBase.py b/insteon_mqtt/device/base/DimmerBase.py index bd31c137..d256ef9f 100644 --- a/insteon_mqtt/device/base/DimmerBase.py +++ b/insteon_mqtt/device/base/DimmerBase.py @@ -167,14 +167,10 @@ def set_resume_dim(self, on_done=None, **kwargs): # 0x05 - Disables resume dim if resume_dim: LOG.info("Device %s enabling resume dim", self.label) - cmd2 = 0x04 + msg = self._set_operating_flag_msg(0x04) else: LOG.info("Device %s disabling resume dim", self.label) - cmd2 = 0x05 - - cmd1 = Msg.CmdType.SET_OPERATING_FLAGS - msg = Msg.OutExtended.direct(self.addr, cmd1, cmd2, - bytes([0x00] * 14)) + msg = self._set_operating_flag_msg(0x05) # Use the standard command handler which will notify us when the # command is ACK'ed. diff --git a/tests/device/test_DimmerDev.py b/tests/device/test_DimmerDev.py index c420c11c..cc732a1f 100644 --- a/tests/device/test_DimmerDev.py +++ b/tests/device/test_DimmerDev.py @@ -73,11 +73,34 @@ def test_handle_on_off_manual(self, test_device, group, cmd1, cmd2, expected): else: mocked.assert_not_called() - def test_set_resume_dim(self, test_device): + def test_set_resume_dim_non_i2cs(self, test_device): # set_resume_dim(self, resume_dim, on_done=None) for params in ([True, 0x04], [False, 0x05]): test_device.set_resume_dim(resume_dim=params[0]) assert len(test_device.protocol.sent) == 1 + assert isinstance(test_device.protocol.sent[0].msg, Msg.OutStandard) + assert test_device.protocol.sent[0].msg.cmd1 == 0x20 + assert test_device.protocol.sent[0].msg.cmd2 == params[1] + test_device.protocol.clear() + + def test_set_resume_dim_unknown(self, test_device): + # set_resume_dim(self, resume_dim, on_done=None) + test_device.db.engine = None + for params in ([True, 0x04], [False, 0x05]): + test_device.set_resume_dim(resume_dim=params[0]) + assert len(test_device.protocol.sent) == 1 + assert isinstance(test_device.protocol.sent[0].msg, Msg.OutStandard) + assert test_device.protocol.sent[0].msg.cmd1 == 0x20 + assert test_device.protocol.sent[0].msg.cmd2 == params[1] + test_device.protocol.clear() + + def test_set_resume_dim_i2cs(self, test_device): + # set_resume_dim(self, resume_dim, on_done=None) + test_device.db.engine = 2 + for params in ([True, 0x04], [False, 0x05]): + test_device.set_resume_dim(resume_dim=params[0]) + assert len(test_device.protocol.sent) == 1 + assert isinstance(test_device.protocol.sent[0].msg, Msg.OutExtended) assert test_device.protocol.sent[0].msg.cmd1 == 0x20 assert test_device.protocol.sent[0].msg.cmd2 == params[1] assert test_device.protocol.sent[0].msg.data == bytes([0x00] * 14)