示例#1
0
async def test_get_read_only_properties(hass, hass_ws_client, iolinc_properties_data):
    """Test getting an Insteon device's properties."""
    mock_read_only = ExtendedProperty(
        "44.44.44", "mock_read_only", bool, is_read_only=True
    )
    mock_read_only.load(False)

    ws_client, devices = await _setup(
        hass, hass_ws_client, "44.44.44", iolinc_properties_data
    )
    device = devices["44.44.44"]
    device.configuration["mock_read_only"] = mock_read_only
    with patch.object(insteon.api.properties, "devices", devices):
        await ws_client.send_json(
            {
                ID: 2,
                TYPE: "insteon/properties/get",
                DEVICE_ADDRESS: "44.44.44",
                SHOW_ADVANCED: False,
            }
        )
        msg = await ws_client.receive_json()
        assert msg["success"]
        assert len(msg["result"]["properties"]) == 6
        await ws_client.send_json(
            {
                ID: 3,
                TYPE: "insteon/properties/get",
                DEVICE_ADDRESS: "44.44.44",
                SHOW_ADVANCED: True,
            }
        )
        msg = await ws_client.receive_json()
        assert msg["success"]
        assert len(msg["result"]["properties"]) == 15
示例#2
0
    def test_value(self):
        """Test the ramp rate value."""
        address = random_address()
        ramp_rate_prop = ExtendedProperty(address, "ramp_rate", RampRate,
                                          False, False, PropertyType.ADVANCED)
        test_prop = RampRateProperty(address, "test_prop", ramp_rate_prop)

        assert ramp_rate_prop.value is None
        test_prop.value is None
        ramp_rate_prop.load(RampRate.MIN_4)
        assert test_prop.value == ramp_rate_to_seconds(RampRate.MIN_4)
示例#3
0
    def test_is_loaded(self):
        """Test the is_dirty property."""

        address = random_address()
        ramp_rate_prop = ExtendedProperty(address, "ramp_rate", RampRate,
                                          False, False, PropertyType.ADVANCED)
        test_prop = RampRateProperty(address, "test_prop", ramp_rate_prop)

        assert not ramp_rate_prop.is_loaded
        assert not test_prop.is_loaded

        ramp_rate_prop.load(RampRate.MIN_1)
        assert ramp_rate_prop.is_loaded
        assert test_prop.is_loaded
示例#4
0
    def test_is_dirty(self):
        """Test the is_dirty property."""

        address = random_address()
        ramp_rate_prop = ExtendedProperty(address, "ramp_rate", RampRate,
                                          False, False, PropertyType.ADVANCED)
        test_prop = RampRateProperty(address, "test_prop", ramp_rate_prop)

        assert not ramp_rate_prop.is_dirty
        assert not test_prop.is_dirty
        ramp_rate_prop.load(0)
        assert not ramp_rate_prop.is_dirty
        assert not test_prop.is_dirty
        ramp_rate_prop.new_value = RampRate.MIN_1
        assert ramp_rate_prop.is_dirty
        assert test_prop.is_dirty
        ramp_rate_prop.new_value = None
        assert not ramp_rate_prop.is_dirty
        assert not test_prop.is_dirty
        ramp_rate_prop.new_value = RampRate.MIN_1_5
        assert ramp_rate_prop.is_dirty
        assert test_prop.is_dirty
        ramp_rate_prop.new_value = ramp_rate_prop.value
        assert not ramp_rate_prop.is_dirty
        assert not test_prop.is_dirty
示例#5
0
    def test_read_only_flag(self):
        """Test an int read only flag."""
        ex_prop = ExtendedProperty(random_address(), "ext_prop", int, False,
                                   True)
        op_flag = OperatingFlag(random_address(), "op_flag", int, False, True)

        for flag in [ex_prop, op_flag]:
            orig = randint(0, 100)
            mod = randint(101, 255)

            assert not flag.is_loaded
            flag.load(orig)
            assert flag.is_loaded

            assert flag.value == orig
            assert not flag.is_dirty

            flag.new_value = mod
            assert flag.value == orig
            assert flag.new_value is None
            assert not flag.is_dirty

            flag.new_value = None
            assert not flag.is_dirty

            assert not flag.is_reversed
            assert flag.is_read_only
