Skip to content

Commit

Permalink
Add service for setting video profile to day or night. Fix service sc…
Browse files Browse the repository at this point in the history
…oping to dahua cameras
  • Loading branch information
rroller committed Jun 18, 2021
1 parent ae638c4 commit 7dc0e3e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -88,6 +88,7 @@ Service | Parameters | Description
`camera.enable_motion_detection` | | Enables motion detection
`camera.disable_motion_detection` | | Disabled motion detection
`dahua.set_infrared_mode` | `entity_id`: camera.cam13_main <br /> `mode`: Auto, On, Off <br /> `brightness`: 0 - 100 inclusive| Sets the infrared mode. Useful to set the mode back to Auto
`dahua.set_video_profile_mode` | `entity_id`: camera.cam13_main <br /> `mode`: Day, Night| Sets the video profile mode to day or night

## Camera
This will provide a normal HA camera entity (can take snapshots, etc)
Expand Down
36 changes: 29 additions & 7 deletions custom_components/dahua/camera.py
Expand Up @@ -23,6 +23,8 @@

# This service handled setting the infrared mode on the camera to Off, Auto, or Manual... along with the brightness
SERVICE_SET_INFRARED_MODE = "set_infrared_mode"
# This service handles setting the video profile mode to day or night
SERVICE_SET_VIDEO_PROFILE_MODE = "set_video_profile_mode"

# For now we'll only support 1 channel. I don't have any cams where I can test a second channel.
# I'm not really sure what channel 2 means anyways, it doesn't seem to be the substream.
Expand Down Expand Up @@ -57,18 +59,33 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
]
)

platform = entity_platform.async_get_current_platform()

# https://developers.home-assistant.io/docs/dev_101_services/
# This will expose a service to enable setting the cameras infrared light to Auto, Manual, and Off along with the brightness
# "async_set_video_profile_mode" is called upon calling the service. Defined below in the DahuaCamera class
platform.async_register_entity_service(
SERVICE_SET_VIDEO_PROFILE_MODE,
{
vol.Required("mode"): vol.In(
[
"Day",
"day",
"Night",
"night",
])
},
"async_set_video_profile_mode"
)

# Exposes a service to enable setting the cameras infrared light to Auto, Manual, and Off along with the brightness
if coordinator.supports_infrared_light():
# method_name is the method that will be called upon calling the service. It's defined below in the DahuaCamera class
method_name = "async_set_infrared_mode"
platform = entity_platform.async_get_current_platform()
# "async_set_infrared_mode" is the method called upon calling the service. Defined below in DahuaCamera class
platform.async_register_entity_service(
SERVICE_SET_INFRARED_MODE,
{
vol.Required("mode"): vol.In(
[
"On", # Dahua uses Manual but that's akward so we'll use On and translate it before we call the Dahua API
"On", # Dahua uses Manual but that's awkward so use On and translate before we call the cam API
"on",
"Off",
"off",
Expand All @@ -77,7 +94,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
]),
vol.Optional('brightness', default=100): vol.All(vol.Coerce(int), vol.Range(min=0, max=100))
},
method_name
"async_set_infrared_mode"
)


Expand Down Expand Up @@ -145,4 +162,9 @@ def name(self):
async def async_set_infrared_mode(self, mode: str, brightness: int):
""" Handles the service call from SERVICE_SET_INFRARED_MODE to set infrared mode and brightness """
await self.coordinator.client.async_set_lighting_v1_mode(mode, brightness)
await self.coordinator.async_refresh()
await self.coordinator.async_refresh()

async def async_set_video_profile_mode(self, mode: str):
""" Handles the service call from SERVICE_SET_VIDEO_PROFILE_MODE to set profile mode to day/night """
await self.coordinator.client.async_set_video_profile_mode(mode)
await self.coordinator.async_refresh()
22 changes: 22 additions & 0 deletions custom_components/dahua/client.py
Expand Up @@ -237,6 +237,28 @@ async def async_set_lighting_v1_mode(self, mode: str, brightness: int) -> dict:
)
return await self.api_wrapper("get", url, headers=HEADERS)

async def async_set_video_profile_mode(self, mode: str):
"""
async_set_video_profile_mode will set camera's profile mode to day or night
Mode should be one of: Day or Night
"""

if mode.lower() == "night":
mode = "1"
else:
# Default to "day", which is 0
mode = "0"

url = "http://{0}/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]={1}".format(
self._address_with_port, mode
)
value = await self.api_wrapper("get", url, headers=HEADERS)
if "OK" in value or "ok" in value:
return
else:
_LOGGER.error("Something really wrong happened! - %s", value)
raise Exception("Could not set video profile mode")

async def async_set_lighting_v2(self, enabled: bool, brightness: int) -> dict:
"""
async_set_lighting_v2 will turn on or off the white light on the camera. If turning on, the brightness will be used.
Expand Down
24 changes: 21 additions & 3 deletions custom_components/dahua/services.yaml
Expand Up @@ -3,10 +3,11 @@
set_infrared_mode:
name: Set Infrared Mode on Dahua Camera
description: Set the infrared light settings on a Dahua camera
target:
entity:
integration: dahua
domain: camera
fields:
entity_id:
description: Entity ID of the camera to update
example: "light.cam_14"
mode:
description: "The infrared mode: Auto, On, Off"
example: "Auto"
Expand All @@ -27,3 +28,20 @@ set_infrared_mode:
max: 100
step: 1
mode: slider

set_video_profile_mode:
name: Set Dahua Video Profile Mode To Day or Night
description: Sets the video profile mode to day or night
target:
entity:
integration: dahua
domain: camera
fields:
mode:
description: "The profile: Day, Night"
example: "Day"
selector:
select:
options:
- "Day"
- "Night"

0 comments on commit 7dc0e3e

Please sign in to comment.