Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
17854bda0f | ||
|
|
88323631ea | ||
|
|
2a56eee4f5 | ||
|
|
caacaea81f | ||
|
|
d7e52c0843 | ||
|
|
dc6b6c0149 | ||
|
|
2687358810 | ||
|
|
1a076c7660 | ||
|
|
3ee2a7bf64 | ||
|
|
441905c91f | ||
|
|
a5a9383ad4 | ||
|
|
61cda99e5f | ||
|
|
1b85a28e01 | ||
|
|
b6e6dc1eca | ||
|
|
2a683fefc8 | ||
|
|
a94b964f9f | ||
|
|
afb215850c | ||
|
|
c99b918e84 | ||
|
|
df45393453 | ||
|
|
f5b655dbf1 |
13
.github/workflows/publish.yml
vendored
13
.github/workflows/publish.yml
vendored
@@ -21,18 +21,25 @@ jobs:
|
|||||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v2.3.4
|
||||||
|
with:
|
||||||
|
ref: main
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v1.2.0
|
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
|
- 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
|
- name: Login to DockerHub
|
||||||
uses: docker/login-action@v1.9.0
|
uses: docker/login-action@v1.10.0
|
||||||
with:
|
with:
|
||||||
username: ${{ secrets.DOCKER_USERNAME }}
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
- name: Build and push
|
- name: Build and push
|
||||||
uses: docker/build-push-action@v2.5.0
|
uses: docker/build-push-action@v2.6.1
|
||||||
with:
|
with:
|
||||||
|
context: .
|
||||||
push: true
|
push: true
|
||||||
platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x
|
platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x
|
||||||
tags: |
|
tags: |
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ RUN mkdir /install
|
|||||||
WORKDIR /install
|
WORKDIR /install
|
||||||
|
|
||||||
COPY requirements.txt /
|
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
|
FROM base
|
||||||
COPY --from=builder /install /usr/local
|
COPY --from=builder /install /usr/local
|
||||||
COPY src /app
|
COPY src /app
|
||||||
|
COPY VERSION /app
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
CMD [ "python", "-u", "/app/amcrest2mqtt.py" ]
|
CMD [ "python", "-u", "/app/amcrest2mqtt.py" ]
|
||||||
|
|||||||
@@ -14,8 +14,13 @@ It supports the following environment variables:
|
|||||||
- `MQTT_HOST` (optional, default = 'localhost')
|
- `MQTT_HOST` (optional, default = 'localhost')
|
||||||
- `MQTT_QOS` (optional, default = 0)
|
- `MQTT_QOS` (optional, default = 0)
|
||||||
- `MQTT_PORT` (optional, default = 1883)
|
- `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` (optional, default = false)
|
||||||
- `HOME_ASSISTANT_PREFIX` (optional, default = 'homeassistant')
|
- `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:
|
It exposes events to the following topics:
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import sys
|
|||||||
from json import dumps
|
from json import dumps
|
||||||
import signal
|
import signal
|
||||||
from threading import Timer
|
from threading import Timer
|
||||||
|
import ssl
|
||||||
|
|
||||||
storage_sensors_interval = 60 # 1 hour
|
|
||||||
is_exiting = False
|
is_exiting = False
|
||||||
mqtt_client = None
|
mqtt_client = None
|
||||||
|
|
||||||
@@ -17,16 +17,33 @@ amcrest_host = os.getenv("AMCREST_HOST")
|
|||||||
amcrest_port = int(os.getenv("AMCREST_PORT") or 80)
|
amcrest_port = int(os.getenv("AMCREST_PORT") or 80)
|
||||||
amcrest_username = os.getenv("AMCREST_USERNAME") or "admin"
|
amcrest_username = os.getenv("AMCREST_USERNAME") or "admin"
|
||||||
amcrest_password = os.getenv("AMCREST_PASSWORD")
|
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_host = os.getenv("MQTT_HOST") or "localhost"
|
||||||
mqtt_qos = int(os.getenv("MQTT_QOS") or 0)
|
mqtt_qos = int(os.getenv("MQTT_QOS") or 0)
|
||||||
mqtt_port = int(os.getenv("MQTT_PORT") or 1883)
|
mqtt_port = int(os.getenv("MQTT_PORT") or 1883)
|
||||||
mqtt_username = os.getenv("MQTT_USERNAME")
|
mqtt_username = os.getenv("MQTT_USERNAME")
|
||||||
mqtt_password = os.getenv("MQTT_PASSWORD") # can be None
|
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 = os.getenv("HOME_ASSISTANT") == "true"
|
||||||
home_assistant_prefix = os.getenv("HOME_ASSISTANT_PREFIX") or "homeassistant"
|
home_assistant_prefix = os.getenv("HOME_ASSISTANT_PREFIX") or "homeassistant"
|
||||||
|
|
||||||
|
def read_file(file_name):
|
||||||
|
with open(file_name, 'r') as file:
|
||||||
|
data = file.read().replace('\n', '')
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
def read_version():
|
||||||
|
if os.path.isfile("./VERSION"):
|
||||||
|
return read_file("./VERSION")
|
||||||
|
|
||||||
|
return read_file("../VERSION")
|
||||||
|
|
||||||
# Helper functions and callbacks
|
# Helper functions and callbacks
|
||||||
def log(msg, level="INFO"):
|
def log(msg, level="INFO"):
|
||||||
ts = datetime.now(timezone.utc).strftime("%d/%m/%Y %H:%M:%S")
|
ts = datetime.now(timezone.utc).strftime("%d/%m/%Y %H:%M:%S")
|
||||||
@@ -66,9 +83,9 @@ def exit_gracefully(rc, skip_mqtt=False):
|
|||||||
os._exit(rc)
|
os._exit(rc)
|
||||||
|
|
||||||
def refresh_storage_sensors():
|
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...")
|
log("Fetching storage sensors...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -102,6 +119,10 @@ if mqtt_username is None:
|
|||||||
log("Please set the MQTT_USERNAME environment variable", level="ERROR")
|
log("Please set the MQTT_USERNAME environment variable", level="ERROR")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
version = read_version()
|
||||||
|
|
||||||
|
log(f"App Version: {version}")
|
||||||
|
|
||||||
# Handle interruptions
|
# Handle interruptions
|
||||||
signal.signal(signal.SIGINT, signal_handler)
|
signal.signal(signal.SIGINT, signal_handler)
|
||||||
|
|
||||||
@@ -129,6 +150,7 @@ log(f"Device name: {device_name}")
|
|||||||
|
|
||||||
# MQTT topics
|
# MQTT topics
|
||||||
topics = {
|
topics = {
|
||||||
|
"config": f"amcrest2mqtt/{serial_number}/config",
|
||||||
"status": f"amcrest2mqtt/{serial_number}/status",
|
"status": f"amcrest2mqtt/{serial_number}/status",
|
||||||
"event": f"amcrest2mqtt/{serial_number}/event",
|
"event": f"amcrest2mqtt/{serial_number}/event",
|
||||||
"motion": f"amcrest2mqtt/{serial_number}/motion",
|
"motion": f"amcrest2mqtt/{serial_number}/motion",
|
||||||
@@ -152,8 +174,27 @@ mqtt_client = mqtt.Client(
|
|||||||
client_id=f"amcrest2mqtt_{serial_number}", clean_session=False
|
client_id=f"amcrest2mqtt_{serial_number}", clean_session=False
|
||||||
)
|
)
|
||||||
mqtt_client.on_disconnect = on_mqtt_disconnect
|
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)
|
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:
|
try:
|
||||||
mqtt_client.connect(mqtt_host, port=mqtt_port)
|
mqtt_client.connect(mqtt_host, port=mqtt_port)
|
||||||
@@ -222,48 +263,58 @@ if home_assistant:
|
|||||||
json=True,
|
json=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
mqtt_publish(
|
if storage_poll_interval > 0:
|
||||||
topics["home_assistant"]["storage_used_percent"],
|
mqtt_publish(
|
||||||
base_config
|
topics["home_assistant"]["storage_used_percent"],
|
||||||
| {
|
base_config
|
||||||
"state_topic": topics["storage_used_percent"],
|
| {
|
||||||
"unit_of_measurement": "%",
|
"state_topic": topics["storage_used_percent"],
|
||||||
"icon": "mdi:micro-sd",
|
"unit_of_measurement": "%",
|
||||||
"name": f"{device_name} Storage Used %",
|
"icon": "mdi:micro-sd",
|
||||||
"unique_id": f"{serial_number}.storage_used_percent",
|
"name": f"{device_name} Storage Used %",
|
||||||
},
|
"unique_id": f"{serial_number}.storage_used_percent",
|
||||||
json=True,
|
},
|
||||||
)
|
json=True,
|
||||||
|
)
|
||||||
|
|
||||||
mqtt_publish(
|
mqtt_publish(
|
||||||
topics["home_assistant"]["storage_used"],
|
topics["home_assistant"]["storage_used"],
|
||||||
base_config
|
base_config
|
||||||
| {
|
| {
|
||||||
"state_topic": topics["storage_used"],
|
"state_topic": topics["storage_used"],
|
||||||
"unit_of_measurement": "GB",
|
"unit_of_measurement": "GB",
|
||||||
"icon": "mdi:micro-sd",
|
"icon": "mdi:micro-sd",
|
||||||
"name": f"{device_name} Storage Used",
|
"name": f"{device_name} Storage Used",
|
||||||
"unique_id": f"{serial_number}.storage_used",
|
"unique_id": f"{serial_number}.storage_used",
|
||||||
},
|
},
|
||||||
json=True,
|
json=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
mqtt_publish(
|
mqtt_publish(
|
||||||
topics["home_assistant"]["storage_total"],
|
topics["home_assistant"]["storage_total"],
|
||||||
base_config
|
base_config
|
||||||
| {
|
| {
|
||||||
"state_topic": topics["storage_total"],
|
"state_topic": topics["storage_total"],
|
||||||
"unit_of_measurement": "GB",
|
"unit_of_measurement": "GB",
|
||||||
"icon": "mdi:micro-sd",
|
"icon": "mdi:micro-sd",
|
||||||
"name": f"{device_name} Storage Total",
|
"name": f"{device_name} Storage Total",
|
||||||
"unique_id": f"{serial_number}.storage_total",
|
"unique_id": f"{serial_number}.storage_total",
|
||||||
},
|
},
|
||||||
json=True,
|
json=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Main loop
|
# Main loop
|
||||||
mqtt_publish(topics["status"], "online")
|
mqtt_publish(topics["status"], "online")
|
||||||
refresh_storage_sensors()
|
mqtt_publish(topics["config"], {
|
||||||
|
"version": version,
|
||||||
|
"device_type": device_type,
|
||||||
|
"device_name": device_name,
|
||||||
|
"sw_version": sw_version,
|
||||||
|
"serial_number": serial_number,
|
||||||
|
}, json=True)
|
||||||
|
|
||||||
|
if storage_poll_interval > 0:
|
||||||
|
refresh_storage_sensors()
|
||||||
|
|
||||||
log("Listening for events...")
|
log("Listening for events...")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user