Beispiel #1
0
 async def change_source_list(self, direction: str) -> None:
     entity_states = await self.get_entity_state(self.media_player,
                                                 attribute="all")
     entity_attributes = entity_states["attributes"]
     source_list = entity_attributes.get("source_list")
     if len(source_list) == 0 or source_list is None:
         self.log(
             f"⚠️ There is no `source_list` parameter in `{self.media_player}`",
             level="WARNING",
             ascii_encode=False,
         )
         return
     source = entity_attributes.get("source")
     if source is None:
         new_index_source = 0
     else:
         index_source = source_list.index(source)
         source_stepper = CircularStepper(0,
                                          len(source_list) - 1,
                                          len(source_list))
         new_index_source, _ = source_stepper.step(index_source, direction)
     await self.call_service(
         "media_player/select_source",
         entity_id=self.media_player,
         source=source_list[new_index_source],
     )
def test_minmax_stepper(minmax, value, steps, direction, expected_value):
    stepper = CircularStepper(*minmax, steps)

    # SUT
    new_value, _ = stepper.step(value, direction)

    # Checks
    assert new_value == expected_value
def test_minmax_stepper(
    minmax: Tuple[int, int],
    value: int,
    steps: int,
    direction: Literal["up", "down"],
    expected_value: int,
):
    stepper = CircularStepper(*minmax, steps)
    new_value, _ = stepper.step(value, direction)
    assert new_value == expected_value
    async def init(self) -> None:
        manual_steps = self.args.get("manual_steps", DEFAULT_MANUAL_STEPS)
        automatic_steps = self.args.get("automatic_steps",
                                        DEFAULT_AUTOMATIC_STEPS)
        self.min_brightness = self.args.get("min_brightness",
                                            DEFAULT_MIN_BRIGHTNESS)
        self.max_brightness = self.args.get("max_brightness",
                                            DEFAULT_MAX_BRIGHTNESS)
        self.min_white_value = self.args.get("min_white_value",
                                             DEFAULT_MIN_WHITE_VALUE)
        self.max_white_value = self.args.get("max_white_value",
                                             DEFAULT_MAX_WHITE_VALUE)
        self.min_color_temp = self.args.get("min_color_temp",
                                            DEFAULT_MIN_COLOR_TEMP)
        self.max_color_temp = self.args.get("max_color_temp",
                                            DEFAULT_MAX_COLOR_TEMP)
        self.transition = self.args.get("transition", DEFAULT_TRANSITION)
        self.color_wheel = get_color_wheel(
            self.args.get("color_wheel", "default_color_wheel"))

        color_stepper = CircularStepper(0,
                                        len(self.color_wheel) - 1,
                                        len(self.color_wheel))
        self.manual_steppers: Dict[str, Stepper] = {
            LightController.ATTRIBUTE_BRIGHTNESS:
            MinMaxStepper(self.min_brightness, self.max_brightness,
                          manual_steps),
            LightController.ATTRIBUTE_WHITE_VALUE:
            MinMaxStepper(self.min_white_value, self.max_white_value,
                          manual_steps),
            LightController.ATTRIBUTE_COLOR_TEMP:
            MinMaxStepper(self.min_color_temp, self.max_color_temp,
                          manual_steps),
            LightController.ATTRIBUTE_XY_COLOR:
            color_stepper,
        }
        self.automatic_steppers: Dict[str, Stepper] = {
            LightController.ATTRIBUTE_BRIGHTNESS:
            MinMaxStepper(self.min_brightness, self.max_brightness,
                          automatic_steps),
            LightController.ATTRIBUTE_WHITE_VALUE:
            MinMaxStepper(self.min_white_value, self.max_white_value,
                          automatic_steps),
            LightController.ATTRIBUTE_COLOR_TEMP:
            MinMaxStepper(self.min_color_temp, self.max_color_temp,
                          automatic_steps),
            LightController.ATTRIBUTE_XY_COLOR:
            color_stepper,
        }
        self.smooth_power_on = self.args.get("smooth_power_on",
                                             self.supports_smooth_power_on())
        self.add_transition = self.args.get("add_transition",
                                            DEFAULT_ADD_TRANSITION)
        self.add_transition_turn_toggle = self.args.get(
            "add_transition_turn_toggle", DEFAULT_TRANSITION_TURN_TOGGLE)
        await super().init()
    async def initialize(self) -> None:
        self.light = self.get_light(self.args["light"])
        await self.check_domain(self.light["name"])
        manual_steps = self.args.get("manual_steps", DEFAULT_MANUAL_STEPS)
        automatic_steps = self.args.get("automatic_steps", DEFAULT_AUTOMATIC_STEPS)
        self.min_brightness = self.args.get("min_brightness", DEFAULT_MIN_BRIGHTNESS)
        self.max_brightness = self.args.get("max_brightness", DEFAULT_MAX_BRIGHTNESS)
        self.min_white_value = self.args.get("min_white_value", DEFAULT_MIN_WHITE_VALUE)
        self.max_white_value = self.args.get("max_white_value", DEFAULT_MAX_WHITE_VALUE)
        self.min_color_temp = self.args.get("min_color_temp", DEFAULT_MIN_COLOR_TEMP)
        self.max_color_temp = self.args.get("max_color_temp", DEFAULT_MAX_COLOR_TEMP)
        self.transition = self.args.get("transition", DEFAULT_TRANSITION)
        self.color_wheel = get_color_wheel(
            self.args.get("color_wheel", "default_color_wheel")
        )

        color_stepper = CircularStepper(
            0, len(self.color_wheel) - 1, len(self.color_wheel)
        )
        self.manual_steppers: Dict[str, Stepper] = {
            LightController.ATTRIBUTE_BRIGHTNESS: MinMaxStepper(
                self.min_brightness, self.max_brightness, manual_steps
            ),
            LightController.ATTRIBUTE_WHITE_VALUE: MinMaxStepper(
                self.min_white_value, self.max_white_value, manual_steps
            ),
            LightController.ATTRIBUTE_COLOR_TEMP: MinMaxStepper(
                self.min_color_temp, self.max_color_temp, manual_steps
            ),
            LightController.ATTRIBUTE_XY_COLOR: color_stepper,
        }
        self.automatic_steppers: Dict[str, Stepper] = {
            LightController.ATTRIBUTE_BRIGHTNESS: MinMaxStepper(
                self.min_brightness, self.max_brightness, automatic_steps
            ),
            LightController.ATTRIBUTE_WHITE_VALUE: MinMaxStepper(
                self.min_white_value, self.max_white_value, automatic_steps
            ),
            LightController.ATTRIBUTE_COLOR_TEMP: MinMaxStepper(
                self.min_color_temp, self.max_color_temp, automatic_steps
            ),
            LightController.ATTRIBUTE_XY_COLOR: color_stepper,
        }
        self.smooth_power_on = self.args.get(
            "smooth_power_on", self.supports_smooth_power_on()
        )
        self.add_transition = self.args.get("add_transition", True)
        self.add_transition_turn_toggle = self.args.get(
            "add_transition_turn_toggle", False
        )
        update_supported_features = self.args.get("update_supported_features", False)

        self.supported_features = LightSupport(
            self.light["name"], self, update_supported_features
        )
        await super().initialize()
 [
     (
         50,
         LightController.ATTRIBUTE_BRIGHTNESS,
         Stepper.UP,
         MinMaxStepper(1, 255, 254),
         "on",
         False,
         False,
         51,
     ),
     (
         0,
         "xy_color",
         Stepper.UP,
         CircularStepper(0, 30, 30),
         "on",
         False,
         False,
         0,
     ),
     (
         499,
         "color_temp",
         Stepper.UP,
         MinMaxStepper(153, 500, 10),
         "on",
         False,
         True,
         500,
     ),
        assert output == float(expected_output)


@pytest.mark.parametrize(
    "old, attribute, direction, stepper, smooth_power_on_check, stop_expected, expected_value_attribute",
    [
        (
            50,
            LightController.ATTRIBUTE_BRIGHTNESS,
            Stepper.UP,
            MinMaxStepper(1, 255, 254),
            False,
            False,
            51,
        ),
        (0, "xy_color", Stepper.UP, CircularStepper(0, 30,
                                                    30), False, False, 0),
        (
            499,
            "color_temp",
            Stepper.UP,
            MinMaxStepper(153, 500, 10),
            False,
            True,
            500,
        ),
        (
            0,
            LightController.ATTRIBUTE_BRIGHTNESS,
            Stepper.UP,
            MinMaxStepper(1, 255, 254),
            True,