Skip to content

Commit

Permalink
Add support for overriding device name and add optional additional en…
Browse files Browse the repository at this point in the history
…tities
  • Loading branch information
dchesterton committed Dec 31, 2021
1 parent cfcee7d commit 1a4718d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ It supports the following environment variables:
- `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)
- `DEVICE_NAME` (optional) - override the default device name used in the Amcrest app

It exposes events to the following topics:

- `amcrest2mqtt/[SERIAL_NUMBER]/event` - all events
- `amcrest2mqtt/[SERIAL_NUMBER]/doorbell` - doorbell status (if AD110 or AD410)
- `amcrest2mqtt/[SERIAL_NUMBER]/human` - human detection (if AD410)
- `amcrest2mqtt/[SERIAL_NUMBER]/motion` - motion events (if supported)
- `amcrest2mqtt/[SERIAL_NUMBER]/config` - device configuration information

## Device Support

Expand Down Expand Up @@ -62,7 +64,7 @@ services:

### 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

Expand Down
56 changes: 55 additions & 1 deletion src/amcrest2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
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)
device_name = os.getenv("DEVICE_NAME")

mqtt_host = os.getenv("MQTT_HOST") or "localhost"
mqtt_qos = int(os.getenv("MQTT_QOS") or 0)
Expand Down Expand Up @@ -160,7 +162,9 @@ def signal_handler(sig, frame):
exit_gracefully(1)

sw_version = camera.software_information[0].replace("version=", "").strip()
device_name = camera.machine_name.replace("name=", "").strip()
if not device_name:
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")
Expand Down Expand Up @@ -189,6 +193,9 @@ def signal_handler(sig, frame):
"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_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",
},
}

Expand Down Expand Up @@ -251,6 +258,7 @@ def signal_handler(sig, frame):
"state_topic": topics["doorbell"],
"payload_on": "on",
"payload_off": "off",
"icon": "mdi:doorbell",
"name": f"{device_name} Doorbell",
"unique_id": f"{serial_number}.doorbell",
},
Expand Down Expand Up @@ -286,6 +294,51 @@ def signal_handler(sig, frame):
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:
mqtt_publish(
topics["home_assistant"]["storage_used_percent"],
Expand Down Expand Up @@ -337,6 +390,7 @@ def signal_handler(sig, frame):
"device_name": device_name,
"sw_version": sw_version,
"serial_number": serial_number,
"host": amcrest_host,
}, json=True)

if storage_poll_interval > 0:
Expand Down

0 comments on commit 1a4718d

Please sign in to comment.