Skip to content

Commit

Permalink
major rework
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaKorneev committed Aug 21, 2024
1 parent 00fc2fd commit 83103a9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/main/test.py
/main/test2.py


1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ All the channels that spot these events would appear in your HA entities list as
- Face recognition:
- Facerecognition_\<channelNo>

**MIND THAT CHANNELS START FROM 0!** So NVR's channel 1 would be binary_sensor.Smartmotioncar_0
## Contributing
Dahua2MQTT is an open-source project, and contributions are warmly welcomed! Whether you're looking to fix bugs, add new features, or improve documentation, your help is appreciated. Please feel free to fork the repository, make your changes, and submit a pull request.

Expand Down
49 changes: 42 additions & 7 deletions main/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def publish_discovery_config(component, sensor_type, sensor_id, attributes):
auth={'username': MQTT_USERNAME, 'password': MQTT_PASSWORD})


# takes data from Dahua's HTTP alarm
# takes data from Dahua HTTP alarm
# sends it to HA's MQTT discovery for the smart motion detection event
# sends it as MQTT topic with updated states
def smd2mqtt(data):
sensor_id = data.get("Index")
sensor_id = str(int(data.get("Index")) + 1)
sensor_type = data.get("Code")
attributes = {}

Expand All @@ -59,7 +59,8 @@ def smd2mqtt(data):
"state": "ON" if data.get("Action") == "Start" else "OFF",
"attributes": {
"StartTime": data["Data"]["StartTime"],
"Device id": data["Data"].get("uuid", "null"),
"Device IP": data["Data"].get("IP", "null"),
"Channel": int(data["Index"]) + 1
}
}

Expand All @@ -80,11 +81,13 @@ def smd2mqtt(data):

print(f"Event registered: Cam{sensor_id} - {sensor_type}")

# takes data from Dahua's HTTP alarm
# takes data from Dahua HTTP alarm
# sends it to HA's MQTT discovery for the face recognition event
# sends it as MQTT topic with updated states


def fr2mqtt(data):
sensor_id = data.get("Index")
sensor_id = str(int(data.get("Index")) + 1)
sensor_type = data.get("Code")
attributes = {}

Expand Down Expand Up @@ -119,14 +122,43 @@ def fr2mqtt(data):
print(f"Event registered: Cam{sensor_id} - {sensor_type}")


def fd2mqtt(data):
sensor_id = str(int(data.get("Index") + 1))
sensor_type = data.get("Code")
attributes = {}

topic = f"dahua2mqtt/{sensor_type}/{sensor_id}/state"
payload = {
"state": "ON" if data.get("Action") == "Start" else "OFF",
"attributes": {}
}

publish.single(
topic=topic,
payload=json.dumps(payload),
hostname=MQTT_BROKER,
port=MQTT_PORT,
auth=AUTH,
)

publish_discovery_config(
"binary_sensor",
sensor_type,
sensor_id,
attributes,
)

print(f"Event registered: Cam{sensor_id} - {sensor_type}")


app = Flask(__name__)


@app.route(rule='/cgi-bin/NotifyEvent', methods=['POST'])
def dahua_event():
data = request.json
print(data)
data_code = data.get("Code")
print(data_code, data["Index"])

if data_code == 'SmartMotionHuman':
smd2mqtt(data)
Expand All @@ -137,8 +169,11 @@ def dahua_event():
if data_code == "FaceRecognition":
fr2mqtt(data)

if data_code == "FaceDetection":
fd2mqtt(data)

return "Data forwarded to MQTT", 200


if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=52345)
app.run(debug=False, host='0.0.0.0', port=52345)
2 changes: 1 addition & 1 deletion main/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: "Dahua2MQTT"
description: "Get the HTTP alarms from your Dahua NVR (or other) in Home Assistant's MQTT broker"
version: "0.0.3"
version: "0.0.4"
slug: "dahua2mqtt"
init: false
arch:
Expand Down

0 comments on commit 83103a9

Please sign in to comment.