Skip to content

Commit

Permalink
ha - handle array of scenes, add automations
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 committed Aug 17, 2024
1 parent 64b2cc6 commit 07f2161
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
56 changes: 56 additions & 0 deletions ph4ha/apps/blinds.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from typing import Optional

import hassapi as hass
import requests
Expand All @@ -16,28 +17,40 @@ class Blinds(hass.Hass):
BLIND_BEDROOM = "Bedroom"
BLIND_STUDY = "Study"
BLIND_SKLAD = "Sklad"
ALL_BLINDS = [BLIND_LIV_BIG, BLIND_LIV_DOOR, BLIND_BEDROOM, BLIND_STUDY, BLIND_SKLAD]
OPEN_HALF = 0.9

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.blinds = None
self.weekdays_open_time = None
self.guest_weekdays_open_time = None
self.guest_mode: bool = False
self.automation_enabled: bool = True

self.field_weekdays_open_time = None
self.field_guest_mode = None
self.field_automation_enabled = None
self.field_guest_weekdays_open_time = None

def initialize(self):
self.blinds = {x["name"]: x for x in self.args["blinds"]}
self.field_weekdays_open_time = self.args["weekdays_open_time"]
self.field_guest_weekdays_open_time = self.args["guest_weekdays_open_time_input"]
self.field_guest_mode = self.args["guest_mode_input"]
self.field_automation_enabled = self.args["automation_enabled_input"]

# Set initial blind open time
self.update_blind_open_time()
self.update_blind_open_time_guest()
self.update_blind_guest_mode()
self.update_blind_automation_enabled()

# Listen for changes to the input_datetime entity
self.listen_state(self.update_blind_open_time, self.field_weekdays_open_time)
self.listen_state(self.update_blind_open_time_guest, self.field_guest_weekdays_open_time)
self.listen_state(self.update_blind_guest_mode, self.field_guest_mode)
self.listen_state(self.update_blind_automation_enabled, self.field_automation_enabled)

