Skip to content

Commit

Permalink
Fixes for climate and auto configure. #216
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
xZetsubou committed May 3, 2024
1 parent 7d7535a commit 5880f8f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
8 changes: 5 additions & 3 deletions custom_components/localtuya/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)

Expand Down
14 changes: 11 additions & 3 deletions custom_components/localtuya/core/ha_entities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions custom_components/localtuya/core/ha_entities/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions custom_components/localtuya/core/ha_entities/climates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
Expand Down

0 comments on commit 5880f8f

Please sign in to comment.