niceboygithub / AqaraGateway

Aqara Gateway/Hub integration for Home Assistant
523 stars 66 forks source link

Offtop: Idea for Aqara g2h camera automation - play any sound #266

Open almirus opened 5 months ago

almirus commented 5 months ago

Hi folks! The camera allows you to play sounds, but there is no functionality to control this at all. I found a command that makes it play any sound through the camera. You can add this to Home Assistant automation. For example, you can play the sound of leaving the order at the door. All you need to do is record an audio file with the following characteristics

Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Format version                           : Version 4
Codec ID                                 : 2
Bit rate mode                            : Variable
Channel(s)                               : 1 channel
Channel layout                           : M
Sampling rate                            : 16.0 kHz

upload the file to the camera via wget

cd /etc/ch/ru/
wget http://www.YOUR_URL.com/output.aac
chown 1000:1000 output.aac

create a Python script (/config/python_scripts/amera_telnet_play_sound.py) in Home Assistant with the following content

#!/usr/bin/python3
import telnetlib
import time
HOST = "192.168.0.34"                                                   
telnet = telnetlib.Telnet(HOST)
#telnet.set_debuglevel(3)
telnet.write(b"\n")
telnet.read_until(b"login: ", timeout=10)
telnet.write(b"root\n\r")
telnet.read_until(b"# ", timeout=10)
telnet.write(b"debuger function debug_playfile /etc/ch/ru/output.aac\n\r")
telnet.read_until(b"# 2", timeout=10)
telnet.write(b"exit\n")
telnet.close()

and add the following lines to configuration.yaml.

# Allow Phyton Scripts 
python_script:

# Command line service:
shell_command:
  play_camera_sound_delivery: 'python3 /config/python_scripts/camera_telnet_play_sound.py'

Restart HA. After that, we will be able to call this script to play the audio file.

action:
  - service: shell_command.play_camera_sound_delivery
    data: {}