Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Charge times improvement - Not supported cached functions. #632

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 48 additions & 45 deletions custom_components/tapo_control/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1397,55 +1397,58 @@ def pytapoFunctionMap(pytapoFunctionName):
def isCacheSupported(check_function, rawData):
rawFunctions = pytapoFunctionMap(check_function)
for function in rawFunctions:
if function in rawData and rawData[function][0]:
if check_function == "getForceWhitelampState":
return (
"image" in rawData["getLdc"][0]
and "switch" in rawData["getLdc"][0]["image"]
and "force_wtl_state" in rawData["getLdc"][0]["image"]["switch"]
)
elif check_function == "getDayNightMode":
return (
"image" in rawData["getLightFrequencyInfo"][0]
and "common" in rawData["getLightFrequencyInfo"][0]["image"]
and "inf_type"
in rawData["getLightFrequencyInfo"][0]["image"]["common"]
)
elif check_function == "getImageFlipVertical":
return (
"image" in rawData["getLdc"][0]
and "switch" in rawData["getLdc"][0]["image"]
and "flip_type" in rawData["getLdc"][0]["image"]["switch"]
) or (
"image" in rawData["getRotationStatus"][0]
and "switch" in rawData["getRotationStatus"][0]["image"]
and "flip_type"
in rawData["getRotationStatus"][0]["image"]["switch"]
)
elif check_function == "getLensDistortionCorrection":
return (
"image" in rawData["getLdc"][0]
and "switch" in rawData["getLdc"][0]["image"]
and "ldc" in rawData["getLdc"][0]["image"]["switch"]
)
return True
if function in rawData:
if rawData[function][0]:
if check_function == "getForceWhitelampState":
return (
"image" in rawData["getLdc"][0]
and "switch" in rawData["getLdc"][0]["image"]
and "force_wtl_state" in rawData["getLdc"][0]["image"]["switch"]
)
elif check_function == "getDayNightMode":
return (
"image" in rawData["getLightFrequencyInfo"][0]
and "common" in rawData["getLightFrequencyInfo"][0]["image"]
and "inf_type"
in rawData["getLightFrequencyInfo"][0]["image"]["common"]
)
elif check_function == "getImageFlipVertical":
return (
"image" in rawData["getLdc"][0]
and "switch" in rawData["getLdc"][0]["image"]
and "flip_type" in rawData["getLdc"][0]["image"]["switch"]
) or (
"image" in rawData["getRotationStatus"][0]
and "switch" in rawData["getRotationStatus"][0]["image"]
and "flip_type"
in rawData["getRotationStatus"][0]["image"]["switch"]
)
elif check_function == "getLensDistortionCorrection":
return (
"image" in rawData["getLdc"][0]
and "switch" in rawData["getLdc"][0]["image"]
and "ldc" in rawData["getLdc"][0]["image"]["switch"]
)
return True
else:
raise Exception(f"Capability {check_function} (mapped to:{function}) cached but not supported.")
return False


async def check_and_create(entry, hass, cls, check_function, config_entry):
if isCacheSupported(check_function, entry["camData"]["raw"]):
LOGGER.debug(
f"Found cached capability {check_function}, creating {cls.__name__}"
)
return cls(entry, hass, config_entry)
else:
LOGGER.debug(f"Capability {check_function} not found, querying again...")
try:
try:
if isCacheSupported(check_function, entry["camData"]["raw"]):
LOGGER.debug(
f"Found cached capability {check_function}, creating {cls.__name__}"
)
return cls(entry, hass, config_entry)
else:
LOGGER.debug(f"Capability {check_function} not found, querying again...")
await hass.async_add_executor_job(
getattr(entry["controller"], check_function)
)
except Exception:
LOGGER.info(f"Camera does not support {cls.__name__}")
return None
LOGGER.debug(f"Creating {cls.__name__}")
return cls(entry, hass, config_entry)
LOGGER.debug(f"Creating {cls.__name__}")
return cls(entry, hass, config_entry)
except Exception as err:
LOGGER.info(f"Camera does not support {cls.__name__}: {err}")
return None