示例#6
0
async def test_get_unknown_properties(hass, hass_ws_client,
                                      iolinc_properties_data):
    """Test getting an Insteon device's properties."""
    class UnknownType:
        """Mock unknown data type."""

    mock_unknown = ExtendedProperty("44.44.44", "mock_unknown", UnknownType)

    ws_client, devices = await _setup(hass, hass_ws_client, "44.44.44",
                                      iolinc_properties_data)
    device = devices["44.44.44"]
    device.configuration["mock_unknown"] = mock_unknown
    with patch.object(insteon.api.properties, "devices", devices):
        await ws_client.send_json({
            ID: 2,
            TYPE: "insteon/properties/get",
            DEVICE_ADDRESS: "44.44.44",
            SHOW_ADVANCED: False,
        })
        msg = await ws_client.receive_json()
        assert msg["success"]
        assert len(msg["result"]["properties"]) == 6
        await ws_client.send_json({
            ID: 3,
            TYPE: "insteon/properties/get",
            DEVICE_ADDRESS: "44.44.44",
            SHOW_ADVANCED: True,
        })
        msg = await ws_client.receive_json()
        assert msg["success"]
        assert len(msg["result"]["properties"]) == 14
示例#7
0
    def test_new_value(self):
        """Test the new_value property."""

        address = random_address()
        ramp_rate_prop = ExtendedProperty(address, "ramp_rate", RampRate,
                                          False, False, PropertyType.ADVANCED)
        test_prop = RampRateProperty(address, "test_prop", ramp_rate_prop)
        assert ramp_rate_prop.new_value is None
        assert test_prop.new_value is None

        # Test all ramp rates except 0 and 2
        for value in range(1, 0x1F):
            if value == 2:  # Special condition
                continue
            ramp_rate = RampRate(value)
            seconds = ramp_rate_to_seconds(ramp_rate)
            test_prop.new_value = seconds
            assert ramp_rate_prop.new_value == ramp_rate
            assert test_prop.new_value == seconds

        # Test 0 and 2
        for value in [0, 2]:
            seconds = ramp_rate_to_seconds(value)
            test_prop.new_value = seconds
            assert ramp_rate_prop.new_value == seconds_to_ramp_rate(seconds)

        # Test clearing the changes
        test_prop.new_value = None
        assert ramp_rate_prop.new_value is None

        test_prop.new_value = 3
        ramp_rate_prop.new_value = None
        assert test_prop.new_value is None

        # Test putting new_value to current value
        seconds = ramp_rate_to_seconds(RampRate.SEC_0_3)
        ramp_rate_prop.load(RampRate.SEC_0_3)
        assert test_prop.value == seconds
        seconds2 = seconds + 2
        ramp_rate2 = seconds_to_ramp_rate(seconds2)
        test_prop.new_value = seconds2
        assert ramp_rate_prop.new_value == ramp_rate2
        assert test_prop.new_value == ramp_rate_to_seconds(ramp_rate2)
        test_prop.new_value = test_prop.value
        assert test_prop.new_value is None
        assert ramp_rate_prop.new_value is None
    def test_load(self):
        """Test the is_loaded property."""
        address = random_address()
        delay_prop = ExtendedProperty(address, "delay", int, False, False,
                                      PropertyType.ADVANCED)
        prescaler_prop = ExtendedProperty(address, "delay", int, False, False,
                                          PropertyType.ADVANCED)

        test_prop = MomentaryDelayProperty(address, "test_prop", delay_prop,
                                           prescaler_prop)
        assert not test_prop.is_loaded
        delay_prop.load(0)
        assert not test_prop.is_loaded
        prescaler_prop.load(0)
        assert test_prop.is_loaded
示例#9
0
    def test_class_properties(self):
        """Test the ExtendedProperty class properties."""
        address = random_address()
        name = "prpp_name"
        value_type = int
        is_reversed = bool(random.randint(0, 1))
        is_read_only = bool(random.randint(0, 1))
        prop_type = PropertyType(random.randint(0, 2))
        prop = ExtendedProperty(address, name, value_type, is_reversed,
                                is_read_only, prop_type)

        assert prop.topic == f"{address.id}.property.{name}"
        assert prop.name == name
        assert prop.value_type == value_type
        assert prop.is_reversed == is_reversed
        assert prop.is_read_only == is_read_only
        assert prop.property_type == prop_type

        prop2 = ExtendedProperty(address, name, value_type, not is_reversed,
                                 not is_read_only, prop_type)

        assert prop2.is_reversed != is_reversed
        assert prop2.is_read_only != is_read_only
示例#10
0
    def test_value(self):
        """Test the momentary delay value."""
        address = random_address()
        delay_prop = ExtendedProperty(address, "delay", int, False, False,
                                      PropertyType.ADVANCED)
        prescaler_prop = ExtendedProperty(address, "delay", int, False, False,
                                          PropertyType.ADVANCED)

        test_prop = MomentaryDelayProperty(address, "test_prop", delay_prop,
                                           prescaler_prop)

        delay_value = randint(0, 255)
        prescaler_value = randint(0, 255)
        delay_prop.load(delay_value)
        prescaler_prop.load(prescaler_value)
        seconds = delay_value * max(1, prescaler_value) / 10

        assert test_prop.value == seconds
