Smarthome Functionen
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

module.py 1.3KB

1234567891011121314151617181920212223242526272829303132333435
  1. import json
  2. from mqtt import mqtt_client
  3. import time
  4. TEST_CLIENT_ID = "__test_device_tester__"
  5. mqtt_test_client = mqtt_client(TEST_CLIENT_ID, "localhost")
  6. def init_state(all_state_keys, device):
  7. for state_topic in all_state_keys:
  8. assert device.get(state_topic, 0) == None
  9. def state_change_by_mqtt(all_state_keys, num_states, mqtt_test_client, base_topic, device, mqtt_data, state_data, warning_condition, mqtt_signal_time):
  10. tm_warning = None
  11. for i in range(num_states):
  12. for state_topic in all_state_keys:
  13. if device.TX_TYPE == device.TX_VALUE:
  14. data = json.dumps(mqtt_data(state_topic)[i])
  15. mqtt_test_client.send(base_topic + '/' + state_topic, data)
  16. elif device.TX_TYPE == device.TX_DICT:
  17. mqtt_test_client.send(base_topic, json.dumps({state_topic: mqtt_data(state_topic)[i]}))
  18. else:
  19. raise TypeError("Unknown TX_TYPE for device.")
  20. if callable(warning_condition):
  21. if warning_condition(state_topic, mqtt_data(state_topic)[i]):
  22. tm_warning = int(time.time())
  23. time.sleep(mqtt_signal_time)
  24. for state_topic in all_state_keys:
  25. assert device.get(state_topic) == state_data(state_topic)[i]
  26. return tm_warning