Skip to content

Commit

Permalink
add blinds
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 committed Aug 11, 2024
1 parent 8a8a4ba commit 70b2344
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
140 changes: 140 additions & 0 deletions ph4ha/apps/blinds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import json

import hassapi as hass
import requests


class Blinds(hass.Hass):
"""
TODO: collect manual state changes. manual state change cancels the next routine
TODO: add pause automation toggle
TODO: add sync - sets state appropriate for this time of a day
"""

BLIND_LIV_BIG = "LivBig"
BLIND_LIV_DOOR = "LivDoor"
BLIND_BEDROOM = "Bedroom"
BLIND_STUDY = "Study"
BLIND_SKLAD = "Sklad"
OPEN_HALF = 1

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.blinds = None
self.weekdays_open_time = None

def initialize(self):
self.blinds = {x["name"]: x for x in self.args["blinds"]}
self.weekdays_open_time = None # self.get_state(self.args["weekdays_open_time"])

self.listen_event(self.scene_activated, "call_service", domain="scene", service="turn_on")

# # Schedule for weekdays
# self.run_daily(self.open_blinds, time(hour=7, minute=30), weekdays="mon,tue,wed,thu,fri")
#
# # Schedule for weekends
# self.run_daily(self.open_blinds_weekends, time(hour=11, minute=0), weekdays="sat,sun")
#
# # Schedule to raise blinds at 18:00 every day
# self.run_daily(self.raise_blinds, time(hour=18, minute=0))
#
# # Schedule to lower blinds at 22:00 every day
# self.run_daily(self.lower_blinds, time(hour=22, minute=0))
#
# # Schedule to lower and tilt blinds 1 hour after sunset
# self.run_at_sunset(self.lower_and_tilt_blinds, offset=3600) # offset in seconds

self.log("initialized")

def scene_activated(self, event_name, data, kwargs):
# Extract the scene ID or entity ID
scene_id = data.get("service_data", {}).get("entity_id")
self.log(f"scene_activated: {scene_id}")

if scene_id == "scene.blinds_vent":
self.log("Scene: blinds_vent")
self.handle_vent()
elif scene_id == "scene.blinds_living_morning":
self.log("Scene: blinds_living_morning")
self.blinds_living_morning()
elif scene_id == "scene.blinds_living_morning_hot":
self.log("Scene: blinds_living_morning_hot")
self.blinds_living_morning_hot()
elif scene_id == "scene.blinds_living_privacy":
self.log("Scene: blinds_living_privacy")
self.blinds_living_privacy()
elif scene_id == "scene.blinds_all_up":
self.log("Scene: blinds_all_up")
self.blinds_all_up()
elif scene_id == "scene.blinds_all_down":
self.log("Scene: blinds_all_down")
self.blinds_all_down()
elif scene_id == "scene.blinds_tilt_open":
self.log("Scene: blinds_tilt_open")
self.blinds_tilt_open()
elif scene_id == "scene.blinds_tilt_close":
self.log("Scene: blinds_tilt_close")
self.blinds_tilt_close()

def handle_vent(self):
self.log("Vent started")
self.blinds_pos_tilt(self.BLIND_LIV_DOOR, 0, self.OPEN_HALF)
self.blinds_pos_tilt(self.BLIND_BEDROOM, 0, self.OPEN_HALF)
self.log("Vent ended")

def blinds_living_morning(self):
self.blinds_pos_tilt(self.BLIND_LIV_BIG, 35, self.OPEN_HALF)

def blinds_living_morning_hot(self):
self.blinds_pos_tilt(self.BLIND_LIV_BIG, 35, 0.2)

def blinds_living_privacy(self):
self.blinds_pos_tilt(self.BLIND_LIV_BIG, 30, 0.1)

