Add scene buttons

This commit is contained in:
Correl Roush 2023-10-05 18:51:48 -04:00
parent 0edcc7aefd
commit 51a5028ea9
2 changed files with 44 additions and 3 deletions

33
main.py
View file

@ -32,8 +32,8 @@ state = StateTree(
)
rotary = RotaryIRQ(
D4,
D5,
D2,
D1,
0,
max_val=128,
range_mode=RotaryIRQ.RANGE_UNBOUNDED,
@ -41,9 +41,14 @@ rotary = RotaryIRQ(
incr=5,
)
rotary_button = Button(Pin(D1, Pin.IN, Pin.PULL_UP), inverted=True)
rotary_button = Button(Pin(D3, Pin.IN, Pin.PULL_UP), inverted=True)
rotary_value = rotary.value()
buttons = [
Button(Pin(D5, Pin.IN, Pin.PULL_UP), inverted=True),
Button(Pin(D6, Pin.IN, Pin.PULL_UP), inverted=True),
Button(Pin(D7, Pin.IN, Pin.PULL_UP), inverted=True),
]
with open("settings.json", "r") as f:
settings = json.load(f)
@ -97,6 +102,13 @@ class HomeAssistant:
json={"entity_id": entity_id, "brightness_step": value},
)
def activate_scene(self, entity_id: str) -> None:
response = requests.post(
f"{self._url}/api/services/scene/turn_on",
headers={"Authorization": f"Bearer {self._api_token}"},
json={"entity_id": entity_id},
)
hass = HomeAssistant(
url=settings["home-assistant"]["url"],
@ -109,6 +121,7 @@ def loop():
global hass
global mqtt, last_mqtt_attempt
global rotary, rotary_button, rotary_value
global buttons
rotary_button.update()
if rotary_button.was_clicked():
@ -123,6 +136,20 @@ def loop():
hass.adjust_light("light.key_lights", change)
rotary_value = new_value
for i, button in enumerate(buttons):
button.update()
if button.was_clicked():
print(f"Pressed button {i}")
try:
scene = settings["scenes"][i]
print(f"Activating scene {scene}")
hass.activate_scene(scene)
except KeyError:
print("No scenes defined")
except IndexError:
print(f"No scene defined for button {i}")
if not sta_if.active():
print("Connecting to WiFi")
sta_if.active(True)

14
settings.json Normal file
View file

@ -0,0 +1,14 @@
{
"wifi": {
"ssid": "FiOS-IJBVH",
"password": "job5638pit5614iced"
},
"mqtt": {
"broker": "192.168.1.183",
"prefix": "desk-controls"
},
"home-assistant": {
"url": "http://192.168.1.183:8123",
"api_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI3MGIyZjk0MzZkZDk0ZjNlYTRhMWVlZGRlZWMzMTU5NCIsImlhdCI6MTY5NDUzNjY3NywiZXhwIjoyMDA5ODk2Njc3fQ.uetwuZ9_Szbnuf3vbKTLclBiF24YXLHOiDBswgT8drA"
}
}