示例#11
0
    def test_value(self):
        """Test the toggle button value."""
        address = random_address()

        for button in range(2, 8):
            non_toggle_prop = ExtendedProperty(address, "non_toggle", int,
                                               False, False,
                                               PropertyType.ADVANCED)
            on_only_prop = ExtendedProperty(address, "on_only", int, False,
                                            False, PropertyType.ADVANCED)
            test_prop = ToggleButtonProperty(
                address,
                "test_prop",
                button,
                non_toggle_prop,
                on_only_prop,
            )
            assert test_prop.value is None

            for toggle_mode, test_values in test_toggle_mode_to_prop.items():
                non_toggle_prop.load(set_bit(0, button - 1, test_values[0]))
                on_only_prop.load(set_bit(0, button - 1, test_values[1]))
                assert test_prop.value == toggle_mode
示例#12
0
    def test_is_dirty(self):
        """Test is_dirty property."""
        ex_prop = ExtendedProperty(random_address(), "ext_prop", int, False,
                                   False)
        op_flag = OperatingFlag(random_address(), "op_flag", int, False, False)

        for flag in [ex_prop, op_flag]:
            # load(orig) -> not is_dirty
            # new_value = mod -> is_dirty
            # new_value = orig => not is_dirty
            # new_value = None => not is_dirty

            orig = randint(0, 100)
            mod = randint(101, 255)

            assert not flag.is_loaded
            flag.load(orig)
            assert flag.is_loaded

            assert flag.value == orig
            assert not flag.is_dirty

            flag.new_value = mod
            assert flag.value == orig
            assert flag.new_value == mod
            assert flag.is_dirty

            flag.new_value = orig
            assert flag.value == orig
            assert flag.new_value is None
            assert not flag.is_dirty

            flag.new_value = mod
            assert flag.is_dirty
            flag.new_value = None
            assert flag.value == orig
            assert flag.new_value is None
            assert not flag.is_dirty
示例#13
0
    def test_reverse_bool_flag(self):
        """Test a reversed bool writable flag."""
        ex_prop = ExtendedProperty(random_address(), "ext_prop", bool, True,
                                   False)
        op_flag = OperatingFlag(random_address(), "op_flag", bool, True, False)

        for flag in [ex_prop, op_flag]:
            assert not flag.is_loaded
            for orig in [True, False]:
                reverse = not orig
                flag.load(orig)
                assert flag.is_loaded

                assert flag.value == reverse
                assert not flag.is_dirty

                flag.new_value = reverse
                assert flag.value == reverse
                assert flag.new_value == orig
                assert flag.is_dirty

                assert flag.is_reversed
                assert not flag.is_read_only
示例#14
0
    def test_load_method(self):
        """Test the load method."""
        prop = ExtendedProperty(random_address(), "prop_name", int, False,
                                False)

        value = random.randint(0, 255)
        assert not prop.is_loaded
        prop.load(value)
        assert prop.is_loaded
        assert prop.value == value
        assert not prop.is_dirty
        assert prop.new_value is None

        new_value = random.randint(0, 255)
        prop.new_value = new_value
        assert prop.new_value == new_value
        assert prop.is_dirty

        value2 = random.randint(0, 255)
        prop.load(value2)
        assert prop.is_loaded
        assert prop.value == value2
        assert not prop.is_dirty
        assert prop.new_value is None
