-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsample_fan.py
More file actions
executable file
·51 lines (38 loc) · 1.29 KB
/
Copy pathsample_fan.py
File metadata and controls
executable file
·51 lines (38 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
"""Example of the most simple usage of a Fan entity.
The Fan has five speeds.
"""
from ham import MqttManager
from ham.fan import PercentageOptimisticFan
from time import sleep
import os
MQTT_USERNAME = os.environ["MQTT_USERNAME"]
MQTT_PASSWORD = os.environ["MQTT_PASSWORD"]
MQTT_HOST = os.environ["MQTT_HOST"]
class SampleFan(PercentageOptimisticFan):
name = "Awesomest Fan"
short_id = "moreawesome"
speed_range_min = 1
speed_range_max = 5
def callback(self, state: bool):
super().callback(state)
print("The fan is set to %s" % ("on" if state else "off",))
def speed_callback(self, speed: int):
super().speed_callback(speed)
print("The fan is set to %s (received: %s)" % (self.speed, speed))
def set_speed(self, speed: int):
print("Setting value. Previous state: %d. New state: %d" % (self.speed, speed))
self.speed = speed
if __name__ == "__main__":
main_fan = SampleFan()
manager = MqttManager(MQTT_HOST, username=MQTT_USERNAME, password=MQTT_PASSWORD)
manager.add_thing(main_fan)
manager.start()
a = 1
print("Entering an infinite loop, Ctrl+C multiple times to exit.")
while True:
sleep(5)
if a > 5:
a = 0
main_fan.set_speed(a)
a += 1