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

Add TapoQuickResponseSelect to handle quick response options #814

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
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
54 changes: 54 additions & 0 deletions custom_components/tapo_control/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ async def setupEntities(entry):
LOGGER.debug("Adding TapoWhitelampIntensityLevelSelect...")
selects.append(tapoWhitelampIntensityLevelSelect)

if(
"quick_response" in entry["camData"]
and entry["camData"]["quick_response"] is not None
and len(entry["camData"]["quick_response"]) > 0
):
tapoQuickResponseSelect = TapoQuickResponseSelect(
entry,
hass,
config_entry
)
if tapoQuickResponseSelect:
LOGGER.debug("Adding tapoQuickResponseSelect...")
selects.append(tapoQuickResponseSelect)

cavefire marked this conversation as resolved.
Show resolved Hide resolved
return selects

selects = await setupEntities(entry)
Expand Down Expand Up @@ -302,6 +316,46 @@ async def async_select_option(self, option: str) -> None:
self.async_write_ha_state()
await self._coordinator.async_request_refresh()

class TapoQuickResponseSelect(TapoSelectEntity):
def __init__(self, entry: dict, hass: HomeAssistant, config_entry):
self.populateSelectOptions(entry["camData"])

self._attr_current_option = None
TapoSelectEntity.__init__(
self,
"Quick Response",
entry,
hass,
config_entry,
"mdi:comment-alert",
)

def populateSelectOptions(self, camData):
self._attr_options = []
self._attr_options_id = []
for quick_resp_audio in camData["quick_response"]:
for key in quick_resp_audio:
self._attr_options.append(quick_resp_audio[key]["name"])
self._attr_options_id.append(quick_resp_audio[key]["id"])

async def async_update(self) -> None:
await self._coordinator.async_request_refresh()

def updateTapo(self, camData):
if not camData:
self._attr_state = "unavailable"
else:
self.populateSelectOptions(camData)
self._attr_current_option = None
self._attr_state = self._attr_current_option

async def async_select_option(self, option: str) -> None:
result = await self._hass.async_add_executor_job(
self._controller.playQuickResponse, self._attr_options_id[self._attr_options.index(option)]
)
self._attr_state = None
self.async_write_ha_state()
await self._coordinator.async_request_refresh()

class TapoPatrolModeSelect(TapoSelectEntity):
def __init__(self, entry: dict, hass: HomeAssistant, config_entry):
Expand Down
5 changes: 5 additions & 0 deletions custom_components/tapo_control/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,11 @@ async def getCamData(hass, controller):

camData["updated"] = datetime.datetime.utcnow().timestamp()

try:
camData['quick_response'] = data['getQuickRespList']['quick_resp_audio']
except Exception:
camData['quick_response'] = None

LOGGER.debug("getCamData - done")
LOGGER.debug("Processed update data:")
LOGGER.debug(camData)
Expand Down