Refactor Tuya light platform (#57980)

This commit is contained in:
Franck Nijhof 2021-10-18 20:35:01 +02:00 committed by GitHub
parent b01170d917
commit be73b21f81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 473 additions and 278 deletions

View file

@ -22,9 +22,9 @@ class IntegerTypeData:
min: int
max: int
unit: str
scale: float
step: float
unit: str | None = None
@property
def max_scaled(self) -> float:
@ -45,6 +45,32 @@ class IntegerTypeData:
"""Scale a value."""
return value * 1.0 / (10 ** self.scale)
def remap_value_to(
self,
value: float,
to_min: float | int = 0,
to_max: float | int = 255,
reverse: bool = False,
) -> float:
"""Remap a value from this range to a new range."""
if reverse:
value = self.max - value + self.min
return ((value - self.min) / (self.max - self.min)) * (to_max - to_min) + to_min
def remap_value_from(
self,
value: float,
from_min: float | int = 0,
from_max: float | int = 255,
reverse: bool = False,
) -> float:
"""Remap a value from its current range to this range."""
if reverse:
value = from_max - value + from_min
return ((value - from_min) / (from_max - from_min)) * (
self.max - self.min
) + self.min
@classmethod
def from_json(cls, data: str) -> IntegerTypeData:
"""Load JSON string and return a IntegerTypeData object."""