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

Lights: supported color mode cleanup and documentation improvement #135

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions custom_components/smartir/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def __init__(self, hass, config, device_data):
self._brightness = None
self._colortemp = None
self._on_by_remote = False
self._support_color_mode = ColorMode.UNKNOWN
self._power_sensor_check_expect = None
self._power_sensor_check_cancel = None

Expand All @@ -111,25 +110,22 @@ def __init__(self, hass, config, device_data):
and CMD_COLORMODE_WARMER in self._commands
):
self._colortemp = self.max_color_temp_kelvin
self._support_color_mode = ColorMode.COLOR_TEMP

if CMD_NIGHTLIGHT in self._commands or (
CMD_BRIGHTNESS_INCREASE in self._commands
and CMD_BRIGHTNESS_DECREASE in self._commands
):
self._brightness = 100
self._support_brightness = True
if self._support_color_mode == ColorMode.UNKNOWN:
self._support_color_mode = ColorMode.BRIGHTNESS
else:
self._support_brightness = False

if (
CMD_POWER_OFF in self._commands
and CMD_POWER_ON in self._commands
and self._support_color_mode == ColorMode.UNKNOWN
):
self._support_color_mode = ColorMode.ONOFF
if self._colortemp:
self._attr_supported_color_modes = [ColorMode.COLOR_TEMP]
elif self._support_brightness:
self._attr_supported_color_modes = [ColorMode.BRIGHTNESS]
elif CMD_POWER_OFF in self._commands and CMD_POWER_ON in self._commands:
self._attr_supported_color_modes = [ColorMode.ONOFF]

# Init exclusive lock for sending IR commands
self._temp_lock = asyncio.Lock()
Expand Down Expand Up @@ -169,14 +165,10 @@ def name(self):
"""Return the display name of the light."""
return self._name

@property
def supported_color_modes(self):
"""Return the list of supported color modes."""
return [self._support_color_mode]

@property
def color_mode(self):
return self._support_color_mode
# We only support a single color mode currently, so no need to track it
return self._attr_supported_color_modes[0]

@property
def color_temp_kelvin(self):
Expand Down Expand Up @@ -222,7 +214,7 @@ async def async_turn_on(self, **params):

if (
ATTR_COLOR_TEMP_KELVIN in params
and ColorMode.COLOR_TEMP == self._support_color_mode
and ColorMode.COLOR_TEMP in self.supported_color_modes
):
target = params.get(ATTR_COLOR_TEMP_KELVIN)
old_color_temp = DeviceData.closest_match(self._colortemp, self._colortemps)
Expand Down
150 changes: 137 additions & 13 deletions docs/LIGHT.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ Find your device's brand code [here](LIGHT_CODES.md) and add the number in the `

## Configuration variables

| Name | Type | Default | Description |
| --- | :---: | :---: | --- |
| `name` | string | optional | The name of the device |
| `unique_id` | string | optional | An ID that uniquely identified this device. If two devices have the same unique ID, Home Assistant will raise an exception. |
| `device_code` | number | required | (Accepts only positive numbers) |
| `controller_data` | string | required | The data required for the controller to function. Look into configuration examples below for valid configuration entries for different controller types. |
| `delay` | number | optional | Adjusts the delay in seconds between multiple commands. The default is 0.5 |
| `power_sensor` | string | optional | _entity_id_ for a sensor or that monitors whether your device is actually On or Off. This may be a power monitor sensor, or a helper that monitors power usage with a threshold. (Accepts only on/off states) |
| Name | Type | Default | Description |
| ---------------------------- | :-----: | :------: | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name` | string | optional | The name of the device |
| `unique_id` | string | optional | An ID that uniquely identified this device. If two devices have the same unique ID, Home Assistant will raise an exception. |
| `device_code` | number | required | (Accepts only positive numbers) |
| `controller_data` | string | required | The data required for the controller to function. Look into configuration examples below for valid configuration entries for different controller types. |
| `delay` | number | optional | Adjusts the delay in seconds between multiple commands. The default is 0.5 |
| `power_sensor` | string | optional | _entity_id_ for a sensor or that monitors whether your device is actually On or Off. This may be a power monitor sensor, or a helper that monitors power usage with a threshold. (Accepts only on/off states) |
| `power_sensor_delay` | int | optional | Maximum delay in second in which power sensor is able to report back to HA changed state of the device, default is 10 seconds. If sensor reaction time is longer extend this time, otherwise you might get unwanted changes in the device state. |
| `power_sensor_restore_state` | boolean | optional | If `true` than in case power sensor will report to HA that device is `on` without HA actually switching it `on `(device was switched on by remote, of device cycled, etc.), than HA will report last assumed state and attributes at the time when the device was `on` managed by HA. If set to `false` when device will be reported as `on` by the power sensors all device attributes will be reported as `UNKNOWN`. Default is `true`. |

## Example (using broadlink controller)

Expand All @@ -25,18 +27,140 @@ light:
name: Bedroom Ceiling Light
unique_id: bedroom_ceiling_light
device_code: 1000
controller_data: remote.bedroom_remote
controller_data:
controller_type: Broadlink
remote_entity: remote.bedroom_remote
delay_secs: 0.5
num_repeats: 1
power_sensor: binary_sensor.bedroom_light_power
```

