feat: format value, then evaluate by expression, and set precision at last (#1516)

This commit is contained in:
Li Shuzhen
2025-11-25 09:31:40 +08:00
committed by GitHub
parent 1b87381f43
commit e6750bb746
3 changed files with 26 additions and 9 deletions

View File

@@ -599,15 +599,25 @@ class MIoTSpecProperty(_MIoTSpecBase):
def value_format(self, value: Any) -> Any:
if value is None:
return None
if isinstance(value, str):
if self.format_ == int:
value = int(float(value))
elif self.format_ == float:
value = float(value)
if self.format_ == bool:
return bool(value in [True, 1, 'True', 'true', '1'])
return value
def value_precision(self, value: Any) -> Any:
if value is None:
return None
if self.format_ == float:
return round(value, self.precision)
if self.format_ == int:
if self.value_range is None:
return int(round(value))
return int(
round(value / self.value_range.step) * self.value_range.step)
if self.format_ == float:
return round(value, self.precision)
if self.format_ == bool:
return bool(value in [True, 1, 'True', 'true', '1'])
return value
def dump(self) -> dict: