Guard against empty Tuya data types (#65571)

This commit is contained in:
Franck Nijhof 2022-02-03 21:46:05 +01:00 committed by GitHub
parent 80102e1e84
commit 445c47c7a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -72,9 +72,11 @@ class IntegerTypeData:
return remap_value(value, from_min, from_max, self.min, self.max, reverse) return remap_value(value, from_min, from_max, self.min, self.max, reverse)
@classmethod @classmethod
def from_json(cls, dpcode: DPCode, data: str) -> IntegerTypeData: def from_json(cls, dpcode: DPCode, data: str) -> IntegerTypeData | None:
"""Load JSON string and return a IntegerTypeData object.""" """Load JSON string and return a IntegerTypeData object."""
parsed = json.loads(data) if not (parsed := json.loads(data)):
return None
return cls( return cls(
dpcode, dpcode,
min=int(parsed["min"]), min=int(parsed["min"]),
@ -94,9 +96,11 @@ class EnumTypeData:
range: list[str] range: list[str]
@classmethod @classmethod
def from_json(cls, dpcode: DPCode, data: str) -> EnumTypeData: def from_json(cls, dpcode: DPCode, data: str) -> EnumTypeData | None:
"""Load JSON string and return a EnumTypeData object.""" """Load JSON string and return a EnumTypeData object."""
return cls(dpcode, **json.loads(data)) if not (parsed := json.loads(data)):
return None
return cls(dpcode, **parsed)
@dataclass @dataclass
@ -222,17 +226,25 @@ class TuyaEntity(Entity):
dptype == DPType.ENUM dptype == DPType.ENUM
and getattr(self.device, key)[dpcode].type == DPType.ENUM and getattr(self.device, key)[dpcode].type == DPType.ENUM
): ):
return EnumTypeData.from_json( if not (
dpcode, getattr(self.device, key)[dpcode].values enum_type := EnumTypeData.from_json(
) dpcode, getattr(self.device, key)[dpcode].values
)
):
continue
return enum_type
if ( if (
dptype == DPType.INTEGER dptype == DPType.INTEGER
and getattr(self.device, key)[dpcode].type == DPType.INTEGER and getattr(self.device, key)[dpcode].type == DPType.INTEGER
): ):
return IntegerTypeData.from_json( if not (
dpcode, getattr(self.device, key)[dpcode].values integer_type := IntegerTypeData.from_json(
) dpcode, getattr(self.device, key)[dpcode].values
)
):
continue
return integer_type
if dptype not in (DPType.ENUM, DPType.INTEGER): if dptype not in (DPType.ENUM, DPType.INTEGER):
return dpcode return dpcode