woof/main.py

66 lines
1.7 KiB
Python
Raw Normal View History

2022-01-30 23:52:17 +00:00
import os, sys
2021-10-07 04:02:16 +00:00
import logging
2022-01-30 23:52:17 +00:00
logging.basicConfig(stream=sys.stdout,
2021-10-07 04:02:16 +00:00
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
level=logging.DEBUG if os.environ.get('DEBUG') else logging.INFO)
2021-10-06 09:04:07 +00:00
import time
2021-10-07 04:02:16 +00:00
import json
2021-10-06 09:04:07 +00:00
import pygame
2021-10-07 04:02:16 +00:00
from pyezviz import EzvizClient, MQTTClient
import secrets
2021-10-06 09:04:07 +00:00
CHIME = 'chime.ogg'
FRONTDOOR = 'frontdoor.ogg'
BACKDOOR = 'backdoor.ogg'
def play_sound(filename):
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
2021-10-07 04:02:16 +00:00
logging.info('Playing sound %s', filename)
2021-10-06 09:04:07 +00:00
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
def backdoor():
play_sound(CHIME)
play_sound(BACKDOOR)
2021-10-07 04:02:16 +00:00
time.sleep(0.75)
2021-10-06 09:04:07 +00:00
play_sound(CHIME)
play_sound(BACKDOOR)
2021-10-07 04:02:16 +00:00
def on_message(client, userdata, mqtt_message):
message = json.loads(mqtt_message.payload)
#print(json.dumps(mqtt_message, indent=4))
if message['alert'] == 'somebody there ring the door': # lmao
logging.info('Received door bell press alert')
if 'E80451501' in message['ext']:
logging.info('Backdoor pressed')
backdoor()
if __name__ == '__main__':
logging.info('')
logging.info('==========================')
logging.info('Booting up...')
pygame.init()
2022-01-30 23:52:17 +00:00
pygame.mixer.pre_init(buffer=4096)
pygame.mixer.init(buffer=4096)
2021-10-06 09:04:07 +00:00
2021-10-07 04:02:16 +00:00
client = EzvizClient(secrets.EZVIZ_EMAIL, secrets.EZVIZ_PASS, 'apiius.ezvizlife.com')
try:
logging.info('Logging into EZVIZ client...')
token = client.login()
mqtt = MQTTClient(token, on_message)
logging.info('Starting MQTT...')
mqtt.start()
except Exception as exp:
logging.exception(str(exp))
finally:
client.close_session()