-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,7 @@ | ||
import logging | ||
import asyncio | ||
|
||
import homeassistant.helpers.config_validation as cv | ||
import voluptuous as vol | ||
from homeassistant.const import CONF_ENTITY_ID, CONF_TIMEOUT, CONF_EVENT | ||
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" | ||
CONF_METHOD = "method" | ||
CONF_PARAMS = "params" | ||
CONF_TAG = "tag" | ||
|
||
DEFAULT_TIMEOUT = 5 | ||
|
||
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="HA"): cv.string, | ||
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): int, | ||
} | ||
) | ||
|
||
SERVICE_SEND_COMMAND = "send_command" | ||
SERVICE_SEND_COMMAND_SCHEMA = vol.Schema( | ||
{ | ||
vol.Required(CONF_ENTITY_ID): cv.string, | ||
vol.Required(CONF_METHOD): object, | ||
vol.Optional(CONF_PARAMS, default=None): object, | ||
vol.Optional(CONF_EVENT, default=True): bool, | ||
vol.Optional(CONF_TAG, default=None): object, | ||
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): int, | ||
} | ||
) | ||
|
||
|
||
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], | ||
event.data[CONF_TIMEOUT]) | ||
except asyncio.TimeoutError: | ||
raise HomeAssistantError("timeout") | ||
else: | ||
raise HomeAssistantError("entity not found") | ||
|
||
async def service_send_command(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.send_command( | ||
event.data[CONF_METHOD], | ||
event.data[CONF_PARAMS], | ||
event.data[CONF_EVENT], | ||
event.data[CONF_TAG], | ||
event.data[CONF_TIMEOUT]) | ||
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 | ||
) | ||
hass.helpers.service.async_register_admin_service( | ||
DOMAIN, SERVICE_SEND_COMMAND, service_send_command, | ||
schema=SERVICE_SEND_COMMAND_SCHEMA | ||
) | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,72 @@ | ||
open_door: | ||
description: Open door | ||
name: Open door | ||
description: Open the door | ||
fields: | ||
entity_id: | ||
description: Entity ID of the sensor | ||
example: sensor.dahua_vto | ||
<<: &entity_id | ||
entity_id: | ||
name: Entity ID | ||
description: Entity ID of the Dahua VTO sensor | ||
required: true | ||
example: sensor.dahua_vto | ||
selector: | ||
entity: | ||
domain: sensor | ||
channel: | ||
name: Channel | ||
description: Number of channel starting from 1 | ||
required: true | ||
example: 1 | ||
selector: | ||
number: | ||
min: 1 | ||
max: 15 | ||
short_number: | ||
description: Short number to show in the log as Room No., default HA | ||
name: Short Number | ||
description: Short number to show in the log as Room No. | ||
default: HA | ||
example: HA | ||
timeout: | ||
description: Command execution timeout, default 5 | ||
selector: | ||
text: | ||
<<: &timeout | ||
timeout: | ||
name: Timeout | ||
description: Command execution timeout | ||
default: 5 | ||
example: 5 | ||
selector: | ||
number: | ||
min: 1 | ||
max: 99 | ||
|
||
send_command: | ||
description: Send command | ||
name: Send command | ||
description: Send the command | ||
fields: | ||
entity_id: | ||
description: Entity ID of the sensor | ||
example: sensor.dahua_vto | ||
<<: *entity_id | ||
method: | ||
name: Method | ||
description: "Method name, example: magicBox.getBootParameter" | ||
required: true | ||
example: system.listService | ||
selector: | ||
object: | ||
params: | ||
name: Params | ||
description: "Method parameters, example: {names: ['serverip', 'ver']}" | ||
example: "{names: ['serverip', 'ver']}" | ||
selector: | ||
object: | ||
event: | ||
description: Fire event with result, default true | ||
name: Event | ||
description: Fire event with result | ||
default: True | ||
example: True | ||
selector: | ||
boolean: | ||
tag: | ||
name: Tag | ||
description: "Tag, will be present in event data, example: 1 or {name: tag}" | ||
timeout: | ||
description: Command execution timeout, default 5 | ||
example: "{name: tag}" | ||
selector: | ||
object: | ||
<<: *timeout |