Skip to content

Commit

Permalink
Fix for Ecobee Switch+ devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Jc2k committed Mar 29, 2020
1 parent bc8e2aa commit a2c3051
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions aiohomekit/controller/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@ def check_convert_value(val: str, target_type: str) -> int:
:return: the converted value
:raises FormatError: if the input value could not be converted to the target type
"""

if target_type == CharacteristicFormats.bool:
try:
val = strtobool(str(val))
except ValueError:
raise FormatError('"{v}" is no valid "{t}"!'.format(v=val, t=target_type))

# We have seen iPhone's sending 1 and 0 for True and False
# This is in spec
# It is also *required* for Ecobee Switch+ devices (as at Mar 2020)
return 1 if val else 0

if target_type in [
CharacteristicFormats.uint64,
CharacteristicFormats.uint32,
Expand All @@ -48,20 +55,24 @@ def check_convert_value(val: str, target_type: str) -> int:
val = int(val)
except ValueError:
raise FormatError('"{v}" is no valid "{t}"!'.format(v=val, t=target_type))

if target_type == CharacteristicFormats.float:
try:
val = float(val)
except ValueError:
raise FormatError('"{v}" is no valid "{t}"!'.format(v=val, t=target_type))

if target_type == CharacteristicFormats.data:
try:
base64.decodebytes(val.encode())
except binascii.Error:
raise FormatError('"{v}" is no valid "{t}"!'.format(v=val, t=target_type))

if target_type == CharacteristicFormats.tlv8:
try:
tmp_bytes = base64.decodebytes(val.encode())
TLV.decode_bytes(tmp_bytes)
except (binascii.Error, TlvParseException):
raise FormatError('"{v}" is no valid "{t}"!'.format(v=val, t=target_type))

return val

0 comments on commit a2c3051

Please sign in to comment.