Skip to content

Commit 6fe5c5b

Browse files
committed
Fix illuminator when used with night profile
1 parent 37c5697 commit 6fe5c5b

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

custom_components/dahua/__init__.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,15 @@ async def _async_update_data(self):
186186

187187
self.initialized = True
188188

189+
# We need the profile mode (0=day, 1=night, 2=scene)
190+
mode_data = await self.client.async_get_video_in_mode()
191+
data.update(mode_data)
192+
profile_mode = mode_data.get("table.VideoInMode[0].Config[0]", "0")
193+
189194
# Figure out which APIs we need to call and then fan out and gather the results
190-
coros = [asyncio.ensure_future(self.client.async_common_config())]
195+
coros = [
196+
asyncio.ensure_future(self.client.async_common_config(profile_mode)),
197+
]
191198
if self._supports_disarming_linkage:
192199
coros.append(asyncio.ensure_future(self.client.async_get_disarming_linkage()))
193200
if self._supports_coaxial_control:
@@ -404,7 +411,6 @@ def get_firmware_version(self) -> str:
404411
return self.data.get("version")
405412

406413
def get_serial_number(self) -> str:
407-
408414
""" returns the device serial number. This is unique per device """
409415
return self._serial_number
410416

@@ -427,7 +433,10 @@ def get_infrared_brightness(self) -> int:
427433

428434
def is_illuminator_on(self) -> bool:
429435
"""Return true if the illuminator light is on"""
430-
return self.data.get("table.Lighting_V2[0][0][0].Mode", "") == "Manual"
436+
# profile_mode 0=day, 1=night, 2=scene
437+
profile_mode = self.get_profile_mode()
438+
439+
return self.data.get("table.Lighting_V2[0][" + profile_mode + "][0].Mode", "") == "Manual"
431440

432441
def get_illuminator_brightness(self) -> int:
433442
"""Return the brightness of the illuminator light, as reported by the camera itself, between 0..255 inclusive"""
@@ -439,6 +448,10 @@ def is_security_light_on(self) -> bool:
439448
"""Return true if the security light is on. This is the red/blue flashing light"""
440449
return self.data.get("status.status.WhiteLight", "") == "On"
441450

451+
def get_profile_mode(self) -> str:
452+
# profile_mode 0=day, 1=night, 2=scene
453+
return self.data.get("table.VideoInMode[0].Config[0]", "0")
454+
442455

443456
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
444457
"""Handle removal of an entry."""

custom_components/dahua/client.py

