-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix] login problem [add] battery state [add] Air Purifier, Smart RGB Plug [update] tuya-iot-python-sdk to 0.3.1
- Loading branch information
Showing
11 changed files
with
211 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,9 +29,7 @@ | |
TUYA_DEVICE_MANAGER, | ||
TUYA_DISCOVERY_NEW, | ||
TUYA_HA_DEVICES, | ||
TUYA_HA_TUYA_MAP, | ||
TUYA_AIR_PURIFIER_TYPE, | ||
TUYA_FAN_TYPE, | ||
TUYA_HA_TUYA_MAP | ||
) | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
@@ -47,21 +45,23 @@ | |
|
||
# Air Purifier | ||
# https://developer.tuya.com/en/docs/iot/s?id=K9gf48r41mn81 | ||
DPCODE_AP_FAN_SPEED = "speed" | ||
DPCODE_AP_FAN_SPEED = "speed" | ||
DPCODE_AP_FAN_SPEED_ENUM = "fan_speed_enum" | ||
|
||
TUYA_SUPPORT_TYPE = { | ||
TUYA_FAN_TYPE, | ||
TUYA_AIR_PURIFIER_TYPE | ||
"fs", # Fan | ||
This comment has been minimized.
Sorry, something went wrong.
jpros
|
||
"kj", # Air Purifier | ||
} | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, _entry: ConfigEntry, async_add_entities | ||
): | ||
"""Set up tuya fan dynamically through tuya discovery.""" | ||
_LOGGER.info("fan init") | ||
|
||
hass.data[DOMAIN][TUYA_HA_TUYA_MAP].update({DEVICE_DOMAIN: TUYA_SUPPORT_TYPE}) | ||
hass.data[DOMAIN][TUYA_HA_TUYA_MAP].update( | ||
{DEVICE_DOMAIN: TUYA_SUPPORT_TYPE}) | ||
|
||
async def async_discover_device(dev_ids): | ||
"""Discover and add a discovered tuya fan.""" | ||
|
@@ -98,30 +98,33 @@ def _setup_entities(hass, device_ids: list): | |
|
||
class TuyaHaFan(TuyaHaDevice, FanEntity): | ||
"""Tuya Fan Device.""" | ||
|
||
def __init__(self, device: TuyaDevice, device_manager: TuyaDeviceManager): | ||
"""Init Tuya Fan Device.""" | ||
super().__init__(device, device_manager) | ||
|
||
# Air purifier fan can be controlled either via the ranged values or via the enum. | ||
# We will always prefer the enumeration if available | ||
# Enum is used for e.g. MEES SmartHIMOX-H06 | ||
# Range is used for e.g. Concept CA3000 | ||
self.air_purifier_speed_range = (0, 0) | ||
self.air_purifier_speed_range_len = 0 | ||
self.air_purifier_speed_range = (0, 0) | ||
self.air_purifier_speed_range_len = 0 | ||
self.air_purifier_speed_range_enum = [] | ||
if self.tuya_device.category == TUYA_AIR_PURIFIER_TYPE: | ||
if self.tuya_device.category == "kj": | ||
try: | ||
if DPCODE_AP_FAN_SPEED_ENUM in self.tuya_device.status: | ||
data = json.loads(self.tuya_device.function.get(DPCODE_AP_FAN_SPEED_ENUM, {}).values).get("range") | ||
data = json.loads(self.tuya_device.function.get( | ||
DPCODE_AP_FAN_SPEED_ENUM, {}).values).get("range") | ||
if data: | ||
self.air_purifier_speed_range = (1, len(data)) | ||
self.air_purifier_speed_range_len = len(data) | ||
self.air_purifier_speed_range_enum = data | ||
elif DPCODE_AP_FAN_SPEED in self.tuya_device.status: | ||
data = json.loads(self.tuya_device.function.get(DPCODE_AP_FAN_SPEED, {}).values).get("range") | ||
data = json.loads(self.tuya_device.function.get( | ||
DPCODE_AP_FAN_SPEED, {}).values).get("range") | ||
if data: | ||
self.air_purifier_speed_range = (int(data[0]), int(data[-1])) | ||
self.air_purifier_speed_range = ( | ||
int(data[0]), int(data[-1])) | ||
self.air_purifier_speed_range_len = len(data) | ||
except: | ||
_LOGGER.warn("Cannot parse the air-purifier speed range") | ||
|
@@ -132,17 +135,21 @@ def set_preset_mode(self, preset_mode: str) -> None: | |
|
||
def set_direction(self, direction: str) -> None: | ||
"""Set the direction of the fan.""" | ||
self._send_command([{"code": DPCODE_FAN_DIRECTION, "value": direction}]) | ||
|
||
self._send_command( | ||
[{"code": DPCODE_FAN_DIRECTION, "value": direction}]) | ||
|
||
def set_percentage(self, percentage: int) -> None: | ||
"""Set the speed of the fan, as a percentage.""" | ||
if self.tuya_device.category == TUYA_AIR_PURIFIER_TYPE: | ||
value_in_range = ceil(percentage_to_ranged_value(self.air_purifier_speed_range, percentage)) | ||
if self.tuya_device.category == "kj": | ||
value_in_range = ceil(percentage_to_ranged_value( | ||
self.air_purifier_speed_range, percentage)) | ||
if len(self.air_purifier_speed_range_enum): | ||
# if air-purifier speed enumeration is supported we will prefer it. | ||
self._send_command([{"code": DPCODE_AP_FAN_SPEED_ENUM, "value": str(self.air_purifier_speed_range_enum[value_in_range - 1])}]) | ||
self._send_command([{"code": DPCODE_AP_FAN_SPEED_ENUM, "value": str( | ||
self.air_purifier_speed_range_enum[value_in_range - 1])}]) | ||
else: | ||
self._send_command([{"code": DPCODE_AP_FAN_SPEED, "value": str(value_in_range)}]) | ||
self._send_command( | ||
[{"code": DPCODE_AP_FAN_SPEED, "value": str(value_in_range)}]) | ||
else: | ||
super().set_percentage(percentage) | ||
|
||
|
@@ -162,7 +169,8 @@ def turn_on( | |
|
||
def oscillate(self, oscillating: bool) -> None: | ||
"""Oscillate the fan.""" | ||
self._send_command([{"code": DPCODE_SWITCH_HORIZONTAL, "value": oscillating}]) | ||
self._send_command( | ||
[{"code": DPCODE_SWITCH_HORIZONTAL, "value": oscillating}]) | ||
|
||
# property | ||
@property | ||
|
@@ -206,29 +214,30 @@ def percentage(self) -> int: | |
"""Return the current speed.""" | ||
if not self.is_on: | ||
return 0 | ||
if self.tuya_device.category == TUYA_AIR_PURIFIER_TYPE: | ||
|
||
if self.tuya_device.category == "kj": | ||
if self.air_purifier_speed_range_len > 1: | ||
if len(self.air_purifier_speed_range_enum): | ||
# if air-purifier speed enumeration is supported we will prefer it. | ||
index = self.air_purifier_speed_range_enum.index( | ||
self.tuya_device.status.get(DPCODE_AP_FAN_SPEED_ENUM, 0) | ||
self.tuya_device.status.get( | ||
DPCODE_AP_FAN_SPEED_ENUM, 0) | ||
) | ||
return ranged_value_to_percentage(self.air_purifier_speed_range, | ||
index + 1 | ||
) | ||
index + 1 | ||
) | ||
else: | ||
return ranged_value_to_percentage(self.air_purifier_speed_range, | ||
int(self.tuya_device.status.get(DPCODE_AP_FAN_SPEED, 0) | ||
) | ||
) | ||
int(self.tuya_device.status.get(DPCODE_AP_FAN_SPEED, 0) | ||
) | ||
) | ||
else: | ||
return self.tuya_device.status.get(DPCODE_FAN_SPEED, 0) | ||
|
||
@property | ||
def speed_count(self) -> int: | ||
"""Return the number of speeds the fan supports.""" | ||
if self.tuya_device.category == TUYA_AIR_PURIFIER_TYPE: | ||
if self.tuya_device.category == "kj": | ||
return self.air_purifier_speed_range_len | ||
return super().speed_count() | ||
|
||
|
@@ -244,7 +253,7 @@ def supported_features(self): | |
supports = supports | SUPPORT_OSCILLATE | ||
if DPCODE_FAN_DIRECTION in self.tuya_device.status: | ||
supports = supports | SUPPORT_DIRECTION | ||
|
||
# Air Purifier specific | ||
if DPCODE_AP_FAN_SPEED in self.tuya_device.status or DPCODE_AP_FAN_SPEED_ENUM in self.tuya_device.status: | ||
supports = supports | SUPPORT_SET_SPEED | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Typo in "ALARM"