Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8e96fc5408 | ||
|
|
0f8858cef4 | ||
|
|
e43cda557a | ||
|
|
7dff1b3bf4 | ||
|
|
28f2761899 | ||
|
|
17854bda0f | ||
|
|
88323631ea | ||
|
|
2a56eee4f5 | ||
|
|
caacaea81f | ||
|
|
d7e52c0843 | ||
|
|
dc6b6c0149 | ||
|
|
2687358810 | ||
|
|
1a076c7660 | ||
|
|
3ee2a7bf64 | ||
|
|
441905c91f | ||
|
|
a5a9383ad4 | ||
|
|
61cda99e5f | ||
|
|
1b85a28e01 | ||
|
|
b6e6dc1eca | ||
|
|
2a683fefc8 | ||
|
|
a94b964f9f |
11
.github/workflows/publish.yml
vendored
11
.github/workflows/publish.yml
vendored
@@ -23,18 +23,23 @@ jobs:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2.3.4
|
||||
with:
|
||||
ref: main
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@v1.2.0
|
||||
with:
|
||||
platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v1.3.0
|
||||
uses: docker/setup-buildx-action@v1.5.1
|
||||
- name: Login to DockerHub
|
||||
uses: docker/login-action@v1.9.0
|
||||
uses: docker/login-action@v1.10.0
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v2.5.0
|
||||
uses: docker/build-push-action@v2.6.1
|
||||
with:
|
||||
context: .
|
||||
push: true
|
||||
platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x
|
||||
tags: |
|
||||
|
||||
@@ -5,7 +5,7 @@ RUN mkdir /install
|
||||
WORKDIR /install
|
||||
|
||||
COPY requirements.txt /
|
||||
RUN pip install --prefix=/install -r /requirements.txt
|
||||
RUN pip install --no-warn-script-location --prefix=/install -r /requirements.txt
|
||||
|
||||
FROM base
|
||||
COPY --from=builder /install /usr/local
|
||||
|
||||
@@ -14,8 +14,13 @@ It supports the following environment variables:
|
||||
- `MQTT_HOST` (optional, default = 'localhost')
|
||||
- `MQTT_QOS` (optional, default = 0)
|
||||
- `MQTT_PORT` (optional, default = 1883)
|
||||
- `MQTT_TLS_ENABLED` (required if using TLS) - set to `true` to enable
|
||||
- `MQTT_TLS_CA_CERT` (required if using TLS) - path to the ca certs
|
||||
- `MQTT_TLS_CERT` (required if using TLS) - path to the private cert
|
||||
- `MQTT_TLS_KEY` (required if using TLS) - path to the private key
|
||||
- `HOME_ASSISTANT` (optional, default = false)
|
||||
- `HOME_ASSISTANT_PREFIX` (optional, default = 'homeassistant')
|
||||
- `STORAGE_POLL_INTERVAL` (optional, default = 3600) - how often to fetch storage data (in seconds)
|
||||
|
||||
It exposes events to the following topics:
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
amcrest==1.7.2
|
||||
amcrest==1.8.0
|
||||
paho-mqtt==1.5.1
|
||||
python-slugify==5.0.2
|
||||
|
||||
@@ -7,8 +7,8 @@ import sys
|
||||
from json import dumps
|
||||
import signal
|
||||
from threading import Timer
|
||||
import ssl
|
||||
|
||||
storage_sensors_interval = 60 # 1 hour
|
||||
is_exiting = False
|
||||
mqtt_client = None
|
||||
|
||||
@@ -17,12 +17,17 @@ amcrest_host = os.getenv("AMCREST_HOST")
|
||||
amcrest_port = int(os.getenv("AMCREST_PORT") or 80)
|
||||
amcrest_username = os.getenv("AMCREST_USERNAME") or "admin"
|
||||
amcrest_password = os.getenv("AMCREST_PASSWORD")
|
||||
storage_poll_interval = int(os.getenv("STORAGE_POLL_INTERVAL") or 3600)
|
||||
|
||||
mqtt_host = os.getenv("MQTT_HOST") or "localhost"
|
||||
mqtt_qos = int(os.getenv("MQTT_QOS") or 0)
|
||||
mqtt_port = int(os.getenv("MQTT_PORT") or 1883)
|
||||
mqtt_username = os.getenv("MQTT_USERNAME")
|
||||
mqtt_password = os.getenv("MQTT_PASSWORD") # can be None
|
||||
mqtt_tls_enabled = os.getenv("MQTT_TLS_ENABLED") == "true"
|
||||
mqtt_tls_ca_cert = os.getenv("MQTT_TLS_CA_CERT")
|
||||
mqtt_tls_cert = os.getenv("MQTT_TLS_CERT")
|
||||
mqtt_tls_key = os.getenv("MQTT_TLS_KEY")
|
||||
|
||||
home_assistant = os.getenv("HOME_ASSISTANT") == "true"
|
||||
home_assistant_prefix = os.getenv("HOME_ASSISTANT_PREFIX") or "homeassistant"
|
||||
@@ -68,6 +73,8 @@ def on_mqtt_disconnect(client, userdata, rc):
|
||||
def exit_gracefully(rc, skip_mqtt=False):
|
||||
global topics, mqtt_client
|
||||
|
||||
log("Exiting app...")
|
||||
|
||||
if mqtt_client is not None and mqtt_client.is_connected() and skip_mqtt == False:
|
||||
mqtt_publish(topics["status"], "offline", exit_on_error=False)
|
||||
mqtt_client.loop_stop(force=True)
|
||||
@@ -78,9 +85,9 @@ def exit_gracefully(rc, skip_mqtt=False):
|
||||
os._exit(rc)
|
||||
|
||||
def refresh_storage_sensors():
|
||||
global camera, topics, storage_sensors_interval
|
||||
global camera, topics, storage_poll_interval
|
||||
|
||||
Timer(storage_sensors_interval, refresh_storage_sensors).start()
|
||||
Timer(storage_poll_interval, refresh_storage_sensors).start()
|
||||
log("Fetching storage sensors...")
|
||||
|
||||
try:
|
||||
@@ -91,6 +98,14 @@ def refresh_storage_sensors():
|
||||
except AmcrestError as error:
|
||||
log(f"Error fetching storage information {error}", level="WARNING")
|
||||
|
||||
def ping_camera():
|
||||
Timer(30, ping_camera).start()
|
||||
response = os.system(f"ping -c1 -W100 {amcrest_host} >/dev/null 2>&1")
|
||||
|
||||
if response != 0:
|
||||
log("Ping unsuccessful", level="ERROR")
|
||||
exit_gracefully(1)
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
# exit immediately upon receiving a second SIGINT
|
||||
global is_exiting
|
||||
@@ -129,14 +144,18 @@ camera = AmcrestCamera(
|
||||
# Fetch camera details
|
||||
log("Fetching camera details...")
|
||||
|
||||
device_type = camera.device_type.replace("type=", "").strip()
|
||||
is_ad110 = device_type == "AD110"
|
||||
is_ad410 = device_type == "AD410"
|
||||
is_doorbell = is_ad110 or is_ad410
|
||||
serial_number = camera.serial_number.strip()
|
||||
sw_version = camera.software_information[0].replace("version=", "").strip()
|
||||
device_name = camera.machine_name.replace("name=", "").strip()
|
||||
device_slug = slugify(device_name, separator="_")
|
||||
try:
|
||||
device_type = camera.device_type.replace("type=", "").strip()
|
||||
is_ad110 = device_type == "AD110"
|
||||
is_ad410 = device_type == "AD410"
|
||||
is_doorbell = is_ad110 or is_ad410
|
||||
serial_number = camera.serial_number.strip()
|
||||
sw_version = camera.software_information[0].replace("version=", "").strip()
|
||||
device_name = camera.machine_name.replace("name=", "").strip()
|
||||
device_slug = slugify(device_name, separator="_")
|
||||
except AmcrestError as error:
|
||||
log(f"Error fetching camera details", level="ERROR")
|
||||
exit_gracefully(1)
|
||||
|
||||
log(f"Device type: {device_type}")
|
||||
log(f"Serial number: {serial_number}")
|
||||
@@ -169,8 +188,27 @@ mqtt_client = mqtt.Client(
|
||||
client_id=f"amcrest2mqtt_{serial_number}", clean_session=False
|
||||
)
|
||||
mqtt_client.on_disconnect = on_mqtt_disconnect
|
||||
mqtt_client.username_pw_set(mqtt_username, password=mqtt_password)
|
||||
mqtt_client.will_set(topics["status"], payload="offline", qos=mqtt_qos, retain=True)
|
||||
if mqtt_tls_enabled:
|
||||
log(f"Setting up MQTT for TLS")
|
||||
if mqtt_tls_ca_cert is None:
|
||||
log("Missing var: MQTT_TLS_CA_CERT", level="ERROR")
|
||||
sys.exit(1)
|
||||
if mqtt_tls_cert is None:
|
||||
log("Missing var: MQTT_TLS_CERT", level="ERROR")
|
||||
sys.exit(1)
|
||||
if mqtt_tls_cert is None:
|
||||
log("Missing var: MQTT_TLS_KEY", level="ERROR")
|
||||
sys.exit(1)
|
||||
mqtt_client.tls_set(
|
||||
ca_certs=mqtt_tls_ca_cert,
|
||||
certfile=mqtt_tls_cert,
|
||||
keyfile=mqtt_tls_key,
|
||||
cert_reqs=ssl.CERT_REQUIRED,
|
||||
tls_version=ssl.PROTOCOL_TLS,
|
||||
)
|
||||
else:
|
||||
mqtt_client.username_pw_set(mqtt_username, password=mqtt_password)
|
||||
|
||||
try:
|
||||
mqtt_client.connect(mqtt_host, port=mqtt_port)
|
||||
@@ -239,6 +277,7 @@ if home_assistant:
|
||||
json=True,
|
||||
)
|
||||
|
||||
if storage_poll_interval > 0:
|
||||
mqtt_publish(
|
||||
topics["home_assistant"]["storage_used_percent"],
|
||||
base_config
|
||||
@@ -288,12 +327,15 @@ mqtt_publish(topics["config"], {
|
||||
"serial_number": serial_number,
|
||||
}, json=True)
|
||||
|
||||
refresh_storage_sensors()
|
||||
if storage_poll_interval > 0:
|
||||
refresh_storage_sensors()
|
||||
|
||||
ping_camera()
|
||||
|
||||
log("Listening for events...")
|
||||
|
||||
try:
|
||||
for code, payload in camera.event_actions("All", retries=5):
|
||||
for code, payload in camera.event_actions("All", retries=5, timeout_cmd=(10.00, 3600)):
|
||||
if (is_ad110 and code == "ProfileAlarmTransmit") or (code == "VideoMotion" and not is_ad110):
|
||||
motion_payload = "on" if payload["action"] == "Start" else "off"
|
||||
mqtt_publish(topics["motion"], motion_payload)
|
||||
|
||||
Reference in New Issue
Block a user