Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
myhomeiot committed Mar 26, 2021
1 parent 4c642ca commit aeea10f
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 2 deletions.
126 changes: 124 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,124 @@
# DahuaVTO
Control Dahua VTO/VTH devices from Home Assistant
A Home Assistant custom integration for control Dahua VTO/VTH devices.

The following device types are currently tested and worked:
* VTO2111D
* VTH5221D

# Installation:

Copy the `dahua_vto` folder and all of its contents into your Home Assistant's custom_components folder. This is often located inside of your /config folder. If you are running Hass.io, use SAMBA to copy the folder over. If you are running Home Assistant Supervised, the custom_components folder might be located at /usr/share/hassio/homeassistant. It is possible that your custom_components folder does not exist. If that is the case, create the folder in the proper location, and then copy the `dahua_vto` folder and all of its contents inside the newly created custom_components folder.

Alternatively, you can install localtuya through HACS by adding this repository.

# Configuration Examples

Add the following to your `configuration.yaml` file:
```yaml
sensor:
- platform: dahua_vto
name: NAME_HERE
host: HOST_HERE
port: PORT_HERE optional, default is 5000
username: USERNAME_HERE_OR_secrets.yaml
password: PASSWORD_HERE_OR_secrets.yaml
scan_interval: INTERVAL_HERE optional, default 60
```
Example:
```yaml
- platform: dahua_vto
name: Dahua VTO
host: 192.168.1.2
username: admin
password: password
scan_interval: 5
```
#### Lock example with call to open door service
```yaml
timer:
door_lock:
name: Door Lock
icon: mdi:timer

lock:
- platform: template
name: Door Lock
value_template: "{{ not is_state('timer.door_lock', 'active') }}"
lock:
unlock:
- service: dahua_vto.open_door
data_template:
entity_id: sensor.dahua_vto
channel: 1
short_number: HA
```
#### Automation example with doorbell ring and unlock events
```yaml
- alias: Dahua VTO All Event
mode: queued
trigger:
- platform: event
event_type: dahua_vto
action:
- service: persistent_notification.create
data:
title: "{{ trigger.event.data.Code }}"
message: "{{ trigger.event.data }}"

- alias: Dahua VTO
mode: queued
trigger:
- platform: event
event_type: dahua_vto
event_data:
Code: BackKeyLight
action:
- choose:
- conditions: >
{{ trigger.event.data.Data.State | int in [0, 1, 2, 6] }}
sequence:
- service: persistent_notification.create
data:
title: Doorbell Ring
message: "{{ trigger.event.data }}"
- conditions: >
{{ trigger.event.data.Data.State | int == 8 }}
sequence:
- service: timer.start
data:
entity_id: timer.door_lock
duration: 00:00:02 # VTO Unlock Period
- service: persistent_notification.create
data:
title: Unlock
message: "{{ trigger.event.data }}"
- conditions: >
{{ trigger.event.data.Data.State | int == 9 }}
sequence:
- service: persistent_notification.create
data:
title: Unlock failed
message: "{{ trigger.event.data }}"
default:
- service: persistent_notification.create
data:
title: "Unknown state {{ trigger.event.data.Data.State | int }}"
message: "{{ trigger.event.data }}"
```
# Debugging
Whenever you write a bug report, it helps tremendously if you include debug logs directly (otherwise we will just ask for them and it will take longer). So please enable debug logs like this and include them in your issue:
```yaml
logger:
default: warning
logs:
custom_components.dahua_vto: debug
```
# Thanks to:
[@elad-bar](https://github.com/elad-bar/DahuaVTO2MQTT)
[@riogrande75](https://github.com/riogrande75/Dahua)
45 changes: 45 additions & 0 deletions custom_components/dahua_vto/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import logging
import asyncio

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant.const import CONF_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from .sensor import DOMAIN

_LOGGER = logging.getLogger(__name__)

CONF_CHANNEL = "channel"
CONF_SHORT_NUMBER = "short_number"
DEFAULT_SHORT_NUMBER = "HA"

SERVICE_OPEN_DOOR = "open_door"
SERVICE_OPEN_DOOR_SCHEMA = vol.Schema(
{
vol.Required(CONF_ENTITY_ID): cv.string,
vol.Required(CONF_CHANNEL): int,
vol.Optional(CONF_SHORT_NUMBER, default=DEFAULT_SHORT_NUMBER): cv.string,
}
)


async def async_setup(hass: HomeAssistant, config: dict):
async def service_open_door(event):
for entry in hass.data[DOMAIN]:
entity = hass.data[DOMAIN][entry]
if entity.entity_id == event.data[CONF_ENTITY_ID]:
if entity.protocol is None:
raise HomeAssistantError("not connected")
try:
return await entity.protocol.open_door(event.data[CONF_CHANNEL] - 1, event.data[CONF_SHORT_NUMBER])
except asyncio.TimeoutError:
raise HomeAssistantError("timeout")
else:
raise HomeAssistantError("entity not found")

hass.data.setdefault(DOMAIN, {})
hass.helpers.service.async_register_admin_service(
DOMAIN, SERVICE_OPEN_DOOR, service_open_door, schema=SERVICE_OPEN_DOOR_SCHEMA
)
return True
9 changes: 9 additions & 0 deletions custom_components/dahua_vto/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"domain": "dahua_vto",
"name": "Dahua VTO",
"version": "1.0.0",
"documentation": "",
"dependencies": [],
"codeowners": [],
"requirements": []
}
Loading

0 comments on commit aeea10f

Please sign in to comment.