first commit

This commit is contained in:
Your Name
2024-01-11 15:10:51 +03:00
commit eddf07c652
602 changed files with 67154 additions and 0 deletions
@@ -0,0 +1,98 @@
import appdaemon.plugins.hass.hassapi as hass
import globals
import time
import datetime
import random
from datetime import timedelta
#
# Co2 report.
#
# Args:
# entity - group of entities to send state report
# notify - notify entity to send message
#
# Release Notes
#
# Version 1.0:
# Initial Version
class AirQuality(hass.Hass):
volume = 0.7
co2_max_level = 1000
def initialize(self):
if 'alice' not in self.args or 'co2_sensor' not in self.args or 'ha_panel' not in self.args:
self.error("Please provide alice, co2_sensor, ha_panel in config!")
return
self.timer = self.run_every(self.timer_tick, self.datetime()+timedelta(seconds=10), 1*60*60)
def timer_tick(self, args) -> None:
if 'constraint' in self.args and not self.constrain_input_boolean(self.args['constraint']):
return
home_state = self.get_state(self.args['ha_panel'])
co2_value = int(float(self.get_state(self.args['co2_sensor'])))
if home_state == "disarmed" and self.time_is_between(self.datetime(), '08:00:00', '23:59:59'):
self.log('co2 level: {}'.format(co2_value))
if co2_value > self.co2_max_level:
self.warn_air_quality(co2_value)
def warn_air_quality(self, co2_value):
commands=[
"Уровень ЦО2 достиг отметки {}... Пожалуйста, откройте окно.".format(co2_value),
"Мне кажется у нас душно... Давайте откроем окно?",
"ЦО2 растёт не по дням, а по часам... Уже {}... Давайте проветрим?".format(co2_value),
"Кажется, я давно не была на улице... Давайте откроем окно и сделаем вид что гуляем?",
"Пора открыть окошко, ЦО2 уже {}.".format(co2_value)
]
random.shuffle(commands)
self.say(commands[0])
def command(self, command):
self.call_service('yandex_station/send_command', command='sendText', text=command)
def say(self, command):
volume = self.get_state(self.args['alice'], 'volume_level')
state = self.get_state(self.args['alice'])
self.log('state: {}'.format(state))
if state == "playing":
self.call_service('media_player/media_pause', entity_id=self.args['alice'])
self.call_service('media_player/volume_set', entity_id=self.args['alice'], volume_level=self.volume)
self.log('command = {}'.format(command))
self.call_service('media_player/play_media', entity_id=self.args['alice'], media_content_type='text', media_content_id=command)
self.run_in(self.run_in_volume, 10, volume=volume, state=state)
def run_in_volume(self, kwargs):
if 'volume' not in kwargs or 'state' not in kwargs:
return
volume = kwargs['volume']
state = kwargs['state']
self.log('setting volume back to {}'.format(volume))
self.call_service('media_player/volume_set', entity_id=self.args['alice'], volume_level=volume)
if state == "playing":
self.log('resuming')
# self.call_service('media_player/media_play', entity_id=self.args['alice'])
def time_is_between(
hass, target_dt: datetime, start_time: str,
end_time: str) -> bool:
"""Generalization of AppDaemon's now_is_between method."""
start_time_dt = hass.parse_time(start_time) # type: datetime
end_time_dt = hass.parse_time(end_time) # type: datetime
start_dt = target_dt.replace(
hour=start_time_dt.hour,
minute=start_time_dt.minute,
second=start_time_dt.second)
end_dt = target_dt.replace(
hour=end_time_dt.hour,
minute=end_time_dt.minute,
second=end_time_dt.second)
if end_dt < start_dt:
# Spans midnight
if target_dt < start_dt and target_dt < end_dt:
target_dt = target_dt + timedelta(days=1)
end_dt = end_dt + timedelta(days=1)
return start_dt <= target_dt <= end_dt
@@ -0,0 +1,11 @@
#################################
# HOME PRESENSE #
#################################
airquality:
module: airquality
class: AirQuality
alice: media_player.yandex_station
ha_panel: alarm_control_panel.ha_alarm
co2_sensor: sensor.co2_mainroom_bed
global_dependencies:
- globals
+31
View File
@@ -0,0 +1,31 @@
import appdaemon.plugins.hass.hassapi as hass
import appdaemon.plugins.mqtt.mqttapi as mqtt
import globals
import json
import random
import time
import datetime
class MqttSay(mqtt.Mqtt, hass.Hass):
base_topic = "home/AliceBig/say"
def initialize(self) -> None:
self.log('Initialize Alice say plugin')
self.base_topic = self.args.get('topic', self.base_topic)
self.log('Listening for {}'.format(self.base_topic))
self.mqtt_subscribe(self.base_topic, namespace='mqtt')
self.listen_event(self.event_listener, 'MQTT_MESSAGE', namespace='mqtt')
def event_listener(self, event_name, data, kwargs):
if self.base_topic != data['topic']:
return
self.say(data['payload'])
def command(self, command):
self.call_service('yandex_station/send_command', command='sendText', text=command)
def say(self, command):
# self.call_service('media_player/volume_set', entity_id=self.args['alice'], volume_level=0.6)
self.call_service('media_player/play_media', entity_id=self.args['alice'], media_content_type='text', media_content_id=command)
@@ -0,0 +1,7 @@
mqttSay:
module: mqttSay
class: MqttSay
global_dependencies:
- globals
alice: media_player.yandex_station
topic: "home/yandexStation/say"
@@ -0,0 +1,49 @@
import appdaemon.plugins.hass.hassapi as hass
import globals
import time
#
# Home and away report
# Send report about home state after presence_actions was done.
#
# Args:
# entity - group of entities to send state report
# notify - notify entity to send message
#
# Release Notes
#
# Version 1.0:
# Initial Version
class PresenceAction(hass.Hass):
report_delay = 15
def initialize(self):
if 'alice' not in self.args:
self.error("Please provide alice in config!")
return
self.listen_event(self.away_mode, "away_mode")
self.listen_event(self.return_home_mode, "return_home_mode")
def away_mode(self, event_id, event_args, kwargs):
if 'constraint' in self.args and not self.constrain_input_boolean(self.args['constraint']):
return
self.command('стоп')
def return_home_mode(self, event_id, event_args, kwargs):
if 'constraint' in self.args and not self.constrain_input_boolean(self.args['constraint']):
return
command = 'Добро пожаловать домой.'
self.run_in(self.run_in_say, 59, command=command)
def run_in_say(self, kwargs):
if 'command' not in kwargs:
return
text = kwargs['command']
self.say(text)
def command(self, command):
self.call_service('yandex_station/send_command', command='sendText', text=command)
def say(self, command):
self.log('command = {}'.format(command))
self.call_service('media_player/play_media', entity_id=self.args['alice'], media_content_type='text', media_content_id=command)
@@ -0,0 +1,9 @@
#################################
# HOME PRESENSE #
#################################
presence_action:
module: presence_action
class: PresenceAction
alice: media_player.yandex_station
global_dependencies:
- globals