+27-8
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ async def async_get_lighting_v2(self) -> dict:
170170
table.Lighting_V2[0][2][0].PercentOfMaxBrightness=100
171171
table.Lighting_V2[0][2][0].Sensitive=3
172172
"""
173-
url = "http://{0}/cgi-bin/configManager.cgi?action=getConfig&name=Lighting_V2[0][0][0]".format(
173+
url = "http://{0}/cgi-bin/configManager.cgi?action=getConfig&name=Lighting_V2".format(
174174
self._address_with_port
175175
)
176176
return await self.api_wrapper("get", url, headers=HEADERS)
@@ -194,10 +194,11 @@ async def async_get_machine_name(self) -> dict:
194194
)
195195
return await self.api_wrapper("get", url, headers=HEADERS)
196196

197-
async def async_common_config(self) -> dict:
197+
async def async_common_config(self, profile_mode) -> dict:
198198
"""
199199
async_common_config will fetch the status of the IR light (InfraRed light) and motion detection status (if it is
200200
enabled or not)
201+
profile_mode: = 0=day, 1=night, 2=normal scene
201202
202203
Example response:
203204
table.Lighting[0][0].Correction=50
@@ -206,8 +207,8 @@ async def async_common_config(self) -> dict:
206207
table.Lighting[0][0].Mode=Auto
207208
table.Lighting[0][0].Sensitive=3
208209
"""
209-
url = "http://{0}/cgi-bin/configManager.cgi?action=getConfig&name=MotionDetect&action=getConfig&name=Lighting[0][0]".format(
210-
self._address_with_port
210+
url = "http://{0}/cgi-bin/configManager.cgi?action=getConfig&name=MotionDetect&action=getConfig&name=Lighting[0][{1}]".format(
211+
self._address_with_port, profile_mode
211212
)
212213
return await self.api_wrapper("get", url, headers=HEADERS)
213214

@@ -301,7 +302,8 @@ async def async_set_service_set_channel_title(self, channel: int, text1: str, te
301302
if "OK" not in value and "ok" not in value:
302303
raise Exception("Could not set text")
303304

304-
async def async_set_service_set_text_overlay(self, channel: int, group: int, text1: str, text2: str, text3: str, text4: str):
305+
async def async_set_service_set_text_overlay(self, channel: int, group: int, text1: str, text2: str, text3: str,
306+
text4: str):
305307
""" async_set_service_set_text_overlay sets the video text overlay """
306308
text = '|'.join(filter(None, [text1, text2, text3, text4]))
307309
url = "http://{0}/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[{1}].CustomTitle[{2}].Text={3}".format(
@@ -321,23 +323,40 @@ async def async_set_service_set_custom_overlay(self, channel: int, group: int, t
321323
if "OK" not in value and "ok" not in value:
322324
raise Exception("Could not set text")
323325

324-
async def async_set_lighting_v2(self, enabled: bool, brightness: int) -> dict:
326+
async def async_set_lighting_v2(self, enabled: bool, brightness: int, profile_mode: str) -> dict:
325327
"""
326328
async_set_lighting_v2 will turn on or off the white light on the camera. If turning on, the brightness will be used.
327329
brightness is in the range of 0 to 100 inclusive where 100 is the brightest.
328330
NOTE: this is not the same as the infrared (IR) light. This is the white visible light on the camera
331+
332+
profile_mode: 0=day, 1=night, 2=scene
329333
"""
330334

331335
# on = Manual, off = Off
332336
mode = "Manual"
333337
if not enabled:
334338
mode = "Off"
335-
url = "http://{0}/cgi-bin/configManager.cgi?action=setConfig&Lighting_V2[0][0][0].Mode={1}&Lighting_V2[0][0][0].MiddleLight[0].Light={2}".format(
336-
self._address_with_port, mode, brightness
339+
url = "http://{0}/cgi-bin/configManager.cgi?action=setConfig&Lighting_V2[0][{1}][0].Mode={2}&Lighting_V2[0][{3}][0].MiddleLight[0].Light={4}".format(
340+
self._address_with_port, profile_mode, mode, profile_mode, brightness
337341
)
338342
_LOGGER.debug("Turning light on: %s", url)
339343
return await self.api_wrapper("get", url, headers=HEADERS)
340344

345+
async def async_get_video_in_mode(self) -> dict:
346+
"""
347+
async_get_video_in_mode will return the profile mode (day/night)
348+
0 means config for day,
349+
1 means config for night, and
350+
2 means config for normal scene.
351+
352+
table.VideoInMode[0].Config[0]=2
353+
table.VideoInMode[0].Mode=0
354+
table.VideoInMode[0].TimeSection[0][0]=0 00:00:00-24:00:00
355+
"""
356+
357+
url = "http://{0}/cgi-bin/configManager.cgi?action=getConfig&name=VideoInMode".format(self._address_with_port)
358+
return await self.api_wrapper("get", url, headers=HEADERS)
359+
341360
async def async_set_coaxial_control_state(self, dahua_type: int, enabled: bool) -> dict:
342361
"""
343362
async_set_lighting_v2 will turn on or off the white light on the camera.

custom_components/dahua/light.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,17 @@ async def async_turn_on(self, **kwargs):
143143
"""Turn the light on with the current brightness"""
144144
hass_brightness = kwargs.get(ATTR_BRIGHTNESS)
145145
dahua_brightness = dahua_utils.hass_brightness_to_dahua_brightness(hass_brightness)
146-
await self.coordinator.client.async_set_lighting_v2(True, dahua_brightness)
147-
await self.coordinator.async_refresh()
146+
profile_mode = self._coordinator.get_profile_mode()
147+
await self._coordinator.client.async_set_lighting_v2(True, dahua_brightness, profile_mode)
148+
await self._coordinator.async_refresh()
148149

149150
async def async_turn_off(self, **kwargs):
150151
"""Turn the light off"""
151152
hass_brightness = kwargs.get(ATTR_BRIGHTNESS)
152153
dahua_brightness = dahua_utils.hass_brightness_to_dahua_brightness(hass_brightness)
153-
await self.coordinator.client.async_set_lighting_v2(False, dahua_brightness)
154-
await self.coordinator.async_refresh()
154+
profile_mode = self._coordinator.get_profile_mode()
155+
await self._coordinator.client.async_set_lighting_v2(False, dahua_brightness, profile_mode)
156+
await self._coordinator.async_refresh()
155157

156158

157159
class DahuaSecurityLight(DahuaBaseEntity, LightEntity):

0 commit comments

Comments
 (0)