Skip to content

Commit

Permalink
Merge pull request #644 from JurajNyiri/experiment_night_vision_2
Browse files Browse the repository at this point in the history
Fix #631: D230 Unable to set night vision to auto
  • Loading branch information
JurajNyiri authored Jul 31, 2024
2 parents 0e3734b + 4d09246 commit 8814ec7
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 38 deletions.
1 change: 0 additions & 1 deletion custom_components/tapo_control/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
LIGHT = "light"
SOUND = "sound"
PRIVACY_MODE = "privacy_mode"
DAY_NIGHT_MODE = "day_night_mode"
ALARM = "alarm"
LED_MODE = "led_mode"
NAME = "name"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/tapo_control/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"codeowners": [
"@JurajNyiri"
],
"version": "5.5.4",
"version": "5.6.0",
"requirements": [
"pytapo==3.3.30"
],
Expand Down
60 changes: 50 additions & 10 deletions custom_components/tapo_control/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from .const import DOMAIN, LOGGER
from .tapo.entities import TapoSelectEntity
from .utils import check_and_create
from .utils import check_and_create, getNightModeName, getNightModeValue


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Expand All @@ -24,11 +24,36 @@ async def setupEntities(entry):
selects = []

if (
"day_night_mode" in entry["camData"]
and entry["camData"]["day_night_mode"] is not None
"night_vision_mode_switching" in entry["camData"]
and entry["camData"]["night_vision_mode_switching"] is not None
):
tapoNightVisionSelect = TapoNightVisionSelect(entry, hass, config_entry)
LOGGER.debug("Adding tapoNightVisionSelect...")
tapoNightVisionSelect = TapoNightVisionSelect(
entry,
hass,
config_entry,
"Night Vision Switching",
["auto", "on", "off"],
"night_vision_mode_switching",
entry["controller"].setDayNightMode,
)
LOGGER.debug("Adding tapoNightVisionSelect (Night Vision Switching)...")
selects.append(tapoNightVisionSelect)

if (
"night_vision_mode" in entry["camData"]
and entry["camData"]["night_vision_mode"] is not None
and entry["camData"]["night_vision_capability"] is not None
):
tapoNightVisionSelect = TapoNightVisionSelect(
entry,
hass,
config_entry,
"Night Vision",
entry["camData"]["night_vision_capability"],
"night_vision_mode",
entry["controller"].setNightVisionModeConfig,
)
LOGGER.debug("Adding tapoNightVisionSelect (Night Vision)...")
selects.append(tapoNightVisionSelect)

tapoLightFrequencySelect = await check_and_create(
Expand Down Expand Up @@ -316,12 +341,26 @@ def entity_category(self):


class TapoNightVisionSelect(TapoSelectEntity):
def __init__(self, entry: dict, hass: HomeAssistant, config_entry):
self._attr_options = ["auto", "on", "off"]
def __init__(
self,
entry: dict,
hass: HomeAssistant,
config_entry,
entityName: str,
nightVisionOptions: list,
currentValueKey: str,
method,
):
self._attr_options = []
self.method = method
self.currentValueKey = currentValueKey
for nightVisionCapability in nightVisionOptions:
self._attr_options.append(getNightModeName(nightVisionCapability))

self._attr_current_option = None
TapoSelectEntity.__init__(
self,
"Night Vision",
entityName,
entry,
hass,
config_entry,
Expand All @@ -336,12 +375,13 @@ def updateTapo(self, camData):
if not camData:
self._attr_state = "unavailable"
else:
self._attr_current_option = camData["day_night_mode"]
self._attr_current_option = getNightModeName(camData[self.currentValueKey])
self._attr_state = self._attr_current_option

async def async_select_option(self, option: str) -> None:
LOGGER.debug("Calling " + self.method.__name__ + " with " + option + "...")
result = await self._hass.async_add_executor_job(
self._controller.setDayNightMode, option
self.method, getNightModeValue(option)
)
if "error_code" not in result or result["error_code"] == 0:
self._attr_state = option
Expand Down
76 changes: 50 additions & 26 deletions custom_components/tapo_control/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,31 @@ def getDataForController(hass, entry, controller):
return childDevice


def getNightModeMap():
return {
"inf_night_vision": "Infrared Mode",
"wtl_night_vision": "Full Color Mode",
"md_night_vision": "Smart Mode",
"dbl_night_vision": "Doorbell Mode",
"shed_night_vision": "Scheduled mode",
}


def getNightModeName(value: str):
nightModeMap = getNightModeMap()
if value in nightModeMap:
return nightModeMap[value]
return value


def getNightModeValue(value: str):
night_mode_map = getNightModeMap()
for key, val in night_mode_map.items():
if val == value:
return key
return value


async def getCamData(hass, controller):
LOGGER.debug("getCamData")
data = await hass.async_add_executor_job(controller.getMost)
Expand Down Expand Up @@ -926,36 +951,35 @@ async def getCamData(hass, controller):
camData["light_frequency_mode"] = light_frequency_mode

try:
day_night_mode = data["getLdc"][0]["image"]["common"]["inf_type"]
night_vision_mode = data["getNightVisionModeConfig"][0]["image"]["switch"][
"night_vision_mode"
]
except Exception:
day_night_mode = None
night_vision_mode = None
camData["night_vision_mode"] = night_vision_mode

if day_night_mode is None:
try:
night_vision_capability = data["getNightVisionCapability"][0][
"image_capability"
]["supplement_lamp"]["night_vision_mode_range"]
except Exception:
night_vision_capability = None
camData["night_vision_capability"] = night_vision_capability

try:
night_vision_mode_switching = data["getLdc"][0]["image"]["common"]["inf_type"]
except Exception:
night_vision_mode_switching = None
camData["night_vision_mode_switching"] = night_vision_mode_switching

if night_vision_mode_switching is None:
try:
if (
data["getNightVisionModeConfig"][0]["image"]["switch"][
"night_vision_mode"
]
== "inf_night_vision"
):
day_night_mode = "on"
elif (
data["getNightVisionModeConfig"][0]["image"]["switch"][
"night_vision_mode"
]
== "wtl_night_vision"
):
day_night_mode = "off"
elif (
data["getNightVisionModeConfig"][0]["image"]["switch"][
"night_vision_mode"
]
== "md_night_vision"
):
day_night_mode = "auto"
night_vision_mode_switching = data["getLightFrequencyInfo"][0]["image"][
"common"
]["inf_type"]
except Exception:
day_night_mode = None
camData["day_night_mode"] = day_night_mode
night_vision_mode_switching = None
camData["night_vision_mode_switching"] = night_vision_mode_switching

try:
force_white_lamp_state = data["getLdc"][0]["image"]["switch"]["force_wtl_state"]
Expand Down

0 comments on commit 8814ec7

Please sign in to comment.