Skip to content

Commit

Permalink
feat: add last_called notify service target
Browse files Browse the repository at this point in the history
This requires HA to allow dynamic targets.
closes #1104
  • Loading branch information
alandtse committed Jan 24, 2021
1 parent 77fc818 commit d3e35c2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions custom_components/alexa_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
CONF_PASSWORD,
CONF_SCAN_INTERVAL,
CONF_URL,
EVENT_HOMEASSISTANT_STARTED,
EVENT_HOMEASSISTANT_STOP,
)
from homeassistant.data_entry_flow import UnknownFlow
Expand Down Expand Up @@ -182,6 +183,15 @@ async def close_alexa_media(event=None) -> None:
for email, _ in hass.data[DATA_ALEXAMEDIA]["accounts"].items():
await close_connections(hass, email)

async def complete_startup(event=None) -> None:
"""Run final tasks after startup."""
_LOGGER.debug("Completing remaining startup tasks.")
await asyncio.sleep(10)
if hass.data[DATA_ALEXAMEDIA].get("notify_service"):
notify = hass.data[DATA_ALEXAMEDIA].get("notify_service")
_LOGGER.debug("Refreshing notify targets")
await notify.async_register_services()

async def relogin(event=None) -> None:
"""Relogin to Alexa."""
if hide_email(email) == event.data.get("email"):
Expand Down Expand Up @@ -290,6 +300,7 @@ async def login_success(event=None) -> None:
hass.data[DATA_ALEXAMEDIA]["accounts"][email]["login_obj"] = login
if not hass.data[DATA_ALEXAMEDIA]["accounts"][email]["second_account_index"]:
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, close_alexa_media)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, complete_startup)
hass.bus.async_listen("alexa_media_relogin_required", relogin)
hass.bus.async_listen("alexa_media_relogin_success", login_success)
await login.login(cookies=await login.load_cookie())
Expand Down
14 changes: 14 additions & 0 deletions custom_components/alexa_media/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,20 @@ async def _update_notify_targets(self) -> None:
"%s: Refreshing notify targets", hide_email(self._login.email),
)
await notify.async_register_services()
entity_name_last_called = f"{ALEXA_DOMAIN}_last_called{'_'+ self._login.email if self.unique_id[-1:].isdigit() else ''}"
if (
notify.last_called
and notify.registered_targets.get(entity_name_last_called)
!= self.unique_id
):
_LOGGER.debug(
"%s: Changing notify.targets is not supported by HA version < 2021.2.0; using toggle method",
hide_email(self._login.email),
)
notify.last_called = False
await notify.async_register_services()
notify.last_called = True
await notify.async_register_services()
else:
_LOGGER.debug(
"%s: Unable to refresh notify targets; notify not ready",
Expand Down
15 changes: 15 additions & 0 deletions custom_components/alexa_media/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class AlexaNotificationService(BaseNotificationService):
def __init__(self, hass):
"""Initialize the service."""
self.hass = hass
self.last_called = True

def convert(self, names, type_="entities", filter_matches=False):
"""Return a list of converted Alexa devices based on names.
Expand Down Expand Up @@ -149,6 +150,20 @@ def targets(self):
for _, entity in account_dict["entities"]["media_player"].items():
entity_name = (entity.entity_id).split(".")[1]
devices[entity_name] = entity.unique_id
if self.last_called and entity.device_state_attributes.get(
"last_called"
):
entity_name_last_called = (
f"last_called{'_'+ email if entity_name[-1:].isdigit() else ''}"
)
_LOGGER.debug(
"%s: Creating last_called target %s using %s",
hide_email(email),
entity_name_last_called,
entity,
)
devices[entity_name_last_called] = entity.unique_id
break
return devices

@property
Expand Down

0 comments on commit d3e35c2

Please sign in to comment.