Skip to content

Commit

Permalink
Use HA scaling functions for fan speed and light brightness
Browse files Browse the repository at this point in the history
HA 2024.1 introduced some new functions for scaling percentages and
light brightness. Using these should give standardised behaviour
compared with other integrations.

User visible changes:

- lights that do not allow brightness to be set all the way down to 1%
  will now adjust their range to use the full range available in HA.
- fan speeds scale to integers. This mostly affects the speeds
  reported by 3 speed and 6 speed fans

Developer visible changes:

- it is no longer neccesary to specify scale mappings for light
  brightness or fan speed. Since these have a fixed range in HA,
  they are automatically scaled into that range.

- TuyaDpsConfig.range now returns a (min, max) tuple compatible with
  the HA scaling functions instead of a dict with min and max items.

Issue make-all#1444
  • Loading branch information
make-all authored and timlaing committed Aug 8, 2024
1 parent a90f31f commit 666fa40
Show file tree
Hide file tree
Showing 109 changed files with 154 additions and 295 deletions.
8 changes: 4 additions & 4 deletions custom_components/tuya_local/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def min_temp(self):
r = self._temp_low_dps.range(self._device)
else:
r = self._temperature_dps.range(self._device)
return DEFAULT_MIN_TEMP if r is None else r["min"]
return DEFAULT_MIN_TEMP if r is None else r[0]

@property
def max_temp(self):
Expand All @@ -226,7 +226,7 @@ def max_temp(self):
r = self._temp_high_dps.range(self._device)
else:
r = self._temperature_dps.range(self._device)
return DEFAULT_MAX_TEMP if r is None else r["max"]
return DEFAULT_MAX_TEMP if r is None else r[1]

async def async_set_temperature(self, **kwargs):
"""Set new target temperature."""
Expand Down Expand Up @@ -284,15 +284,15 @@ def min_humidity(self):
if self._humidity_dps is None:
return None
r = self._humidity_dps.range(self._device)
return DEFAULT_MIN_HUMIDITY if r is None else r["min"]
return DEFAULT_MIN_HUMIDITY if r is None else r[0]

@property
def max_humidity(self):
"""Return the maximum supported target humidity."""
if self._humidity_dps is None:
return None
r = self._humidity_dps.range(self._device)
return DEFAULT_MAX_HUMIDITY if r is None else r["max"]
return DEFAULT_MAX_HUMIDITY if r is None else r[1]

async def async_set_humidity(self, humidity: int):
if self._humidity_dps is None:
Expand Down
7 changes: 4 additions & 3 deletions custom_components/tuya_local/devices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,9 @@ Either **position** or **open** should be specified.
- **switch** (optional, boolean): a dp to control the power state of the fan
- **preset_mode** (optional, mapping of strings): a dp to control different modes of the fan.
Values `"off", low, medium, high` used to be handled specially by HA as deprecated speed aliases. If these are the only "presets", consider mapping them as **speed** values instead, as voice assistants will respond to phrases like "turn the fan up/down" for speed.
- **speed** (optional, number 0-100): a dp to control the speed of the fan (%).
scale and step can be used to convert smaller ranges to percentages, or a mapping for discrete values.
- **speed** (optional, number): a dp to control the speed of the fan (%).
if a range is supplied, it will be automatically scaled to the 0-100 range.
if a mapping is used for dicrete steps, the values must be mapped within that range.
- **oscillate** (optional, boolean): a dp to control whether the fan will oscillate or not.
- **direction** (optional, string): a dp to control the spin direction of the fan.
Valid values are `forward, reverse`.
Expand All @@ -603,7 +604,7 @@ Humidifer can also cover dehumidifiers (use class to specify which).

### `light`
- **switch** (optional, boolean): a dp to control the on/off state of the light
- **brightness** (optional, number 0-255): a dp to control the dimmer if available.
- **brightness** (optional, number): a dp to control the dimmer if available. If a range is provided, the value will be automatically scaled into the 0-255 range for HA, so there is no need to provide a scale. If there is a fixed list of mappings, the values should be between 0 (off) and 255 (full brightness). If there is no switch dp, a brightness of 0 will be sent to turn the light off.
- **color_temp** (optional, number): a dp to control the color temperature if available. See `target_range` above for mapping Tuya's range into Kelvin.

- **rgbhsv** (optional, hex): a dp to control the color of the light, using encoded RGB and HSV values. The `format` field names recognized for decoding this field are `r`, `g`, `b`, `h`, `s`, `v`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ secondary_entities:
min: 0
max: 100
optional: true
mapping:
- scale: 0.392
- id: 109
name: rgbhsv
type: base64
Expand Down Expand Up @@ -173,8 +171,6 @@ secondary_entities:
range:
min: 0
max: 100
mapping:
- scale: 0.392
- id: 110
name: rgbhsv
type: base64
Expand Down Expand Up @@ -219,8 +215,6 @@ secondary_entities:
range:
min: 0
max: 100
mapping:
- scale: 0.392
- id: 111
name: rgbhsv
type: base64
Expand Down
4 changes: 1 addition & 3 deletions custom_components/tuya_local/devices/arlec_19speed_fan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ primary_entity:
name: speed
type: integer
range:
min: 0
min: 1
max: 19
mapping:
- scale: 0.19
- id: 5
name: oscillate
type: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ primary_entity:
range:
min: 1
max: 3
mapping:
- scale: 0.03
secondary_entities:
- entity: light
dps:
Expand Down
4 changes: 1 addition & 3 deletions custom_components/tuya_local/devices/arlec_fan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ primary_entity:
name: speed
type: integer
range:
min: 0
min: 1
max: 6
mapping:
- scale: 0.06
- id: 4
name: direction
type: string
Expand Down
5 changes: 1 addition & 4 deletions custom_components/tuya_local/devices/arlec_fan_light.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ primary_entity:
name: speed
type: integer
range:
min: 0
min: 1
max: 6
mapping:
- scale: 0.06
- id: 4
name: direction
type: string
Expand All @@ -39,7 +37,6 @@ secondary_entities:
max: 100
mapping:
- step: 2
scale: 0.392
- id: 11
type: integer
name: color_temp
Expand Down
4 changes: 1 addition & 3 deletions custom_components/tuya_local/devices/aspen_asp200_fan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ primary_entity:
type: integer
name: speed
range:
min: 0
min: 1
max: 3
mapping:
- scale: 0.03
- id: 101
type: boolean
name: preset_mode
Expand Down
6 changes: 1 addition & 5 deletions custom_components/tuya_local/devices/atomi_ceiling_fan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ primary_entity:
type: integer
name: speed
range:
min: 0
min: 1
max: 3
mapping:
- scale: 0.03
- id: 71
type: string
name: unknown_71
Expand All @@ -47,8 +45,6 @@ secondary_entities:
range:
min: 10
max: 1000
mapping:
- scale: 3.92
- id: 23
name: color_temp
type: integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ secondary_entities:
range:
min: 1
max: 9
mapping:
- scale: 0.0353
- entity: light
name: Display standby
category: config
Expand All @@ -160,8 +158,6 @@ secondary_entities:
range:
min: 0
max: 9
mapping:
- scale: 0.0353
- entity: number
name: Standby delay
category: config
Expand Down
3 changes: 1 addition & 2 deletions custom_components/tuya_local/devices/ble_solar_light.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ primary_entity:
min: 10
max: 1000
mapping:
- scale: 3.92
constraint: standby
- constraint: standby
conditions:
- dps_val: true
value_redirect: standby_brightness
Expand Down
2 changes: 0 additions & 2 deletions custom_components/tuya_local/devices/blitzwolf_bw_lt31.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ primary_entity:
range:
min: 10
max: 1000
mapping:
- scale: 3.92
- id: 24
name: rgbhsv
type: hex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ primary_entity:
range:
min: 0
max: 8
mapping:
- scale: 0.08
- id: 2
type: string
name: preset_mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ secondary_entities:
range:
min: 10
max: 1000
mapping:
- scale: 3.92
# 10: rgbhsv, but not present in log and no color_mode to select it,
# so probably a doc error
- id: 105
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ primary_entity:
range:
min: 10
max: 1000
mapping:
- scale: 3.92
- id: 28
type: string
name: control_data
Expand All @@ -35,10 +33,6 @@ secondary_entities:
- id: 62
type: integer
name: speed
mapping:
- dps_val: 1
value: 33
- dps_val: 2
value: 66
- dps_val: 3
value: 100
range:
min: 1
max: 3
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ primary_entity:
range:
min: 1
max: 10
mapping:
- scale: 0.1
- id: 103
type: string
name: direction
Expand Down Expand Up @@ -57,8 +55,6 @@ secondary_entities:
range:
min: 10
max: 1000
mapping:
- scale: 3.92
- entity: light
name: Daylight light
dps:
Expand All @@ -71,5 +67,3 @@ secondary_entities:
range:
min: 10
max: 1000
mapping:
- scale: 3.92
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ primary_entity:
range:
min: 1
max: 6
mapping:
- scale: 0.06
- id: 8
type: string
name: direction
Expand All @@ -42,8 +40,6 @@ secondary_entities:
range:
min: 10
max: 100
mapping:
- scale: 0.392
- id: 17
# 17 is listed as color_temp, and reported by the device, but light
# is fixed 3000K light
Expand Down
2 changes: 0 additions & 2 deletions custom_components/tuya_local/devices/cct_lightbulb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ primary_entity:
range:
min: 10
max: 1000
mapping:
- scale: 3.92
- id: 23
name: color_temp
type: integer
Expand Down
3 changes: 0 additions & 3 deletions custom_components/tuya_local/devices/cecotec_fan_light.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ primary_entity:
- id: 3
name: speed
type: string
range:
min: 1
max: 6
mapping:
- dps_val: "1"
value: 17
Expand Down
6 changes: 1 addition & 5 deletions custom_components/tuya_local/devices/chanfok_fan_light.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ primary_entity:
type: integer
name: speed
range:
min: 0
min: 1
max: 6
mapping:
- scale: 0.06
- id: 63
type: string
name: direction
Expand All @@ -43,8 +41,6 @@ secondary_entities:
range:
min: 10
max: 1000
mapping:
- scale: 3.92
- id: 23
name: color_temp
type: integer
Expand Down
2 changes: 0 additions & 2 deletions custom_components/tuya_local/devices/deta_dimmer_switch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ primary_entity:
range:
min: 10
max: 1000
mapping:
- scale: 3.92
secondary_entities:
- entity: number
name: Minimum brightness
Expand Down
4 changes: 1 addition & 3 deletions custom_components/tuya_local/devices/deta_fan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ primary_entity:
type: integer
name: speed
range:
min: 0
min: 1
max: 3
mapping:
- scale: 0.03
- id: 102
type: integer
name: timer
Expand Down
2 changes: 0 additions & 2 deletions custom_components/tuya_local/devices/dimming_lightbulb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ primary_entity:
range:
min: 10
max: 1000
mapping:
- scale: 3.92
secondary_entities:
- entity: number
name: Timer
Expand Down
4 changes: 1 addition & 3 deletions custom_components/tuya_local/devices/djive_arc_fan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ primary_entity:
name: speed
type: integer
range:
min: 0
min: 1
max: 9
mapping:
- scale: 0.09
- id: 5
type: boolean
name: oscillate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ primary_entity:
mapping:
- dps_val: null
value: 0
- scale: 3.92
- id: 23
name: color_temp
type: integer
Expand Down
Loading

0 comments on commit 666fa40

Please sign in to comment.