Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1a4718db0c | ||
|
|
cfcee7dd2d | ||
|
|
ad323ec8ef | ||
|
|
8b6ee051d4 | ||
|
|
fd2123535d | ||
|
|
477aa89606 | ||
|
|
8e5f0620cc | ||
|
|
cc5e374419 | ||
|
|
afaef732c5 | ||
|
|
4cd3b1add8 | ||
|
|
766aa50499 | ||
|
|
594a42320f | ||
|
|
2886e5c1ca | ||
|
|
9ba2544237 | ||
|
|
0ccdc6fe11 | ||
|
|
ed38b86eb6 | ||
|
|
9d3d44f36b | ||
|
|
8cb2cc2327 | ||
|
|
4d4961e842 | ||
|
|
cbb1995deb | ||
|
|
781becf0d0 | ||
|
|
8a1b1945ff | ||
|
|
65623b45a2 | ||
|
|
4af6eac710 | ||
|
|
7bbe3cbcf4 | ||
|
|
8e96fc5408 | ||
|
|
0f8858cef4 | ||
|
|
e43cda557a | ||
|
|
7dff1b3bf4 | ||
|
|
28f2761899 |
11
.github/workflows/publish.yml
vendored
11
.github/workflows/publish.yml
vendored
@@ -12,17 +12,16 @@ jobs:
|
|||||||
runs-on: ubuntu-20.04
|
runs-on: ubuntu-20.04
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v2.3.4
|
uses: actions/checkout@v2.4.0
|
||||||
- name: Bump version
|
- name: Bump version
|
||||||
uses: remorses/bump-version@js
|
uses: remorses/bump-version@js
|
||||||
id: version
|
id: version
|
||||||
with:
|
with:
|
||||||
version_file: ./VERSION
|
version_file: ./VERSION
|
||||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v2.3.4
|
uses: actions/checkout@v2.4.0
|
||||||
with:
|
with:
|
||||||
ref: main
|
ref: main
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
@@ -30,14 +29,14 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
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
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v1.5.1
|
uses: docker/setup-buildx-action@v1.6.0
|
||||||
- name: Login to DockerHub
|
- name: Login to DockerHub
|
||||||
uses: docker/login-action@v1.10.0
|
uses: docker/login-action@v1.12.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.6.1
|
uses: docker/build-push-action@v2.7.0
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
push: true
|
push: true
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ COPY requirements.txt /
|
|||||||
RUN pip install --no-warn-script-location --prefix=/install -r /requirements.txt
|
RUN pip install --no-warn-script-location --prefix=/install -r /requirements.txt
|
||||||
|
|
||||||
FROM base
|
FROM base
|
||||||
|
STOPSIGNAL SIGINT
|
||||||
COPY --from=builder /install /usr/local
|
COPY --from=builder /install /usr/local
|
||||||
COPY src /app
|
COPY src /app
|
||||||
COPY VERSION /app
|
COPY VERSION /app
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ It supports the following environment variables:
|
|||||||
- `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)
|
||||||
|
- `DEVICE_NAME` (optional) - override the default device name used in the Amcrest app
|
||||||
|
|
||||||
It exposes events to the following topics:
|
It exposes events to the following topics:
|
||||||
|
|
||||||
@@ -28,6 +29,7 @@ It exposes events to the following topics:
|
|||||||
- `amcrest2mqtt/[SERIAL_NUMBER]/doorbell` - doorbell status (if AD110 or AD410)
|
- `amcrest2mqtt/[SERIAL_NUMBER]/doorbell` - doorbell status (if AD110 or AD410)
|
||||||
- `amcrest2mqtt/[SERIAL_NUMBER]/human` - human detection (if AD410)
|
- `amcrest2mqtt/[SERIAL_NUMBER]/human` - human detection (if AD410)
|
||||||
- `amcrest2mqtt/[SERIAL_NUMBER]/motion` - motion events (if supported)
|
- `amcrest2mqtt/[SERIAL_NUMBER]/motion` - motion events (if supported)
|
||||||
|
- `amcrest2mqtt/[SERIAL_NUMBER]/config` - device configuration information
|
||||||
|
|
||||||
## Device Support
|
## Device Support
|
||||||
|
|
||||||
@@ -62,7 +64,7 @@ services:
|
|||||||
|
|
||||||
### Multiple Devices
|
### Multiple Devices
|
||||||
|
|
||||||
The app will not support multiple devices. You can run multiple instances of the app if you need to expose events for multiple devies.
|
The app will not support multiple devices. You can run multiple instances of the app if you need to expose events for multiple devices.
|
||||||
|
|
||||||
### Non-Docker Environments
|
### Non-Docker Environments
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
amcrest==1.7.2
|
amcrest==1.9.3
|
||||||
paho-mqtt==1.5.1
|
paho-mqtt==1.6.1
|
||||||
python-slugify==5.0.2
|
python-slugify==5.0.2
|
||||||
|
|||||||
@@ -17,7 +17,9 @@ 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)
|
storage_poll_interval = int(os.getenv("STORAGE_POLL_INTERVAL") or 3600)
|
||||||
|
device_name = os.getenv("DEVICE_NAME")
|
||||||
|
|
||||||
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)
|
||||||
@@ -73,6 +75,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)
|
||||||
@@ -90,12 +94,24 @@ def refresh_storage_sensors():
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
storage = camera.storage_all
|
storage = camera.storage_all
|
||||||
|
|
||||||
mqtt_publish(topics["storage_used_percent"], str(storage["used_percent"]))
|
mqtt_publish(topics["storage_used_percent"], str(storage["used_percent"]))
|
||||||
mqtt_publish(topics["storage_used"], str(storage["used"][0]))
|
mqtt_publish(topics["storage_used"], to_gb(storage["used"]))
|
||||||
mqtt_publish(topics["storage_total"], str(storage["total"][0]))
|
mqtt_publish(topics["storage_total"], to_gb(storage["total"]))
|
||||||
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 to_gb(total):
|
||||||
|
return str(round(float(total[0]) / 1024 / 1024 / 1024, 2))
|
||||||
|
|
||||||
|
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
|
||||||
@@ -134,14 +150,25 @@ camera = AmcrestCamera(
|
|||||||
# Fetch camera details
|
# Fetch camera details
|
||||||
log("Fetching camera details...")
|
log("Fetching camera details...")
|
||||||
|
|
||||||
|
try:
|
||||||
device_type = camera.device_type.replace("type=", "").strip()
|
device_type = camera.device_type.replace("type=", "").strip()
|
||||||
is_ad110 = device_type == "AD110"
|
is_ad110 = device_type == "AD110"
|
||||||
is_ad410 = device_type == "AD410"
|
is_ad410 = device_type == "AD410"
|
||||||
is_doorbell = is_ad110 or is_ad410
|
is_doorbell = is_ad110 or is_ad410
|
||||||
serial_number = camera.serial_number.strip()
|
serial_number = camera.serial_number
|
||||||
|
|
||||||
|
if not isinstance(serial_number, str):
|
||||||
|
log(f"Error fetching serial number", level="ERROR")
|
||||||
|
exit_gracefully(1)
|
||||||
|
|
||||||
sw_version = camera.software_information[0].replace("version=", "").strip()
|
sw_version = camera.software_information[0].replace("version=", "").strip()
|
||||||
|
if not device_name:
|
||||||
device_name = camera.machine_name.replace("name=", "").strip()
|
device_name = camera.machine_name.replace("name=", "").strip()
|
||||||
|
|
||||||
device_slug = slugify(device_name, separator="_")
|
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}")
|
||||||
@@ -166,6 +193,9 @@ topics = {
|
|||||||
"storage_used": f"{home_assistant_prefix}/sensor/amcrest2mqtt-{serial_number}/{device_slug}_storage_used/config",
|
"storage_used": f"{home_assistant_prefix}/sensor/amcrest2mqtt-{serial_number}/{device_slug}_storage_used/config",
|
||||||
"storage_used_percent": f"{home_assistant_prefix}/sensor/amcrest2mqtt-{serial_number}/{device_slug}_storage_used_percent/config",
|
"storage_used_percent": f"{home_assistant_prefix}/sensor/amcrest2mqtt-{serial_number}/{device_slug}_storage_used_percent/config",
|
||||||
"storage_total": f"{home_assistant_prefix}/sensor/amcrest2mqtt-{serial_number}/{device_slug}_storage_total/config",
|
"storage_total": f"{home_assistant_prefix}/sensor/amcrest2mqtt-{serial_number}/{device_slug}_storage_total/config",
|
||||||
|
"version": f"{home_assistant_prefix}/sensor/amcrest2mqtt-{serial_number}/{device_slug}_version/config",
|
||||||
|
"host": f"{home_assistant_prefix}/sensor/amcrest2mqtt-{serial_number}/{device_slug}_host/config",
|
||||||
|
"serial_number": f"{home_assistant_prefix}/sensor/amcrest2mqtt-{serial_number}/{device_slug}_serial_number/config",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,6 +258,7 @@ if home_assistant:
|
|||||||
"state_topic": topics["doorbell"],
|
"state_topic": topics["doorbell"],
|
||||||
"payload_on": "on",
|
"payload_on": "on",
|
||||||
"payload_off": "off",
|
"payload_off": "off",
|
||||||
|
"icon": "mdi:doorbell",
|
||||||
"name": f"{device_name} Doorbell",
|
"name": f"{device_name} Doorbell",
|
||||||
"unique_id": f"{serial_number}.doorbell",
|
"unique_id": f"{serial_number}.doorbell",
|
||||||
},
|
},
|
||||||
@@ -263,6 +294,51 @@ if home_assistant:
|
|||||||
json=True,
|
json=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
mqtt_publish(
|
||||||
|
topics["home_assistant"]["version"],
|
||||||
|
base_config
|
||||||
|
| {
|
||||||
|
"state_topic": topics["config"],
|
||||||
|
"value_template": "{{ value_json.sw_version }}",
|
||||||
|
"icon": "mdi:package-up",
|
||||||
|
"name": f"{device_name} Version",
|
||||||
|
"unique_id": f"{serial_number}.version",
|
||||||
|
"entity_category": "diagnostic",
|
||||||
|
"enabled_by_default": False
|
||||||
|
},
|
||||||
|
json=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
mqtt_publish(
|
||||||
|
topics["home_assistant"]["serial_number"],
|
||||||
|
base_config
|
||||||
|
| {
|
||||||
|
"state_topic": topics["config"],
|
||||||
|
"value_template": "{{ value_json.serial_number }}",
|
||||||
|
"icon": "mdi:alphabetical-variant",
|
||||||
|
"name": f"{device_name} Serial Number",
|
||||||
|
"unique_id": f"{serial_number}.serial_number",
|
||||||
|
"entity_category": "diagnostic",
|
||||||
|
"enabled_by_default": False
|
||||||
|
},
|
||||||
|
json=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
mqtt_publish(
|
||||||
|
topics["home_assistant"]["host"],
|
||||||
|
base_config
|
||||||
|
| {
|
||||||
|
"state_topic": topics["config"],
|
||||||
|
"value_template": "{{ value_json.host }}",
|
||||||
|
"icon": "mdi:ip-network",
|
||||||
|
"name": f"{device_name} Host",
|
||||||
|
"unique_id": f"{serial_number}.host",
|
||||||
|
"entity_category": "diagnostic",
|
||||||
|
"enabled_by_default": False
|
||||||
|
},
|
||||||
|
json=True,
|
||||||
|
)
|
||||||
|
|
||||||
if storage_poll_interval > 0:
|
if storage_poll_interval > 0:
|
||||||
mqtt_publish(
|
mqtt_publish(
|
||||||
topics["home_assistant"]["storage_used_percent"],
|
topics["home_assistant"]["storage_used_percent"],
|
||||||
@@ -273,6 +349,7 @@ if home_assistant:
|
|||||||
"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_percent",
|
"unique_id": f"{serial_number}.storage_used_percent",
|
||||||
|
"entity_category": "diagnostic",
|
||||||
},
|
},
|
||||||
json=True,
|
json=True,
|
||||||
)
|
)
|
||||||
@@ -286,6 +363,7 @@ if home_assistant:
|
|||||||
"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",
|
||||||
|
"entity_category": "diagnostic",
|
||||||
},
|
},
|
||||||
json=True,
|
json=True,
|
||||||
)
|
)
|
||||||
@@ -299,6 +377,7 @@ if home_assistant:
|
|||||||
"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",
|
||||||
|
"entity_category": "diagnostic",
|
||||||
},
|
},
|
||||||
json=True,
|
json=True,
|
||||||
)
|
)
|
||||||
@@ -311,15 +390,18 @@ mqtt_publish(topics["config"], {
|
|||||||
"device_name": device_name,
|
"device_name": device_name,
|
||||||
"sw_version": sw_version,
|
"sw_version": sw_version,
|
||||||
"serial_number": serial_number,
|
"serial_number": serial_number,
|
||||||
|
"host": amcrest_host,
|
||||||
}, json=True)
|
}, json=True)
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user