first commit
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import appdaemon.plugins.hass.hassapi as hass
|
||||
# from automation import Automation # type: ignore
|
||||
|
||||
"""
|
||||
Monitor events and output changes to the verbose_log. Nice for debugging purposes.
|
||||
Arguments:
|
||||
- events: List of events to monitor
|
||||
"""
|
||||
class Monitor(hass.Hass):
|
||||
def initialize(self) -> None:
|
||||
# super().initialize()
|
||||
self.listen_event_handle_list = []
|
||||
|
||||
events = self.args['events']
|
||||
|
||||
if events != None:
|
||||
for event in self.split_device_list(self.args["events"]):
|
||||
self.log('watching event "{}" for state changes'.format(event))
|
||||
self.listen_event_handle_list.append(self.listen_event(self.changed, event))
|
||||
if len(self.listen_event_handle_list) == 0:
|
||||
self.log('watching all events for state changes')
|
||||
self.listen_event_handle_list.append(self.listen_event(self.changed))
|
||||
|
||||
def changed(self, event_name, data, kwargs):
|
||||
self.log(event_name + ': ' + str(data))
|
||||
|
||||
def terminate(self):
|
||||
for listen_event_handle in self.listen_event_handle_list:
|
||||
self.cancel_listen_event(listen_event_handle)
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
# eventMonitor:
|
||||
# module: eventMonitor
|
||||
# class: Monitor
|
||||
# global_dependencies:
|
||||
# - automation
|
||||
# events:
|
||||
@@ -0,0 +1,67 @@
|
||||
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 HappyBubblesToDeviceTracker(mqtt.Mqtt, hass.Hass):
|
||||
base_topic = "happy-bubbles/presence/ha"
|
||||
tag_timers = {}
|
||||
|
||||
def initialize(self) -> None:
|
||||
self.log('Initialize HappyBubblesToDeviceTracker')
|
||||
self.mqtt_subscribe('{}/#'.format(self.base_topic), namespace='mqtt')
|
||||
self.listen_event(self.event_listener, 'MQTT_MESSAGE', namespace='mqtt')
|
||||
|
||||
def event_listener(self, event_name, data, kwargs):
|
||||
topic = data.get('topic', '')
|
||||
if self.base_topic not in topic:
|
||||
return
|
||||
location = topic.replace('{}/'.format(self.base_topic), "")
|
||||
|
||||
payload = json.loads(data['payload']) if data.get('payload') else {}
|
||||
# self.log('[{}] topic: {}, payload: {}'.format(event_name, topic, payload))
|
||||
id = payload.get('id', None)
|
||||
name = payload.get('name', None)
|
||||
distance = payload.get('distance', None)
|
||||
|
||||
# devices = self.get_state()
|
||||
# device_name = "device_tracker.tag_ble_{}".format(id)
|
||||
# if device_name in devices:
|
||||
# self.log('tag found in devices')
|
||||
|
||||
self.move_tag(id, isHome=True)
|
||||
self.restart_timer(id)
|
||||
|
||||
self.log('Beacon {} ({}) moved to {} (distance: {}).'.format(name, id, location, distance))
|
||||
|
||||
def restart_timer(self, id):
|
||||
if id in self.tag_timers:
|
||||
self.cancel_timer(self.tag_timers[id])
|
||||
self.tag_timers[id] = self.run_in(self.tag_away_timer_callback, 60, id=id)
|
||||
|
||||
def tag_away_timer_callback(self, kwargs):
|
||||
id = kwargs.get('id', None)
|
||||
if id == None:
|
||||
self.log('id is forgotten')
|
||||
return
|
||||
|
||||
self.log('awaying tag:{}'.format(id))
|
||||
self.move_tag(id, isHome=False)
|
||||
|
||||
def move_tag(self, id, isHome = False):
|
||||
hb_topic = "owntracks/tag/ble_{}".format(id)
|
||||
tst = int(round(time.time(),0))
|
||||
payload = '{"_type":"location","acc":18,"alt":0,"batt":100,"lat":55.7899036,"lon":37.6595347,"tst":1579857481}'
|
||||
jsobj = json.loads(payload)
|
||||
jsobj['tst'] = tst
|
||||
if isHome:
|
||||
jsobj['lat'] = self.args['home_lat']
|
||||
jsobj['lon'] = self.args['home_lon']
|
||||
else:
|
||||
jsobj['lat'] = 0
|
||||
jsobj['lon'] = 0
|
||||
payload = json.dumps(jsobj)
|
||||
self.mqtt_publish(hb_topic, payload, qos = 0, retain = False, namespace='mqtt')
|
||||
@@ -0,0 +1,8 @@
|
||||
# happyBubblesToDeviceTracker:
|
||||
# module: happyBubblesToDeviceTracker
|
||||
# class: HappyBubblesToDeviceTracker
|
||||
# global_dependencies:
|
||||
# - globals
|
||||
# constrain: input_boolean.constraint_test
|
||||
# home_lat: !secret home_latitude
|
||||
# home_lon: !secret home_longitude
|
||||
@@ -0,0 +1,37 @@
|
||||
import appdaemon.plugins.hass.hassapi as hass
|
||||
import appdaemon.plugins.mqtt.mqttapi as mqtt
|
||||
import globals
|
||||
import json
|
||||
import random
|
||||
|
||||
"""
|
||||
Monitor events and output changes to the verbose_log. Nice for debugging purposes.
|
||||
Arguments:
|
||||
- events: List of events to monitor
|
||||
"""
|
||||
|
||||
class RassToHappyBubbles(mqtt.Mqtt):
|
||||
base_topic = "home/presence"
|
||||
def initialize(self) -> None:
|
||||
self.log('Initialize RassToHappyBubbles')
|
||||
self.mqtt_subscribe('{}/#'.format(self.base_topic), namespace='mqtt')
|
||||
self.listen_event(self.event_listener, 'MQTT_MESSAGE', namespace='mqtt')
|
||||
|
||||
def event_listener(self, event_name, data, kwargs):
|
||||
topic = data.get('topic', '')
|
||||
if self.base_topic not in topic:
|
||||
return
|
||||
#array = ['hall', 'balcony']
|
||||
#random.shuffle(array)
|
||||
#device=array[0]
|
||||
|
||||
device = topic.replace('{}/'.format(self.base_topic), "")
|
||||
payload = json.loads(data['payload']) if data.get('payload') else {}
|
||||
id = payload.get('id', None)
|
||||
hb_topic="happy-bubbles/ble/{}/raw/{}".format(device,id)
|
||||
payload['hostname'] = device
|
||||
payload['mac'] = payload.get('id', '')
|
||||
|
||||
payload_data = json.dumps(payload)
|
||||
self.mqtt_publish(hb_topic, payload_data, qos = 0, retain = False, namespace='mqtt')
|
||||
# self.log('publishing to: {}: {}'.format(hb_topic, payload_data))
|
||||
@@ -0,0 +1,6 @@
|
||||
# rassToHappybubbles:
|
||||
# module: rassToHappybubbles
|
||||
# class: RassToHappyBubbles
|
||||
# global_dependencies:
|
||||
# - globals
|
||||
# constrain: input_boolean.constraint_test
|
||||
@@ -0,0 +1,20 @@
|
||||
import appdaemon.plugins.hass.hassapi as hass
|
||||
import os
|
||||
|
||||
"""
|
||||
Monitor events and output changes to the verbose_log. Nice for debugging purposes.
|
||||
Arguments:
|
||||
- events: List of events to monitor
|
||||
"""
|
||||
class Test(hass.Hass):
|
||||
notify = None
|
||||
|
||||
def initialize(self) -> None:
|
||||
self.listen_state(self.sensor_trigger, 'sensor.0x00158d0003a361d8_action')
|
||||
self.listen_state(self.sensor_trigger, 'binary_sensor.0x00158d0003f26d61_occupancy')
|
||||
# self.listen_state(self.sensor_trigger, 'sensor.remote20_action')
|
||||
|
||||
def sensor_trigger(self, entity, attribute, old, new, kwargs):
|
||||
if new != "" and new != "unknown":
|
||||
self.log("doorbell call")
|
||||
self.toggle("switch.0x00158d00020f3a29_switch")
|
||||
@@ -0,0 +1,5 @@
|
||||
test:
|
||||
module: test
|
||||
class: Test
|
||||
dependencies:
|
||||
- notify
|
||||
Reference in New Issue
Block a user