## Light configuration
## Example (using xiaomi controller)

As well as the generic settings, the light supports two lists:
```yaml
remote:
- platform: xiaomi_miio
host: 192.168.10.10
token: YOUR_TOKEN

light:
- platform: smartir
name: Bedroom light
unique_id: bedroom_light
device_code: 2000
controller_data:
controller_type: Xiaomi
remote_entity: remote.xiaomi_miio_192_168_10_10
power_sensor: binary_sensor.bedroom_light_power
```

### Example (using MQTT controller)

```yaml
light:
- platform: smartir
name: Bedroom light
unique_id: bedroom_light
device_code: 3000
controller_data:
controller_type: MQTT
mqtt_topic: home-assistant/bedroom_light/command
power_sensor: binary_sensor.bedroom_light_power
```

### Example (using mqtt Z06/UFO-R11 controller)

```yaml
light:
- platform: smartir
name: Bedroom light
unique_id: bedroom_light
device_code: 3000
controller_data:
controller_type: UFOR11
mqtt_topic: home-assistant/bedroom_light/command
power_sensor: binary_sensor.bedroom_light_power
```

### Example (using LOOKin controller)

```yaml
light:
- platform: smartir
name: Bedroom light
unique_id: bedroom_light
device_code: 4000
controller_data:
controller_type: LOOKin
remote_host: 192.168.10.10
power_sensor: binary_sensor.bedroom_light_power
```

### Example (using ESPHome)

ESPHome configuration example:

```yaml
esphome:
name: my_espir
platform: ESP8266
board: esp01_1m

api:
services:
- service: send_raw_command
variables:
command: int[]
then:
- remote_transmitter.transmit_raw:
code: !lambda "return command;"

remote_transmitter:
pin: GPIO14
carrier_duty_percent: 50%
```

HA configuration.yaml:

```yaml
light:
- platform: smartir
name: Bedroom light
unique_id: bedroom_light
device_code: 4000
controller_data:
controller_type: ESPHome
esphome_service: my_espir_send_raw_command
power_sensor: binary_sensor.bedroom_light_power
```

### Example (using ZHA controller and a TuYa ZS06)

```yaml
light:
- platform: smartir
name: Bedroom light
unique_id: bedroom_light
device_code: 5000
controller_data:
controller_type: ZHA
zha_ieee: "XX:XX:XX:XX:XX:XX:XX:XX"
zha_endpoint_id: 1
zha_cluster_id: 57348
zha_cluster_type: "in"
zha_command: 2
zha_command_type: "server"
power_sensor: binary_sensor.bedroom_light_power
```

## Light device files

As well as the command mappings, the light device config supports two lists:
`brightness` and `color_temperature`. These should be sorted lists
from lower to higher values of brightness on a scale of 1 to 255, and
color temperature in Kelvin (normally from 2700 to 6500). Supported
commands are "on", "off", "brighten", "dim", "warmer", "colder" and "night".
If "night" is configured, it is implemented as a special brightness step that
commands are `on` `off`, `brighten`, `dim`, `warmer`, `colder` and `night`.
If `night` is configured, it is implemented as a special brightness step that
can be selected by setting a brightness of 1 (such lights usually have a
separate small and dim nightlight bulb inside the fixture).

Expand Down
Loading