示例#15
0
    def test_new_value(self):
        """Test the new_value property."""
        address = random_address()
        non_toggle_prop = ExtendedProperty(address, "non_toggle", int, False,
                                           False, PropertyType.ADVANCED)
        on_only_prop = ExtendedProperty(address, "on_only", int, False, False,
                                        PropertyType.ADVANCED)

        for button in range(2, 8):
            test_prop = ToggleButtonProperty(
                address,
                "test_prop",
                button,
                non_toggle_prop,
                on_only_prop,
            )

            # Test the new_value is calculated correctly from the underlying properties
            for toggle_mode, test_values in test_toggle_mode_to_prop.items():
                non_toggle_prop.load(set_bit(0, button - 1,
                                             not test_values[0]))
                on_only_prop.load(set_bit(0, button - 1, not test_values[1]))
                assert test_prop.new_value is None
                non_toggle_prop.new_value = set_bit(0, button - 1,
                                                    test_values[0])
                on_only_prop.new_value = set_bit(0, button - 1, test_values[1])
                assert test_prop.new_value == toggle_mode

            # Test the underlying properties are set correctly
            for toggle_mode, test_values in test_toggle_mode_to_prop.items():
                non_toggle_prop.load(set_bit(0, button - 1,
                                             not test_values[0]))
                on_only_prop.load(set_bit(0, button - 1, not test_values[1]))
                assert test_prop.new_value is None
                test_prop.new_value = toggle_mode
                assert non_toggle_prop.new_value == set_bit(
                    0, button - 1, test_values[0])
                assert on_only_prop.new_value == set_bit(
                    0, button - 1, test_values[1])
                test_prop.new_value = None
                assert non_toggle_prop.new_value is None
                assert on_only_prop.new_value is None

                test_prop.new_value = toggle_mode
                assert non_toggle_prop.new_value == set_bit(
                    0, button - 1, test_values[0])
                assert on_only_prop.new_value == set_bit(
                    0, button - 1, test_values[1])
                test_prop.new_value = test_prop.value
                assert non_toggle_prop.new_value is None
                assert on_only_prop.new_value is None
示例#16
0
    def test_value(self):
        """Test the momentary delay value."""
        address = random_address()

        momentary_mode_on_prop = ExtendedProperty(address, "mm_on", bool,
                                                  False, False,
                                                  PropertyType.ADVANCED)
        momentary_follow_sense_prop = ExtendedProperty(address, "m_follow",
                                                       bool, False, False,
                                                       PropertyType.ADVANCED)
        momentary_on_off_trigger_prop = ExtendedProperty(
            address, "m_on_off", bool, False, False, PropertyType.ADVANCED)

        test_prop = RelayModeProperty(
            address,
            "test_prop",
            momentary_mode_on_prop,
            momentary_follow_sense_prop,
            momentary_on_off_trigger_prop,
        )

        for test_value in test_prop_to_relay_mode:
            momentary_mode_on_prop.load(test_value[0])
            momentary_follow_sense_prop.load(test_value[1])
            momentary_on_off_trigger_prop.load(test_value[2])
            assert test_prop.value == test_value[3]
示例#17
0
    def test_new_value(self):
        """Test the new_value property."""
        address = random_address()

        momentary_mode_on_prop = ExtendedProperty(address, "mm_on", bool,
                                                  False, False,
                                                  PropertyType.ADVANCED)
        momentary_follow_sense_prop = ExtendedProperty(address, "m_follow",
                                                       bool, False, False,
                                                       PropertyType.ADVANCED)
        momentary_on_off_trigger_prop = ExtendedProperty(
            address, "m_on_off", bool, False, False, PropertyType.ADVANCED)

        test_prop = RelayModeProperty(
            address,
            "test_prop",
            momentary_mode_on_prop,
            momentary_follow_sense_prop,
            momentary_on_off_trigger_prop,
        )

        for relay_mode, test_value in test_relay_mode_to_prop.items():
            # Make sure the new_value does not eq value otherwise no change is triggered
            momentary_mode_on_prop.load(not test_value[0])
            momentary_follow_sense_prop.load(not test_value[1])
            momentary_on_off_trigger_prop.load(not test_value[2])
            test_prop.new_value = relay_mode
            assert momentary_mode_on_prop.new_value == test_value[0]
            assert momentary_follow_sense_prop.new_value == test_value[1]
            assert momentary_on_off_trigger_prop.new_value == test_value[2]

        # Test resetting relay mode
        momentary_mode_on_prop.load(False)
        momentary_follow_sense_prop.load(False)
        momentary_on_off_trigger_prop.load(False)
        assert test_prop.value == RelayMode.LATCHING
        test_prop.new_value = RelayMode.MOMENTARY_A
        assert test_prop.new_value == RelayMode.MOMENTARY_A
        test_prop.new_value = RelayMode.LATCHING
        assert test_prop.new_value is None
        assert test_prop.value == RelayMode.LATCHING

        test_prop.new_value = RelayMode.MOMENTARY_A
        assert test_prop.new_value == RelayMode.MOMENTARY_A
        test_prop.new_value = None
        assert test_prop.new_value is None
        assert momentary_mode_on_prop.new_value is None
        assert momentary_follow_sense_prop.new_value is None
        assert momentary_on_off_trigger_prop.new_value is None