def blinds_all_up(self):
self.blinds_pos_tilt(self.BLIND_LIV_BIG, 100, 0)
self.blinds_pos_tilt(self.BLIND_LIV_DOOR, 100, 0)
self.blinds_pos_tilt(self.BLIND_BEDROOM, 100, 0)
self.blinds_pos_tilt(self.BLIND_SKLAD, 100, 0)
self.blinds_pos_tilt(self.BLIND_STUDY, 100, 0)

def blinds_all_down(self):
self.blinds_pos_tilt(self.BLIND_LIV_BIG, 0, 0)
self.blinds_pos_tilt(self.BLIND_LIV_DOOR, 0, 0)
self.blinds_pos_tilt(self.BLIND_BEDROOM, 0, 0)
self.blinds_pos_tilt(self.BLIND_SKLAD, 0, 0)
self.blinds_pos_tilt(self.BLIND_STUDY, 0, 0)

def blinds_tilt_open(self):
self.blinds_tilt(self.BLIND_LIV_BIG, self.OPEN_HALF)
self.blinds_tilt(self.BLIND_LIV_DOOR, self.OPEN_HALF)
self.blinds_tilt(self.BLIND_BEDROOM, self.OPEN_HALF)
self.blinds_tilt(self.BLIND_SKLAD, self.OPEN_HALF)
self.blinds_tilt(self.BLIND_STUDY, self.OPEN_HALF)

def blinds_tilt_close(self):
self.blinds_tilt(self.BLIND_LIV_BIG, 0)
self.blinds_tilt(self.BLIND_LIV_DOOR, 0)
self.blinds_tilt(self.BLIND_BEDROOM, 0)
self.blinds_tilt(self.BLIND_SKLAD, 0)
self.blinds_tilt(self.BLIND_STUDY, 0)

def blinds_pos_tilt(self, blind, pos, tilt):
blind_rec = self.blinds[blind]
blind_host = blind_rec["ip_address"]

data = {"id": 1, "method": "Script.Eval", "params": {"id": 1, "code": f"posAndTilt({pos}, {tilt})"}}
response = requests.post(
f"http://{blind_host}/rpc", data=json.dumps(data), headers={"Content-Type": "application/json"}
)
self.log(f"Req: {blind_host}, data: {json.dumps(data)}, response: {response}")

def blinds_tilt(self, blind, tilt):
blind_rec = self.blinds[blind]
blind_host = blind_rec["ip_address"]

data = {"id": 1, "method": "Script.Eval", "params": {"id": 1, "code": f"tilt({tilt})"}}
response = requests.post(
f"http://{blind_host}/rpc", data=json.dumps(data), headers={"Content-Type": "application/json"}
)
self.log(f"Req: {blind_host}, data: {json.dumps(data)}, response: {response}")
42 changes: 42 additions & 0 deletions ph4ha/config-ha/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,45 @@ switch:
entity_id: input_text.hallway_switch_state
data:
value: "off"

scene:
- id: 'scene-blinds-vent'
name: Blinds vent
entities: { }
icon: mdi:blinds
metadata: { }
- id: 'scene-blinds-living-morning'
name: Blinds Living Morning
entities: { }
icon: mdi:blinds
metadata: { }
- id: 'scene-blinds-living-morning-hot'
name: Blinds Living Morning Hot
entities: { }
icon: mdi:blinds
metadata: { }
- id: 'scene-blinds-living-privacy'
name: Blinds Living Privacy
entities: { }
icon: mdi:blinds
metadata: { }
- id: 'scene-blinds-all-up'
name: Blinds All Up
entities: { }
icon: mdi:blinds
metadata: { }
- id: 'scene-blinds-all-down'
name: Blinds All Down
entities: { }
icon: mdi:blinds
metadata: { }
- id: 'scene-blinds-tilt-open'
name: Blinds Tilt Open
entities: { }
icon: mdi:blinds
metadata: { }
- id: 'scene-blinds-tilt-close'
name: Blinds Tilt Close
entities: { }
icon: mdi:blinds
metadata: { }

0 comments on commit 70b2344

Please sign in to comment.