Skip to content

Commit

Permalink
refactor state
Browse files Browse the repository at this point in the history
  • Loading branch information
vladak committed Nov 21, 2023
1 parent 1467599 commit 75143e5
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ def message(client, topic, message):
device.last_time = time.monotonic()


def get_device_state(device_info, args):
"""
return device state based on power consumption
"""
state = "N/A"
# if not updated for X seconds, consider the state as unknown.
if time.monotonic() - device_info.last_time < args.timeout:
if device_info.power > args.threshold:
state = "on"
else:
state = "off"

return state


# pylint: disable=too-many-statements,too-many-locals
def main():
"""
Expand Down Expand Up @@ -182,14 +197,7 @@ def main():

for device_name, device_info in devices.items():
logger.debug(f"{device_info}")
state = "N/A"
# if not updated for X seconds, consider the state as unknown.
if time.monotonic() - device_info.last_time < args.timeout:
if device_info.power > args.threshold:
state = "on"
else:
state = "off"
logger.info(f"{device_name} = {state}")
logger.info(f"{device_name} = {get_device_state(device_info, args)}")


if __name__ == "__main__":
Expand Down

0 comments on commit 75143e5

Please sign in to comment.