-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhome_automation.py
108 lines (95 loc) · 3.49 KB
/
home_automation.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import json
import speech_recognition as sr
class Appliance:
def __init__(self, name):
self.name = name
self.state = "off"
def turn_on(self):
self.state = "on"
def turn_off(self):
self.state = "off"
def __repr__(self):
return f"{self.name} is {self.state}"
class HomeAutomation:
def __init__(self):
self.appliances = {
"light": Appliance("light"),
"fan": Appliance("fan"),
"tv": Appliance("tv")
}
def control_appliance(self, command):
words = command.split()
if "turn" in words and "on" in words:
appliance_name = words[-1]
if appliance_name in self.appliances:
self.appliances[appliance_name].turn_on()
else:
print("Appliance not found.")
elif "turn" in words and "off" in words:
appliance_name = words[-1]
if appliance_name in self.appliances:
self.appliances[appliance_name].turn_off()
else:
print("Appliance not found.")
elif "status" in words:
appliance_name = words[-1]
if appliance_name in self.appliances:
print(self.appliances[appliance_name])
else:
print("Appliance not found.")
else:
print("Command not recognized.")
def save_to_file(self, filename='appliances.json'):
data = {name: appliance.state for name, appliance in self.appliances.items()}
with open(filename, 'w') as f:
json.dump(data, f)
print("Appliance states saved to file.")
def load_from_file(self, filename='appliances.json'):
try:
with open(filename, 'r') as f:
data = json.load(f)
for name, state in data.items():
if name in self.appliances:
self.appliances[name].state = state
print("Appliance states loaded from file.")
except FileNotFoundError:
print("File not found. Starting with default appliance states.")
def recognize_speech():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = recognizer.listen(source)
try:
command = recognizer.recognize_google(audio)
print(f"You said: {command}")
return command.lower()
except sr.UnknownValueError:
print("Sorry, I did not understand that.")
return None
except sr.RequestError:
print("Sorry, my speech service is down.")
return None
def main():
home_automation = HomeAutomation()
home_automation.load_from_file()
while True:
print("\nVoice-Controlled Home Automation")
print("1. Issue Voice Command")
print("2. Save Appliance States to File")
print("3. Load Appliance States from File")
print("4. Exit")
choice = input("Choose an option: ")
if choice == '1':
command = recognize_speech()
if command:
home_automation.control_appliance(command)
elif choice == '2':
home_automation.save_to_file()
elif choice == '3':
home_automation.load_from_file()
elif choice == '4':
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()