Skip to content

Commit

Permalink
Fix missing value cast in Tuya
Browse files Browse the repository at this point in the history
Sometimes, the base functions are called by providing a string instead of a float. In these case, the computation done returns with an exception.

This code fixes that
  • Loading branch information
azerty9971 committed Jun 20, 2024
1 parent 89467e8 commit 2e82676
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions homeassistant/components/tuya/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ def step_scaled(self) -> float:

def scale_value(self, value: float) -> float:
"""Scale a value."""
if not isinstance(value, float):
value = float(value)
return value / (10**self.scale)

def scale_value_back(self, value: float) -> int:
"""Return raw value for scaled."""
if not isinstance(value, float):
value = float(value)
return int(value * (10**self.scale))

def remap_value_to(
Expand All @@ -61,6 +65,8 @@ def remap_value_to(
reverse: bool = False,
) -> float:
"""Remap a value from this range to a new range."""
if not isinstance(value, float):
value = float(value)
return remap_value(value, self.min, self.max, to_min, to_max, reverse)

def remap_value_from(
Expand All @@ -71,6 +77,8 @@ def remap_value_from(
reverse: bool = False,
) -> float:
"""Remap a value from its current range to this range."""
if not isinstance(value, float):
value = float(value)
return remap_value(value, from_min, from_max, self.min, self.max, reverse)

@classmethod
Expand Down

0 comments on commit 2e82676

Please sign in to comment.