Skip to content

Commit

Permalink
Use python script to control lights
Browse files Browse the repository at this point in the history
  • Loading branch information
claha committed Nov 16, 2023
1 parent 18a79fa commit 85835bd
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 350 deletions.
3 changes: 1 addition & 2 deletions roles/homeassistant/defaults/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
homeassistant_service_name: "{{ ansible_role_name }}"
homeassistant_service_path: "{{ services_path }}/{{ homeassistant_service_name }}" # yamllint disable rule:line-length
homeassistant_areas:
- bedroom
- guest_room
- kitchen
- living_room
- laundry
- master_bedroom
- office
- kids_bedroom

homeassistant_stocks: []
homeassistant_funds: []
Expand Down
10 changes: 8 additions & 2 deletions roles/homeassistant/files/config/automation/lights_closet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
to: "off"

action:
service: script.lights_closet_off
service: python_script.control_lights
data:
area: "closet"
action: "off"


- alias: "Lights closet on"
Expand All @@ -20,4 +23,7 @@
to: "on"

action:
service: script.lights_closet_on
service: python_script.control_lights
data:
area: "closet"
action: "on"
15 changes: 12 additions & 3 deletions roles/homeassistant/files/config/automation/lights_laundry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
state: "on"

action:
service: script.lights_laundry_dim
service: python_script.control_lights
data:
area: "laundry"
action: "dim"


- alias: "Lights laundry on motion"
Expand All @@ -37,7 +40,10 @@
after: input_datetime.lights_laundry_off_evening

action:
service: script.lights_laundry_dim
service: python_script.control_lights
data:
area: "laundry"
action: "dim"


- alias: "Lights laundry off motion"
Expand All @@ -64,4 +70,7 @@
after: input_datetime.lights_laundry_off_evening

action:
service: script.lights_laundry_off
service: python_script.control_lights
data:
area: "laundry"
action: "off"
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
state: "on"

action:
service: script.lights_master_bedroom_dim
service: python_script.control_lights
data:
area: "master_bedroom"
action: "dim"

This file was deleted.

90 changes: 0 additions & 90 deletions roles/homeassistant/files/config/helper/input_datetime.yaml

This file was deleted.

140 changes: 140 additions & 0 deletions roles/homeassistant/files/config/python_scripts/control_lights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
"""Control lights."""
hass = hass # noqa: F821
data = data # noqa: F821
logger = logger # noqa: F821

ACTION_ON = "on"
ACTION_DIM = "dim"
ACTION_OFF = "off"


def light_on(entity_id):
"""Turn on a light."""
hass.services.call(
"light",
"turn_on",
{
"entity_id": entity_id,
"brightness_pct": 80,
"color_temp": 454,
"transition": 10,
},
False,
)


def light_dim(entity_id):
"""Dim a light."""
hass.services.call(
"light",
"turn_on",
{
"entity_id": entity_id,
"brightness_pct": 10,
"color_temp": 454,
"transition": 10,
},
False,
)


def light_off(entity_id):
"""Turn off a light."""
hass.services.call(
"light",
"turn_off",
{
"entity_id": entity_id,
"transition": 10,
},
False,
)


def switch_on(entity_id):
"""Turn on a switch."""
hass.services.call(
"switch",
"turn_on",
{
"entity_id": entity_id,
},
False,
)


def switch_dim(entity_id):
"""Dim a switch."""
pass


def switch_off(entity_id):
"""Turn off a switch."""
hass.services.call(
"switch",
"turn_off",
{
"entity_id": entity_id,
},
False,
)


on_control = {
"light": light_on,
"switch": switch_on,
}

dim_control = {
"light": light_dim,
"switch": switch_dim,
}

off_control = {
"light": light_off,
"switch": switch_off,
}


lights = {
"master_bedroom": [
"light.tradfri_bulb_0",
],
"guest_room": [
"switch.shelly_plug_s_0",
],
"kitchen": [
"switch.tradfri_control_outlet_1",
# "switch.osram_smart_plug_1", # Seems broken?
],
"living_room": [
"switch.osram_smart_plug_0",
"switch.tradfri_control_outlet_0",
"switch.shelly_plug_s_1",
],
"laundry": [
"light.tradfri_bulb_2",
],
"closet": [
"light.bulb_rgbw_8b5534",
],
"kids_bedroom": [
"switch.tradfri_control_outlet_2",
"switch.tradfri_control_outlet_3",
],
}

# Get script arguments
area = data.get("area")
action = data.get("action")

# Perform action on each entity id
entity_ids = lights[area]
for entity_id in entity_ids:
entity_type = entity_id.split(".")[0]
if action == ACTION_ON:
on_control[entity_type](entity_id)
elif action == ACTION_DIM:
dim_control[entity_type](entity_id)
elif action == ACTION_OFF:
off_control[entity_type](entity_id)
19 changes: 0 additions & 19 deletions roles/homeassistant/files/config/script/lights_bedroom.yaml

This file was deleted.

24 changes: 0 additions & 24 deletions roles/homeassistant/files/config/script/lights_closet.yaml

This file was deleted.

Loading

0 comments on commit 85835bd

Please sign in to comment.