1
0
mirror of synced 2026-03-10 04:23:39 +00:00

Compare commits

9 Commits
1.0.6 ... 1.0.9

Author SHA1 Message Date
Daniel Chesterton
8e96fc5408 Add ping functionality to stop app hanging 2021-08-07 21:26:07 +01:00
bump_version
0f8858cef4 Version 1.0.8 [skip ci] 2021-08-03 17:10:40 +00:00
Daniel Chesterton
e43cda557a Merge pull request #31 from dchesterton/dependabot/pip/amcrest-1.8.0
Bump amcrest from 1.7.2 to 1.8.0
2021-08-03 18:10:19 +01:00
dependabot[bot]
7dff1b3bf4 Bump amcrest from 1.7.2 to 1.8.0
Bumps [amcrest](https://github.com/tchellomello/python-amcrest) from 1.7.2 to 1.8.0.
- [Release notes](https://github.com/tchellomello/python-amcrest/releases)
- [Commits](https://github.com/tchellomello/python-amcrest/compare/1.7.2...1.8.0)

---
updated-dependencies:
- dependency-name: amcrest
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2021-08-02 12:03:54 +00:00
bump_version
28f2761899 Version 1.0.7 [skip ci] 2021-07-27 17:01:21 +00:00
Daniel Chesterton
17854bda0f Merge pull request #29 from bennycooly/feature/mqtt-tls
Add TLS support
2021-07-27 18:01:02 +01:00
Ben Fu
88323631ea Update README with TLS vars 2021-07-26 10:40:26 -05:00
Ben Fu
2a56eee4f5 Add TLS support 2021-07-26 09:51:18 -05:00
bump_version
caacaea81f Version 1.0.6 [skip ci] 2021-07-18 20:23:21 +00:00
4 changed files with 56 additions and 12 deletions

View File

@@ -14,6 +14,10 @@ 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) - `STORAGE_POLL_INTERVAL` (optional, default = 3600) - how often to fetch storage data (in seconds)

View File

@@ -1 +1 @@
1.0.5 1.0.8

View File

@@ -1,3 +1,3 @@
amcrest==1.7.2 amcrest==1.8.0
paho-mqtt==1.5.1 paho-mqtt==1.5.1
python-slugify==5.0.2 python-slugify==5.0.2

View File

@@ -7,6 +7,7 @@ import sys
from json import dumps from json import dumps
import signal import signal
from threading import Timer from threading import Timer
import ssl
is_exiting = False is_exiting = False
mqtt_client = None mqtt_client = None
@@ -23,6 +24,10 @@ 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"
@@ -68,6 +73,8 @@ def on_mqtt_disconnect(client, userdata, rc):
def exit_gracefully(rc, skip_mqtt=False): def exit_gracefully(rc, skip_mqtt=False):
global topics, mqtt_client global topics, mqtt_client
log("Exiting app...")
if mqtt_client is not None and mqtt_client.is_connected() and skip_mqtt == False: 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_publish(topics["status"], "offline", exit_on_error=False)
mqtt_client.loop_stop(force=True) mqtt_client.loop_stop(force=True)
@@ -91,6 +98,14 @@ def refresh_storage_sensors():
except AmcrestError as error: except AmcrestError as error:
log(f"Error fetching storage information {error}", level="WARNING") 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): def signal_handler(sig, frame):
# exit immediately upon receiving a second SIGINT # exit immediately upon receiving a second SIGINT
global is_exiting global is_exiting
@@ -129,14 +144,18 @@ camera = AmcrestCamera(
# Fetch camera details # Fetch camera details
log("Fetching camera details...") log("Fetching camera details...")
device_type = camera.device_type.replace("type=", "").strip() try:
is_ad110 = device_type == "AD110" device_type = camera.device_type.replace("type=", "").strip()
is_ad410 = device_type == "AD410" is_ad110 = device_type == "AD110"
is_doorbell = is_ad110 or is_ad410 is_ad410 = device_type == "AD410"
serial_number = camera.serial_number.strip() is_doorbell = is_ad110 or is_ad410
sw_version = camera.software_information[0].replace("version=", "").strip() serial_number = camera.serial_number.strip()
device_name = camera.machine_name.replace("name=", "").strip() sw_version = camera.software_information[0].replace("version=", "").strip()
device_slug = slugify(device_name, separator="_") 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"Device type: {device_type}")
log(f"Serial number: {serial_number}") log(f"Serial number: {serial_number}")
@@ -169,8 +188,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)
@@ -292,10 +330,12 @@ mqtt_publish(topics["config"], {
if storage_poll_interval > 0: if storage_poll_interval > 0:
refresh_storage_sensors() refresh_storage_sensors()
ping_camera()
log("Listening for events...") log("Listening for events...")
try: 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): if (is_ad110 and code == "ProfileAlarmTransmit") or (code == "VideoMotion" and not is_ad110):
motion_payload = "on" if payload["action"] == "Start" else "off" motion_payload = "on" if payload["action"] == "Start" else "off"
mqtt_publish(topics["motion"], motion_payload) mqtt_publish(topics["motion"], motion_payload)