# Listen to scene changes
self.listen_event(self.scene_activated, "call_service", domain="scene", service="turn_on")
Expand Down Expand Up @@ -76,16 +89,35 @@ def update_blind_open_time(self, entity=None, attribute=None, old=None, new=None
# Schedule the blind open event at the new time
# self.run_daily(self.open_blinds, self.weekdays_open_time)

def update_blind_open_time_guest(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
open_time = self.get_state(self.field_guest_weekdays_open_time)

if open_time is None:
self.log(f"Failed to retrieve {self.field_guest_weekdays_open_time} state.")
else:
self.guest_weekdays_open_time = self.parse_time(open_time)
self.log(f"{self.guest_weekdays_open_time=}")

def update_blind_guest_mode(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
guest_mode = self.get_state(self.field_guest_mode)
self.guest_mode = guest_mode == "on"
self.log(f"{self.guest_mode=}")

def update_blind_automation_enabled(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
automation_enabled = self.get_state(self.field_automation_enabled)
self.automation_enabled = automation_enabled == "on"
self.log(f"{self.automation_enabled=}")

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}")

scenes = scene_id if isinstance(scene_id, list) else [scene_id]
for scene in scenes:
self.handle_scene(scene)

def handle_scene(self, scene_id):
if scene_id == "scene.blinds_vent":
self.handle_vent()
elif scene_id == "scene.blinds_vent_bedroom":
Expand Down Expand Up @@ -118,6 +150,24 @@ def scene_activated(self, event_name, data, kwargs):
self.blinds_living_down_open()
elif scene_id == "scene.blinds_living_down_privacy":
self.blinds_living_down_privacy()
elif scene_id.startswith("scene.blinds_vent_"):
self.handle_scene_template(scene_id, 0, self.OPEN_HALF)
elif scene_id.startswith("scene.blinds_close_"):
self.handle_scene_template(scene_id, 0, 0)
elif scene_id.startswith("scene.blinds_open_"):
self.handle_scene_template(scene_id, 100, 0)
elif scene_id.startswith("scene.blinds_tilt_open_"):
self.handle_scene_template(scene_id, None, self.OPEN_HALF)
elif scene_id.startswith("scene.blinds_tilt_close_"):
self.handle_scene_template(scene_id, None, 0)
else:
self.log(f"Scene {scene_id} not found")

def handle_scene_template(self, scene_id, pos: Optional[float], tilt: Optional[float]):
for bld in self.ALL_BLINDS:
bld_low = bld.lower()
if scene_id.endswith(f"_{bld_low}"):
self.blind_move(bld, pos, tilt)

def handle_vent(self):
self.blinds_pos_tilt(self.BLIND_LIV_DOOR, 0, self.OPEN_HALF)
Expand Down Expand Up @@ -195,6 +245,12 @@ def blinds_morning(self):
self.blinds_pos_tilt(self.BLIND_SKLAD, 0, self.OPEN_HALF)
self.blinds_pos_tilt(self.BLIND_STUDY, 0, self.OPEN_HALF)

def blind_move(self, blind, pos: Optional[float], tilt: float):
if pos is None:
self.blinds_tilt(blind, tilt)
else:
self.blinds_pos_tilt(blind, pos, tilt)

def blinds_pos_tilt(self, blind, pos, tilt):
data = {"id": 1, "method": "Script.Eval", "params": {"id": 1, "code": f"posAndTilt({pos}, {tilt})"}}
return self.blinds_req(blind, data)
Expand Down
25 changes: 25 additions & 0 deletions ph4ha/config-ha/automations.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
- alias: "Receive Hallway Switch State Update"
trigger:
- platform: webhook
webhook_id: shelly_cor
condition:
- condition: template
value_template: "{{ trigger.json.token == 'xxxxx' }}"
action:
- service: input_boolean.turn_on
entity_id: input_boolean.hallway_switch_updated_by_webhook
- service: input_text.set_value
target:
entity_id: input_text.hallway_switch_state
data:
value: "{{ trigger.json.state }}"

- alias: Button 1 Short Click - Activate Blinds Vent Scene
trigger:
platform: state
entity_id: sensor.switch4bed_switch4bed_action
to: "1_single"
action:
service: scene.turn_on
target:
entity_id: scene.blinds_vent_bedroom
7 changes: 7 additions & 0 deletions ph4ha/config-ha/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ input_boolean:
blinds_guest_mode:
name: "Blinds Guest Mode"
initial: false
blinds_automation_enabled:
name: "Blinds Automation enabled"
initial: true

input_datetime:
blinds_weekday_open_time:
name: Blinds Weekday Open Time
has_date: false
has_time: true
blinds_weekday_guest_open_time:
name: Blinds Guest Weekday Open Time
has_date: false
has_time: true

switch:
- platform: template
Expand Down
15 changes: 15 additions & 0 deletions ph4ha/config-ha/scenes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,18 @@
entities: { }
icon: mdi:blinds
metadata: { }
- id: 'scene-blinds-vent-livbig'
name: Blinds vent livbig
entities: { }
icon: mdi:blinds
metadata: { }
- id: 'scene-blinds-vent-study'
name: Blinds vent Study
entities: { }
icon: mdi:blinds
metadata: { }
- id: 'scene-blinds-vent-sklad'
name: Blinds vent Sklad
entities: { }
icon: mdi:blinds
metadata: { }
6 changes: 4 additions & 2 deletions ph4ha/config/apps.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ blinds:
ip_address: "192.168.0.12"
ha_name: "shellyplus2pm-zzz"
password: !secret shelly_blinds_study
weekdays_open_time: "input_datetime.blinds_weekday_open_time"
weekdays_open_time: "input_datetime.weekday_blind_open_time"
guest_mode_input: "input_boolean.blinds_guest_mode"
stop_automation_input: "input_text.weekday_blind_open_time"
guest_weekdays_open_time_input: "input_datetime.blinds_weekday_guest_open_time"
automation_enabled_input: "input_boolean.blinds_automation_enabled"

morning_routine_time_input: "input_text.weekday_blind_open_time"
full_open_time_input: "input_text.weekday_blind_open_time"
full_close_time_input: "input_text.weekday_blind_open_time"
Expand Down

0 comments on commit 07f2161

Please sign in to comment.