Guard against empty Tuya data types (#65571)

pull/65584/head
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
1 changed files with 22 additions and 10 deletions

View File

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