publicpythonsnippets/homeassistant_chores/genchores.py

259 lines
7.5 KiB
Python
Raw Permalink Normal View History

2024-09-17 17:01:09 -05:00
import os
import shutil
import csv
import re
import datetime
import locale
class chore:
def __init__(self,who,chore,icon,points):
self.who = who
self.chore = chore
self.icon = icon
self.points = points
def genchoreyaml(self):
boolname = self.chore.lower()
boolname=boolname.replace(" ","_")
boolname=boolname.replace("-","")
boolname=boolname.replace("__","_")
if self.icon == '':
iconstring = 'mdi:account-hard-hat'
else:
iconstring = self.icon
if self.who == 'b':
return f"""
chore_charley_{boolname}:
name: {self.chore}
icon: {iconstring}
chore_charley_{boolname}_completed_today:
name: {self.chore} Completed Today
chore_henry_{boolname}:
name: {self.chore}
icon: {iconstring}
chore_henry_{boolname}_completed_today:
name: {self.chore} Completed Today
"""
if self.who == 'c':
return f"""
chore_charley_{boolname}:
name: {self.chore}
icon: {iconstring}
chore_charley_{boolname}_completed_today:
name: {self.chore} Completed Today
"""
if self.who == 'h':
return f"""
chore_henry_{boolname}:
name: {self.chore}
icon: {iconstring}
chore_henry_{boolname}_completed_today:
name: {self.chore} Completed Today
"""
#return boolname
def genautomationyaml(self):
boolname = self.chore.lower()
boolname=boolname.replace(" ","_")
boolname=boolname.replace("-","")
boolname=boolname.replace("__","_")
if self.who == 'b':
return f"""
- alias: chore_charley_{boolname}
trigger:
- platform: state
entity_id:
- input_boolean.chore_charley_{boolname}
to: "on"
condition:
- condition: state
entity_id:
- input_boolean.chore_charley_{boolname}_completed_today
state: "off"
action:
- action: script.chore_charley_{boolname}
- action: input_boolean.turn_on
target:
entity_id:
- input_boolean.chore_charley_{boolname}_completed_today
mode: single
- alias: chore_henry_{boolname}
trigger:
- platform: state
entity_id:
- input_boolean.chore_henry_{boolname}
to: "on"
condition:
- condition: state
entity_id:
- input_boolean.chore_henry_{boolname}_completed_today
state: "off"
action:
- action: script.chore_henry_{boolname}
- action: input_boolean.turn_on
target:
entity_id:
- input_boolean.chore_henry_{boolname}_completed_today
mode: single
"""
if self.who == 'c':
return f"""
- alias: chore_charley_{boolname}
trigger:
- platform: state
entity_id:
- input_boolean.chore_charley_{boolname}
to: "on"
condition:
- condition: state
entity_id:
- input_boolean.chore_charley_{boolname}_completed_today
state: "off"
action:
- action: script.chore_charley_{boolname}
- action: input_boolean.turn_on
target:
entity_id:
- input_boolean.chore_charley_{boolname}_completed_today
mode: single
"""
if self.who == 'h':
return f"""
- alias: chore_henry_{boolname}
trigger:
- platform: state
entity_id: input_boolean.chore_henry_{boolname}
to: "on"
condition:
- condition: state
entity_id: input_boolean.chore_henry_{boolname}_completed_today
state: "off"
action:
- action: script.chore_henry_{boolname}
- action: input_boolean.turn_on
target:
entity_id:
- input_boolean.chore_henry_{boolname}_completed_today
mode: single
"""
def genscriptyaml(self):
boolname = self.chore.lower()
boolname=boolname.replace(" ","_")
boolname=boolname.replace("-","")
boolname=boolname.replace("__","_")
if self.who == 'b':
return f"""
chore_henry_{boolname}:
alias: chore_henry_{boolname}
sequence:
- repeat:
count: {self.points}
sequence:
- action: counter.increment
target:
entity_id: counter.henrys_chore_points
chore_charley_{boolname}:
alias: chore_charley_{boolname}
sequence:
- repeat:
count: {self.points}
sequence:
- action: counter.increment
target:
entity_id: counter.charleys_chore_points
"""
if self.who == 'c':
return f"""
chore_charley_{boolname}:
alias: chore_charley_{boolname}
sequence:
- repeat:
count: {self.points}
sequence:
- action: counter.increment
target:
entity_id: counter.charleys_chore_points
"""
if self.who == 'h':
return f"""
chore_henry_{boolname}:
alias: chore_henry_{boolname}
sequence:
- repeat:
count: {self.points}
sequence:
- action: counter.increment
target:
entity_id: counter.henrys_chore_points
"""
def resetentities(self):
boolname = self.chore.lower()
boolname=boolname.replace(" ","_")
boolname=boolname.replace("-","")
boolname=boolname.replace("__","_")
if self.who == 'b':
return [
f"input_boolean.chore_charley_{boolname}",
f"input_boolean.chore_charley_{boolname}_completed_today",
f"input_boolean.chore_henry_{boolname}",
f"input_boolean.chore_henry_{boolname}_completed_today",
]
if self.who == 'c':
return [
f"input_boolean.chore_charley_{boolname}",
f"input_boolean.chore_charley_{boolname}_completed_today",
]
if self.who == 'h':
return [
f"input_boolean.chore_henry_{boolname}",
f"input_boolean.chore_henry_{boolname}_completed_today",
]
if os.path.exists(f"./homeassistant/chores.yaml"):
os.remove(f"./homeassistant/chores.yaml")
if os.path.exists(f"./homeassistant/automation.yaml"):
os.remove(f"./homeassistant/automation.yaml")
if os.path.exists(f"./homeassistant/scripts.yaml"):
os.remove(f"./homeassistant/scripts.yaml")
chorefile = "./homeassistant/chorelist.txt"
chorelist=[]
with open(chorefile) as csvcontents:
chores = csv.DictReader(csvcontents,delimiter=',',quotechar='"')
for mychore in chores:
chorelist.append(chore(mychore['For'],mychore["Chore"],mychore['icon'],mychore['points']))
#print(chore(mychore['For'],mychore["Chore"],mychore['icon'],mychore['points']).genchoreyaml())
#print(chore(mychore['For'],mychore["Chore"],mychore['icon'],mychore['points']).genautomationyaml())
#print(chore(mychore['For'],mychore["Chore"],mychore['icon'],mychore['points']).genscriptyaml())
resetentities = []
for mychore in chorelist:
with open("./homeassistant/chores.yaml","a+") as outchores:
outchores.write(mychore.genchoreyaml())
with open("./homeassistant/automation.yaml","a+") as outchores:
outchores.write(mychore.genautomationyaml())
with open("./homeassistant/scripts.yaml","a+") as outchores:
outchores.write(mychore.genscriptyaml())
myentities = mychore.resetentities()
for myentity in myentities:
resetentities.append(myentity)
resetscript = """
chore_reset_daily_limiters:
alias: Chore - Reset Entities
sequence:
- action: input_boolean.turn_off
target:
entity_id:
"""
for entity in resetentities:
resetscript += f" - {entity}\n"
with open("./homeassistant/scripts.yaml","a+") as outchores:
outchores.write(resetscript)
#print(resetscript)