Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix switch state representation for scenes that turn switches off #517

Merged
merged 3 commits into from
Aug 16, 2023

Conversation

tstabrawa
Copy link
Contributor

@tstabrawa tstabrawa commented Aug 16, 2023

Proposed change

This change fixes a bug whereby switch scenes that are configured to turn the switch off (i.e. with data_1 == 0x00) would be marked as having turned the switch on in MQTT, despite the switch actually turning off.

Additional information

Note: I wasn't sure if other device types that have on-off states (e.g. KeypadLinc, Outlet) behaved the same (as I don't have these devices), so this change only applies to the Switch device type. Let me know if you know which other devices work this way and if you'd like me to add changes for them to this PR.

Confirmed no new flake8 errors, pylint errors, or code coverage gaps. All tests passing. Also, confirmed new test (test_handle_group_cmd) fails prior to applying fix:

================================================================================ FAILURES ================================================================================
___________________________________________________________ Test_Base_Config.test_handle_group_cmd[17-0-False] ___________________________________________________________

self = <test_SwitchDev.Test_Base_Config object at 0x7f9370aa5390>, test_device = <insteon_mqtt.device.Switch.Switch object at 0x7f937055b910>, cmd1 = 17, entry_d1 = 0
expected = False

    @pytest.mark.parametrize("cmd1, entry_d1, expected", [
        (0x11, None, None),
        (0x11, 0xFF, True),
        (0x11, 0x00, False),
        (0x13, None, None),
        (0x13, 0xFF, False),
        (0x13, 0x00, False),
    ])
    def test_handle_group_cmd(self, test_device, cmd1, entry_d1, expected):
        with mock.patch.object(IM.Signal, 'emit'):
            # Build the msg to send to the handler
            to_addr = test_device.addr
            from_addr = IM.Address(0x04, 0x05, 0x06)
            flags = IM.message.Flags(IM.message.Flags.Type.ALL_LINK_CLEANUP,
                                     False)
            msg = IM.message.InpStandard(from_addr, to_addr, flags, cmd1, 0x01)
            # If db entry is requested, build and add the entry to the dev db
            if entry_d1 is not None:
                db_flags = IM.message.DbFlags(True, False, True)
                entry = IM.db.DeviceEntry(from_addr, 0x01, 0xFFFF, db_flags,
                                          bytes([entry_d1, 0x00, 0x00]))
                test_device.db.add_entry(entry)
            # send the message to the handler
            test_device.handle_group_cmd(from_addr, msg)
            # Test the responses received
            calls = IM.Signal.emit.call_args_list
            if expected is not None:
                print(calls)
>               assert calls[0][1]['is_on'] == expected
E               assert True == False

tests/device/test_SwitchDev.py:113: AssertionError
-------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------
[call(<insteon_mqtt.device.Switch.Switch object at 0x7f937055b910>, is_on=True, level=None, mode=<Mode.NORMAL: 'normal'>, button=1, reason='scene')]
======================================================================== short test summary info =========================================================================
FAILED tests/device/test_SwitchDev.py::Test_Base_Config::test_handle_group_cmd[17-0-False] - assert True == False
===================================================================== 1 failed, 689 passed in 10.74s =====================================================================

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.
  • Code documentation was added where necessary - N/A

If user exposed functionality or configuration variables are added/changed:

  • Documentation added/updated - N/A

2477S devices actually turn off when receiving ON commands on a group with data_1 == 0x00.
Also, represent data_1 as 'on_off' field in scenes.yaml.
@tstabrawa tstabrawa changed the title Off by group data Fix switch state representation for scenes that turn switches off Aug 16, 2023
@krkeegan
Copy link
Collaborator

Perfect, thank you for this.

@krkeegan krkeegan merged commit 7493a0e into TD22057:dev Aug 16, 2023
1 check passed
@tstabrawa tstabrawa deleted the off-by-group-data branch August 18, 2023 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants