From b97d5a703ccfcdb2a00ea8745f71f273691b8446 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 15 Oct 2021 11:33:30 +0200 Subject: [PATCH] Renamed variables in Tuya (#57759) --- homeassistant/components/tuya/base.py | 24 ++++----- .../components/tuya/binary_sensor.py | 2 +- homeassistant/components/tuya/climate.py | 45 ++++++++--------- homeassistant/components/tuya/fan.py | 50 +++++++++---------- homeassistant/components/tuya/light.py | 44 ++++++++-------- homeassistant/components/tuya/number.py | 2 +- homeassistant/components/tuya/select.py | 2 +- homeassistant/components/tuya/sensor.py | 2 +- homeassistant/components/tuya/switch.py | 2 +- 9 files changed, 85 insertions(+), 88 deletions(-) diff --git a/homeassistant/components/tuya/base.py b/homeassistant/components/tuya/base.py index 8d1d8d26f94..66b497c2f4e 100644 --- a/homeassistant/components/tuya/base.py +++ b/homeassistant/components/tuya/base.py @@ -71,8 +71,8 @@ class TuyaEntity(Entity): def __init__(self, device: TuyaDevice, device_manager: TuyaDeviceManager) -> None: """Init TuyaHaEntity.""" self._attr_unique_id = f"tuya.{device.id}" - self.tuya_device = device - self.tuya_device_manager = device_manager + self.device = device + self.device_manager = device_manager @property def name(self) -> str | None: @@ -81,37 +81,35 @@ class TuyaEntity(Entity): hasattr(self, "entity_description") and self.entity_description.name is not None ): - return f"{self.tuya_device.name} {self.entity_description.name}" - return self.tuya_device.name + return f"{self.device.name} {self.entity_description.name}" + return self.device.name @property def device_info(self) -> DeviceInfo: """Return a device description for device registry.""" return DeviceInfo( - identifiers={(DOMAIN, self.tuya_device.id)}, + identifiers={(DOMAIN, self.device.id)}, manufacturer="Tuya", - name=self.tuya_device.name, - model=self.tuya_device.product_name, + name=self.device.name, + model=self.device.product_name, ) @property def available(self) -> bool: """Return if the device is available.""" - return self.tuya_device.online + return self.device.online async def async_added_to_hass(self) -> None: """Call when entity is added to hass.""" self.async_on_remove( async_dispatcher_connect( self.hass, - f"{TUYA_HA_SIGNAL_UPDATE_ENTITY}_{self.tuya_device.id}", + f"{TUYA_HA_SIGNAL_UPDATE_ENTITY}_{self.device.id}", self.async_write_ha_state, ) ) def _send_command(self, commands: list[dict[str, Any]]) -> None: """Send command to the device.""" - _LOGGER.debug( - "Sending commands for device %s: %s", self.tuya_device.id, commands - ) - self.tuya_device_manager.send_commands(self.tuya_device.id, commands) + _LOGGER.debug("Sending commands for device %s: %s", self.device.id, commands) + self.device_manager.send_commands(self.device.id, commands) diff --git a/homeassistant/components/tuya/binary_sensor.py b/homeassistant/components/tuya/binary_sensor.py index a78e5362f36..0a6f0aed053 100644 --- a/homeassistant/components/tuya/binary_sensor.py +++ b/homeassistant/components/tuya/binary_sensor.py @@ -88,4 +88,4 @@ class TuyaBinarySensorEntity(TuyaEntity, BinarySensorEntity): @property def is_on(self) -> bool: """Return true if sensor is on.""" - return self.tuya_device.status.get(self.entity_description.key, False) + return self.device.status.get(self.entity_description.key, False) diff --git a/homeassistant/components/tuya/climate.py b/homeassistant/components/tuya/climate.py index d05d063df05..3d678b84fd0 100644 --- a/homeassistant/components/tuya/climate.py +++ b/homeassistant/components/tuya/climate.py @@ -251,7 +251,7 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity): ): self._current_humidity_dpcode = DPCode.HUMIDITY_CURRENT self._current_humidity_type = IntegerTypeData.from_json( - self.tuya_device.status_range[DPCode.HUMIDITY_CURRENT].values + self.device.status_range[DPCode.HUMIDITY_CURRENT].values ) # Determine dpcode to use for getting the current humidity @@ -261,7 +261,7 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity): ): self._current_humidity_dpcode = DPCode.HUMIDITY_CURRENT self._current_humidity_type = IntegerTypeData.from_json( - self.tuya_device.status_range[DPCode.HUMIDITY_CURRENT].values + self.device.status_range[DPCode.HUMIDITY_CURRENT].values ) # Determine fan modes @@ -271,12 +271,12 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity): ): self._attr_supported_features |= SUPPORT_FAN_MODE self._attr_fan_modes = EnumTypeData.from_json( - self.tuya_device.status_range[DPCode.FAN_SPEED_ENUM].values + self.device.status_range[DPCode.FAN_SPEED_ENUM].values ).range # Determine swing modes if any( - dpcode in self.tuya_device.function + dpcode in self.device.function for dpcode in ( DPCode.SHAKE, DPCode.SWING, @@ -287,15 +287,15 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity): self._attr_supported_features |= SUPPORT_SWING_MODE self._attr_swing_modes = [SWING_OFF] if any( - dpcode in self.tuya_device.function + dpcode in self.device.function for dpcode in (DPCode.SHAKE, DPCode.SWING) ): self._attr_swing_modes.append(SWING_ON) - if DPCode.SWITCH_HORIZONTAL in self.tuya_device.function: + if DPCode.SWITCH_HORIZONTAL in self.device.function: self._attr_swing_modes.append(SWING_HORIZONTAL) - if DPCode.SWITCH_VERTICAL in self.tuya_device.function: + if DPCode.SWITCH_VERTICAL in self.device.function: self._attr_swing_modes.append(SWING_VERTICAL) def set_hvac_mode(self, hvac_mode: str) -> None: @@ -379,7 +379,7 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity): ): return None - temperature = self.tuya_device.status.get(self._current_temperature_dpcode) + temperature = self.device.status.get(self._current_temperature_dpcode) if temperature is None: return None @@ -391,7 +391,7 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity): if self._current_humidity_dpcode is None or self._current_humidity_type is None: return None - humidity = self.tuya_device.status.get(self._current_humidity_dpcode) + humidity = self.device.status.get(self._current_humidity_dpcode) if humidity is None: return None @@ -403,7 +403,7 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity): if self._set_temperature_dpcode is None or self._set_temperature_type is None: return None - temperature = self.tuya_device.status.get(self._set_temperature_dpcode) + temperature = self.device.status.get(self._set_temperature_dpcode) if temperature is None: return None @@ -415,7 +415,7 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity): if self._set_humidity_dpcode is None or self._set_humidity_type is None: return None - humidity = self.tuya_device.status.get(self._set_humidity_dpcode) + humidity = self.device.status.get(self._set_humidity_dpcode) if humidity is None: return None @@ -426,34 +426,33 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity): """Return hvac mode.""" # If the switch off, hvac mode is off as well. Unless the switch # the switch is on or doesn't exists of course... - if not self.tuya_device.status.get(DPCode.SWITCH, True): + if not self.device.status.get(DPCode.SWITCH, True): return HVAC_MODE_OFF - if DPCode.MODE not in self.tuya_device.function: - if self.tuya_device.status.get(DPCode.SWITCH, False): + if DPCode.MODE not in self.device.function: + if self.device.status.get(DPCode.SWITCH, False): return self.entity_description.switch_only_hvac_mode return HVAC_MODE_OFF - if self.tuya_device.status.get(DPCode.MODE) is not None: - return TUYA_HVAC_TO_HA[self.tuya_device.status[DPCode.MODE]] + if self.device.status.get(DPCode.MODE) is not None: + return TUYA_HVAC_TO_HA[self.device.status[DPCode.MODE]] return HVAC_MODE_OFF @property def fan_mode(self) -> str | None: """Return fan mode.""" - return self.tuya_device.status.get(DPCode.FAN_SPEED_ENUM) + return self.device.status.get(DPCode.FAN_SPEED_ENUM) @property def swing_mode(self) -> str: """Return swing mode.""" if any( - self.tuya_device.status.get(dpcode) - for dpcode in (DPCode.SHAKE, DPCode.SWING) + self.device.status.get(dpcode) for dpcode in (DPCode.SHAKE, DPCode.SWING) ): return SWING_ON - horizontal = self.tuya_device.status.get(DPCode.SWITCH_HORIZONTAL) - vertical = self.tuya_device.status.get(DPCode.SWITCH_VERTICAL) + horizontal = self.device.status.get(DPCode.SWITCH_HORIZONTAL) + vertical = self.device.status.get(DPCode.SWITCH_VERTICAL) if horizontal and vertical: return SWING_BOTH if horizontal: @@ -465,7 +464,7 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity): def turn_on(self) -> None: """Turn the device on, retaining current HVAC (if supported).""" - if DPCode.SWITCH in self.tuya_device.function: + if DPCode.SWITCH in self.device.function: self._send_command([{"code": DPCode.SWITCH, "value": True}]) return @@ -478,7 +477,7 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity): def turn_off(self) -> None: """Turn the device on, retaining current HVAC (if supported).""" - if DPCode.SWITCH in self.tuya_device.function: + if DPCode.SWITCH in self.device.function: self._send_command([{"code": DPCode.SWITCH, "value": False}]) return diff --git a/homeassistant/components/tuya/fan.py b/homeassistant/components/tuya/fan.py index 35b893f9103..99d28f1b998 100644 --- a/homeassistant/components/tuya/fan.py +++ b/homeassistant/components/tuya/fan.py @@ -65,9 +65,9 @@ class TuyaFanEntity(TuyaEntity, FanEntity): super().__init__(device, device_manager) self.ha_preset_modes = [] - if DPCode.MODE in self.tuya_device.function: + if DPCode.MODE in self.device.function: self.ha_preset_modes = json.loads( - self.tuya_device.function[DPCode.MODE].values + self.device.function[DPCode.MODE].values ).get("range", []) # Air purifier fan can be controlled either via the ranged values or via the enum. @@ -76,18 +76,18 @@ class TuyaFanEntity(TuyaEntity, FanEntity): # Range is used for e.g. Concept CA3000 self.air_purifier_speed_range_len = 0 self.air_purifier_speed_range_enum = [] - if self.tuya_device.category == "kj" and ( - DPCode.FAN_SPEED_ENUM in self.tuya_device.function - or DPCode.SPEED in self.tuya_device.function + if self.device.category == "kj" and ( + DPCode.FAN_SPEED_ENUM in self.device.function + or DPCode.SPEED in self.device.function ): - if DPCode.FAN_SPEED_ENUM in self.tuya_device.function: + if DPCode.FAN_SPEED_ENUM in self.device.function: self.dp_code_speed_enum = DPCode.FAN_SPEED_ENUM else: self.dp_code_speed_enum = DPCode.SPEED - data = json.loads( - self.tuya_device.function[self.dp_code_speed_enum].values - ).get("range") + data = json.loads(self.device.function[self.dp_code_speed_enum].values).get( + "range" + ) if data: self.air_purifier_speed_range_len = len(data) self.air_purifier_speed_range_enum = data @@ -102,7 +102,7 @@ class TuyaFanEntity(TuyaEntity, FanEntity): def set_percentage(self, percentage: int) -> None: """Set the speed of the fan, as a percentage.""" - if self.tuya_device.category == "kj": + if self.device.category == "kj": value_in_range = percentage_to_ordered_list_item( self.air_purifier_speed_range_enum, percentage ) @@ -140,19 +140,19 @@ class TuyaFanEntity(TuyaEntity, FanEntity): @property def is_on(self) -> bool: """Return true if fan is on.""" - return self.tuya_device.status.get(DPCode.SWITCH, False) + return self.device.status.get(DPCode.SWITCH, False) @property def current_direction(self) -> str: """Return the current direction of the fan.""" - if self.tuya_device.status[DPCode.FAN_DIRECTION]: + if self.device.status[DPCode.FAN_DIRECTION]: return DIRECTION_FORWARD return DIRECTION_REVERSE @property def oscillating(self) -> bool: """Return true if the fan is oscillating.""" - return self.tuya_device.status.get(DPCode.SWITCH_HORIZONTAL, False) + return self.device.status.get(DPCode.SWITCH_HORIZONTAL, False) @property def preset_modes(self) -> list[str]: @@ -162,7 +162,7 @@ class TuyaFanEntity(TuyaEntity, FanEntity): @property def preset_mode(self) -> str: """Return the current preset_mode.""" - return self.tuya_device.status[DPCode.MODE] + return self.device.status[DPCode.MODE] @property def percentage(self) -> int | None: @@ -171,24 +171,24 @@ class TuyaFanEntity(TuyaEntity, FanEntity): return 0 if ( - self.tuya_device.category == "kj" + self.device.category == "kj" and self.air_purifier_speed_range_len > 1 and not self.air_purifier_speed_range_enum - and DPCode.FAN_SPEED_ENUM in self.tuya_device.status + and DPCode.FAN_SPEED_ENUM in self.device.status ): # if air-purifier speed enumeration is supported we will prefer it. return ordered_list_item_to_percentage( self.air_purifier_speed_range_enum, - self.tuya_device.status[DPCode.FAN_SPEED_ENUM], + self.device.status[DPCode.FAN_SPEED_ENUM], ) # some type may not have the fan_speed_percent key - return self.tuya_device.status.get(DPCode.FAN_SPEED_PERCENT) + return self.device.status.get(DPCode.FAN_SPEED_PERCENT) @property def speed_count(self) -> int: """Return the number of speeds the fan supports.""" - if self.tuya_device.category == "kj": + if self.device.category == "kj": return self.air_purifier_speed_range_len return super().speed_count @@ -196,19 +196,19 @@ class TuyaFanEntity(TuyaEntity, FanEntity): def supported_features(self): """Flag supported features.""" supports = 0 - if DPCode.MODE in self.tuya_device.status: + if DPCode.MODE in self.device.status: supports |= SUPPORT_PRESET_MODE - if DPCode.FAN_SPEED_PERCENT in self.tuya_device.status: + if DPCode.FAN_SPEED_PERCENT in self.device.status: supports |= SUPPORT_SET_SPEED - if DPCode.SWITCH_HORIZONTAL in self.tuya_device.status: + if DPCode.SWITCH_HORIZONTAL in self.device.status: supports |= SUPPORT_OSCILLATE - if DPCode.FAN_DIRECTION in self.tuya_device.status: + if DPCode.FAN_DIRECTION in self.device.status: supports |= SUPPORT_DIRECTION # Air Purifier specific if ( - DPCode.SPEED in self.tuya_device.status - or DPCode.FAN_SPEED_ENUM in self.tuya_device.status + DPCode.SPEED in self.device.status + or DPCode.FAN_SPEED_ENUM in self.device.status ): supports |= SUPPORT_SET_SPEED return supports diff --git a/homeassistant/components/tuya/light.py b/homeassistant/components/tuya/light.py index 69e7020f56c..9758fdefc80 100644 --- a/homeassistant/components/tuya/light.py +++ b/homeassistant/components/tuya/light.py @@ -109,7 +109,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity): @property def is_on(self) -> bool: """Return true if light is on.""" - return self.tuya_device.status.get(DPCode.SWITCH_LED, False) + return self.device.status.get(DPCode.SWITCH_LED, False) def turn_on(self, **kwargs: Any) -> None: """Turn on or control the light.""" @@ -118,8 +118,8 @@ class TuyaLightEntity(TuyaEntity, LightEntity): _LOGGER.debug("light kwargs-> %s; work_mode %s", kwargs, work_mode) if ( - DPCode.LIGHT in self.tuya_device.status - and DPCode.SWITCH_LED not in self.tuya_device.status + DPCode.LIGHT in self.device.status + and DPCode.SWITCH_LED not in self.device.status ): commands += [{"code": DPCode.LIGHT, "value": True}] else: @@ -204,8 +204,8 @@ class TuyaLightEntity(TuyaEntity, LightEntity): def turn_off(self, **kwargs: Any) -> None: """Instruct the light to turn off.""" if ( - DPCode.LIGHT in self.tuya_device.status - and DPCode.SWITCH_LED not in self.tuya_device.status + DPCode.LIGHT in self.device.status + and DPCode.SWITCH_LED not in self.device.status ): commands = [{"code": DPCode.LIGHT, "value": False}] else: @@ -216,10 +216,10 @@ class TuyaLightEntity(TuyaEntity, LightEntity): def brightness(self) -> int | None: """Return the brightness of the light.""" old_range = self._tuya_brightness_range() - brightness = self.tuya_device.status.get(self.dp_code_bright, 0) + brightness = self.device.status.get(self.dp_code_bright, 0) if self._work_mode().startswith(WORK_MODE_COLOUR): - colour_json = self.tuya_device.status.get(self.dp_code_colour) + colour_json = self.device.status.get(self.dp_code_colour) if not colour_json: return None colour_data = json.loads(colour_json) @@ -230,9 +230,9 @@ class TuyaLightEntity(TuyaEntity, LightEntity): return int(self.remap(brightness, old_range[0], old_range[1], 0, 255)) def _tuya_brightness_range(self) -> tuple[int, int]: - if self.dp_code_bright not in self.tuya_device.status: + if self.dp_code_bright not in self.device.status: return 0, 255 - bright_item = self.tuya_device.function.get(self.dp_code_bright) + bright_item = self.device.function.get(self.dp_code_bright) if not bright_item: return 0, 255 bright_value = json.loads(bright_item.values) @@ -249,7 +249,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity): @property def hs_color(self) -> tuple[float, float] | None: """Return the hs_color of the light.""" - colour_json = self.tuya_device.status.get(self.dp_code_colour) + colour_json = self.device.status.get(self.dp_code_colour) if not colour_json: return None colour_data = json.loads(colour_json) @@ -266,7 +266,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity): def color_temp(self) -> int: """Return the color_temp of the light.""" new_range = self._tuya_temp_range() - tuya_color_temp = self.tuya_device.status.get(self.dp_code_temp, 0) + tuya_color_temp = self.device.status.get(self.dp_code_temp, 0) return ( self.max_mireds - self.remap( @@ -290,7 +290,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity): return MIREDS_MAX def _tuya_temp_range(self) -> tuple[int, int]: - temp_item = self.tuya_device.function.get(self.dp_code_temp) + temp_item = self.device.function.get(self.dp_code_temp) if not temp_item: return 0, 255 temp_value = json.loads(temp_item.values) @@ -312,13 +312,13 @@ class TuyaLightEntity(TuyaEntity, LightEntity): return 0, 255 def _tuya_hsv_function(self) -> dict[str, dict] | None: - hsv_item = self.tuya_device.function.get(self.dp_code_colour) + hsv_item = self.device.function.get(self.dp_code_colour) if not hsv_item: return None hsv_data = json.loads(hsv_item.values) if hsv_data: return hsv_data - colour_json = self.tuya_device.status.get(self.dp_code_colour) + colour_json = self.device.status.get(self.dp_code_colour) if not colour_json: return None colour_data = json.loads(colour_json) @@ -331,30 +331,30 @@ class TuyaLightEntity(TuyaEntity, LightEntity): return DEFAULT_HSV def _work_mode(self) -> str: - return self.tuya_device.status.get(DPCode.WORK_MODE, "") + return self.device.status.get(DPCode.WORK_MODE, "") def _get_hsv(self) -> dict[str, int]: if ( - self.dp_code_colour not in self.tuya_device.status - or len(self.tuya_device.status[self.dp_code_colour]) == 0 + self.dp_code_colour not in self.device.status + or len(self.device.status[self.dp_code_colour]) == 0 ): return {"h": 0, "s": 0, "v": 0} - return json.loads(self.tuya_device.status[self.dp_code_colour]) + return json.loads(self.device.status[self.dp_code_colour]) @property def supported_color_modes(self) -> set[str] | None: """Flag supported color modes.""" color_modes = [COLOR_MODE_ONOFF] - if self.dp_code_bright in self.tuya_device.status: + if self.dp_code_bright in self.device.status: color_modes.append(COLOR_MODE_BRIGHTNESS) - if self.dp_code_temp in self.tuya_device.status: + if self.dp_code_temp in self.device.status: color_modes.append(COLOR_MODE_COLOR_TEMP) if ( - self.dp_code_colour in self.tuya_device.status - and len(self.tuya_device.status[self.dp_code_colour]) > 0 + self.dp_code_colour in self.device.status + and len(self.device.status[self.dp_code_colour]) > 0 ): color_modes.append(COLOR_MODE_HS) return set(color_modes) diff --git a/homeassistant/components/tuya/number.py b/homeassistant/components/tuya/number.py index d048bc214b1..91ad3f0e6ba 100644 --- a/homeassistant/components/tuya/number.py +++ b/homeassistant/components/tuya/number.py @@ -121,7 +121,7 @@ class TuyaNumberEntity(TuyaEntity, NumberEntity): return None # Raw value - value = self.tuya_device.status.get(self.entity_description.key) + value = self.device.status.get(self.entity_description.key) # Scale integer/float value if value and isinstance(self._type_data, IntegerTypeData): diff --git a/homeassistant/components/tuya/select.py b/homeassistant/components/tuya/select.py index a71e3db5125..2b033757bc9 100644 --- a/homeassistant/components/tuya/select.py +++ b/homeassistant/components/tuya/select.py @@ -105,7 +105,7 @@ class TuyaSelectEntity(TuyaEntity, SelectEntity): def current_option(self) -> str | None: """Return the selected entity option to represent the entity state.""" # Raw value - value = self.tuya_device.status.get(self.entity_description.key) + value = self.device.status.get(self.entity_description.key) if value is None or value not in self._attr_options: return None diff --git a/homeassistant/components/tuya/sensor.py b/homeassistant/components/tuya/sensor.py index c56c9a851b4..8d36a9b1207 100644 --- a/homeassistant/components/tuya/sensor.py +++ b/homeassistant/components/tuya/sensor.py @@ -161,7 +161,7 @@ class TuyaSensorEntity(TuyaEntity, SensorEntity): return None # Raw value - value = self.tuya_device.status.get(self.entity_description.key) + value = self.device.status.get(self.entity_description.key) if value is None: return None diff --git a/homeassistant/components/tuya/switch.py b/homeassistant/components/tuya/switch.py index b001da78f9f..e00fea62c7a 100644 --- a/homeassistant/components/tuya/switch.py +++ b/homeassistant/components/tuya/switch.py @@ -312,7 +312,7 @@ class TuyaSwitchEntity(TuyaEntity, SwitchEntity): @property def is_on(self) -> bool: """Return true if switch is on.""" - return self.tuya_device.status.get(self.entity_description.key, False) + return self.device.status.get(self.entity_description.key, False) def turn_on(self, **kwargs: Any) -> None: """Turn the switch on."""