Skip to content

Light

One LightEntity is created per PWM channel where pwm#X#onoff = false and pwm#X#color != "".

Brightness mapping

PWM values (0–1000) are mapped to HA brightness (0–255) with exact endpoints:

  • PWM 0 → HA 0
  • PWM 1–3 → HA 1 (avoids is_on=True, brightness=0 contradiction)
  • PWM 996–999 → HA 254
  • PWM 1000 → HA 255

Only HA 255 sends PWM 1000 to the device.

Reference

light

Classes

SunRiserLight

Bases: CoordinatorEntity[SunRiserCoordinator], LightEntity

Dimmable PWM channel on a SunRiser device.

Source code in custom_components/sunriser/light.py
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
class SunRiserLight(CoordinatorEntity[SunRiserCoordinator], LightEntity):
    """Dimmable PWM channel on a SunRiser device."""

    _attr_has_entity_name = True
    _attr_color_mode = ColorMode.BRIGHTNESS
    _attr_supported_color_modes = {ColorMode.BRIGHTNESS}

    def __init__(
        self,
        coordinator: SunRiserCoordinator,
        entry: ConfigEntry,
        pwm_num: int,
    ) -> None:
        super().__init__(coordinator)
        self._pwm_num = pwm_num
        self._attr_unique_id = f"{entry.entry_id}_pwm_{pwm_num}"
        self._attr_name = coordinator.pwm_name(pwm_num)
        self._attr_device_info = coordinator.device_info

    @property
    def is_on(self) -> bool:
        return self.coordinator.pwm_value(self._pwm_num) > 0

    @property
    def brightness(self) -> int:
        return _to_ha_brightness(self.coordinator.pwm_value(self._pwm_num))

    async def async_turn_on(self, **kwargs: Any) -> None:
        brightness: int = cast(int, kwargs.get(ATTR_BRIGHTNESS, 255))
        device_value = _to_device_brightness(brightness)
        await self.coordinator.async_set_pwms({str(self._pwm_num): device_value})
        await self.coordinator.async_request_refresh()

    async def async_turn_off(self, **kwargs: Any) -> None:
        await self.coordinator.async_set_pwms({str(self._pwm_num): 0})
        await self.coordinator.async_request_refresh()