Skip to content

Commit

Permalink
Filter unsupported features and convert sensor data to float
Browse files Browse the repository at this point in the history
  • Loading branch information
felipediel committed Jan 23, 2021
1 parent 69afcd7 commit 5ebfb1f
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions broadlink/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,18 @@ def __init__(self, *args, **kwargs) -> None:
device.__init__(self, *args, **kwargs)
self.type = "SP4B"

def get_state(self) -> dict:
"""Get full state of device."""
state = super().get_state()

# Convert sensor data to float. Remove keys if sensors are not supported.
sensor_attrs = ["current", "volt", "power", "totalconsum", "overload"]
for attr in sensor_attrs:
value = state.pop(attr, -1)
if value != -1:
state[attr] = value / 1000
return state

def _encode(self, flag: int, state: dict) -> bytes:
"""Encode a message."""
payload = json.dumps(state, separators=(",", ":")).encode()
Expand Down

0 comments on commit 5ebfb1f

Please sign in to comment.