示例#18
0
    def test_new_value(self):
        """Test setting a new value."""
        address = random_address()
        delay_prop = ExtendedProperty(address, "delay", int, False, False,
                                      PropertyType.ADVANCED)
        prescaler_prop = ExtendedProperty(address, "delay", int, False, False,
                                          PropertyType.ADVANCED)

        test_prop = MomentaryDelayProperty(address, "test_prop", delay_prop,
                                           prescaler_prop)
        delay_prop.load(0)
        prescaler_prop.load(0)
        assert test_prop.new_value is None

        # Test small number delays (< 255).  Result should be exact
        for delay_value in range(1, 255):
            test_prop.new_value = delay_value / 10
            assert test_prop.new_value == delay_value / 10
            assert delay_prop.new_value == delay_value
            assert prescaler_prop.new_value == 1

        # Test a set of larger number
        for _ in range(0, 20):
            delay_value = randint(26, 255)
            prescaler_value = randint(1, 255)
            seconds = delay_value * max(1, prescaler_value) / 10
            test_prop.new_value = seconds
            assert (delay_prop.new_value * prescaler_prop.new_value /
                    10 == test_prop.new_value)

        # Test setting new_value to value
        delay_value = randint(1, 20)
        prescaler_value = 1
        new_delay_value = randint(21, 50)
        seconds = delay_value / 10
        new_seconds = new_delay_value / 10

        # Set delay in seconds to seconds
        delay_prop.load(delay_value)
        prescaler_prop.load(prescaler_value)
        assert test_prop.value == seconds

        # Set new_value to new_seconds
        test_prop.new_value = new_seconds
        assert test_prop.new_value == new_seconds
        assert delay_prop.new_value == new_delay_value

        # Set new_value to seconds and confirm new_value is None
        test_prop.new_value = seconds
        assert test_prop.new_value is None
        assert delay_prop.new_value is None

        # Test too large and too small a number
        for val in [10000, -1]:
            try:
                test_prop.new_value = val
                assert False
            except ValueError:
                assert True
示例#19
0
    def test_is_dirty(self):
        """Test the id_dirty property."""

        address = random_address()

        momentary_mode_on_prop = ExtendedProperty(address, "mm_on", bool,
                                                  False, False,
                                                  PropertyType.ADVANCED)
        momentary_follow_sense_prop = ExtendedProperty(address, "m_follow",
                                                       bool, False, False,
                                                       PropertyType.ADVANCED)
        momentary_on_off_trigger_prop = ExtendedProperty(
            address, "m_on_off", bool, False, False, PropertyType.ADVANCED)

        test_prop = RelayModeProperty(
            address,
            "test_prop",
            momentary_mode_on_prop,
            momentary_follow_sense_prop,
            momentary_on_off_trigger_prop,
        )
        assert not test_prop.is_dirty
        momentary_mode_on_prop.load(False)
        assert not test_prop.is_dirty
        momentary_follow_sense_prop.load(False)
        assert not test_prop.is_dirty
        momentary_on_off_trigger_prop.load(False)
        assert not test_prop.is_dirty

        momentary_mode_on_prop.new_value = True
        assert test_prop.is_dirty
        momentary_mode_on_prop.new_value = None
        assert not test_prop.is_dirty

        momentary_follow_sense_prop.new_value = True
        assert test_prop.is_dirty
        momentary_follow_sense_prop.new_value = None
        assert not test_prop.is_dirty

        momentary_on_off_trigger_prop.new_value = True
        assert test_prop.is_dirty
        momentary_on_off_trigger_prop.new_value = None
        assert not test_prop.is_dirty
示例#20
0
    def test_is_dirty(self):
        """Test the id_dirty property."""
        address = random_address()
        delay_prop = ExtendedProperty(address, "delay", int, False, False,
                                      PropertyType.ADVANCED)
        prescaler_prop = ExtendedProperty(address, "delay", int, False, False,
                                          PropertyType.ADVANCED)

        test_prop = MomentaryDelayProperty(address, "test_prop", delay_prop,
                                           prescaler_prop)
        assert not test_prop.is_dirty
        delay_prop.load(0)
        assert not test_prop.is_dirty
        prescaler_prop.load(0)
        assert not test_prop.is_dirty

        delay_prop.new_value = randint(1, 255)
        assert test_prop.is_dirty

        delay_prop.new_value = None
        assert not test_prop.is_dirty

        prescaler_prop.new_value = randint(1, 255)
        assert test_prop.is_dirty

        prescaler_prop.new_value = None
        assert not test_prop.is_dirty

        delay_prop.new_value = randint(1, 255)
        assert test_prop.is_dirty

        delay_prop.new_value = randint(1, 255)
        prescaler_prop.new_value = randint(1, 255)
        assert test_prop.is_dirty

        delay_prop.new_value = None
        prescaler_prop.new_value = None
        assert not test_prop.is_dirty