From 5880f8f3f5d15e0991cabf9e9a08db9957cecc49 Mon Sep 17 00:00:00 2001 From: xZetsubou Date: Fri, 3 May 2024 07:11:52 +0300 Subject: [PATCH] Fixes for climate and auto configure. #216 * Added scale option in cloud_value * force to int if the float point is .0 * Fix set_temperture if set_temp forced to celsius. * Auto configure now will scale min/max for climates. * Removed dev logs. --- custom_components/localtuya/climate.py | 8 +++++--- .../localtuya/core/ha_entities/__init__.py | 14 +++++++++++--- .../localtuya/core/ha_entities/base.py | 2 ++ .../localtuya/core/ha_entities/climates.py | 8 ++++++-- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/custom_components/localtuya/climate.py b/custom_components/localtuya/climate.py index a6cf36e3..8b57339d 100644 --- a/custom_components/localtuya/climate.py +++ b/custom_components/localtuya/climate.py @@ -390,11 +390,12 @@ def fan_modes(self) -> list: async def async_set_temperature(self, **kwargs): """Set new target temperature.""" + if self._target_temp_forced_to_celsius: + # Revert temperture to Fahrenheit it was forced to celsius + temperature = round((temperature - 32) * 5 / 9) + if ATTR_TEMPERATURE in kwargs and self.has_config(CONF_TARGET_TEMPERATURE_DP): temperature = round(kwargs[ATTR_TEMPERATURE] / self._precision_target) - if self._target_temp_forced_to_celsius: - # Revert temperture to Fahrenheit it was forced to celsius - temperature = round((temperature * 1.8) + 32) await self._device.set_dp( temperature, self._config[CONF_TARGET_TEMPERATURE_DP] @@ -457,6 +458,7 @@ def status_updated(self): if target_temp != self._target_temperature: self._target_temp_forced_to_celsius = True self._target_temperature = target_temp + self._min_temp = round((self._min_temp - 32) * 5 / 9) self._max_temp = round((self._max_temp - 32) * 5 / 9) diff --git a/custom_components/localtuya/core/ha_entities/__init__.py b/custom_components/localtuya/core/ha_entities/__init__.py index 046b2526..fa563dad 100644 --- a/custom_components/localtuya/core/ha_entities/__init__.py +++ b/custom_components/localtuya/core/ha_entities/__init__.py @@ -127,7 +127,6 @@ def gen_localtuya_entities(localtuya_data: dict, tuya_category: str) -> list[dic continue if code and code.lower() in dp_data.split(): - _LOGGER.debug(f"Added!!!!: {dp_data}: code: {code}") entity[k] = dp_id # Pull dp values from cloud. still unsure to apply this to all. @@ -167,7 +166,7 @@ def gen_localtuya_entities(localtuya_data: dict, tuya_category: str) -> list[dic # convert to list of configs list_entities = [entities.get(id) for id in sorted_ids] - _LOGGER.debug(f"{device_name}: Entities configured: {list_entities}") + _LOGGER.debug(f"{device_name}: Configured entities: {list_entities}") # return [] return list_entities @@ -214,6 +213,12 @@ def get_dp_values(dp: str, dps_data: dict, req_info: CLOUD_VALUE = None) -> dict pref_type = req_info.prefer_type if valid_type else float dp_values["scale"] = pref_type(scale(1, val_scale, float)) + # Scale if requested. + if req_info.scale: + for v in ("min", "max", "step"): + value = dp_values[v] + dp_values[v] = pref_type(scale(value, val_scale)) + return dp_values # ENUM Values: range: list of values. @@ -233,7 +238,10 @@ def get_dp_values(dp: str, dps_data: dict, req_info: CLOUD_VALUE = None) -> dict def scale(value: int, scale: int, _type: type = int) -> float: """Return scaled value.""" - return _type(value) / (10**scale) + value = _type(value) / (10**scale) + if value.is_integer(): + value = int(value) + return value def convert_list(_list: list, req_info: CLOUD_VALUE = str): diff --git a/custom_components/localtuya/core/ha_entities/base.py b/custom_components/localtuya/core/ha_entities/base.py index 3a6c50f2..9b0095a1 100644 --- a/custom_components/localtuya/core/ha_entities/base.py +++ b/custom_components/localtuya/core/ha_entities/base.py @@ -26,6 +26,7 @@ class CLOUD_VALUE: Enums: convert the values to [dict or str splitted by comma, default is list].\n `remap_values(dict)`: Used to remap dict values, if prefer_type is dict.\n `reverse_dict(bool)`: Reverse dict keys, value, if prefer_type is dict.\n + `scale(bool)`: For integers, scale final value.\n """ default_value: Any @@ -34,6 +35,7 @@ class CLOUD_VALUE: prefer_type: type = None remap_values: dict[str, Any] = field(default_factory=dict) reverse_dict: bool = False + scale: bool = False class LocalTuyaEntity: diff --git a/custom_components/localtuya/core/ha_entities/climates.py b/custom_components/localtuya/core/ha_entities/climates.py index db3d30cc..921cd054 100644 --- a/custom_components/localtuya/core/ha_entities/climates.py +++ b/custom_components/localtuya/core/ha_entities/climates.py @@ -62,8 +62,12 @@ def localtuya_climate( CONF_HVAC_MODE_SET: CLOUD_VALUE( hvac_mode_set, CONF_HVAC_MODE_DP, "range", dict, MAP_CLIMATE_MODES, True ), - CONF_MIN_TEMP: CLOUD_VALUE(min_temperature, CONF_TARGET_TEMPERATURE_DP, "min"), - CONF_MAX_TEMP: CLOUD_VALUE(max_temperature, CONF_TARGET_TEMPERATURE_DP, "max"), + CONF_MIN_TEMP: CLOUD_VALUE( + min_temperature, CONF_TARGET_TEMPERATURE_DP, "min", scale=True + ), + CONF_MAX_TEMP: CLOUD_VALUE( + max_temperature, CONF_TARGET_TEMPERATURE_DP, "max", scale=True + ), CONF_TEMPERATURE_STEP: CLOUD_VALUE( str(temp_step), CONF_TARGET_TEMPERATURE_DP, "step", str ),