Skip to content

Commit

Permalink
Merge pull request #821 from alandtse/event_cookie_expired
Browse files Browse the repository at this point in the history
Event cookie expired
  • Loading branch information
alandtse authored Jul 3, 2020
2 parents e0577ef + 3fde629 commit 238d04e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 8 deletions.
8 changes: 7 additions & 1 deletion custom_components/alexa_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ async def async_update_data():
try:
# Note: asyncio.TimeoutError and aiohttp.ClientError are already
# handled by the data update coordinator.
async with async_timeout.timeout(10):
async with async_timeout.timeout(30):
if new_devices:
(
devices,
Expand Down Expand Up @@ -310,6 +310,10 @@ async def async_update_data():
login_obj.status,
)
if login_obj.status:
await hass.bus.async_fire(
"alexa_media_player/relogin_required",
event_data={"email": hide_email(email), "url": login_obj.url},
)
await login_obj.reset()
await login_obj.login()
await test_login_status(hass, config_entry, login_obj, setup_alexa)
Expand Down Expand Up @@ -894,6 +898,8 @@ async def ws_error_handler(message):
_LOGGER.debug("Refreshing coordinator")
await coordinator.async_refresh()

coordinator.async_add_listener(lambda: None)

hass.services.async_register(
DOMAIN,
SERVICE_UPDATE_LAST_CALLED,
Expand Down
18 changes: 18 additions & 0 deletions custom_components/alexa_media/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ def __init__(self, login, media_players=None) -> None:
self.alexa_api = AlexaAPI(self, login)
self.email = login.email
self.account = hide_email(login.email)
self._available = None
self._assumed_state = None

# Guard info
self._appliance_id = None
Expand Down Expand Up @@ -242,6 +244,8 @@ async def async_update(self):
import json

if self._login.session.closed:
self._available = False
self._assumed_state = True
return
_LOGGER.debug("%s: Refreshing %s", self.account, self.name)
state = None
Expand All @@ -266,13 +270,17 @@ async def async_update(self):
json.dumps(state_json["errors"]) if state_json else None,
)
if state is None:
self._available = False
self._assumed_state = True
return
if state == "ARMED_AWAY":
self._state = STATE_ALARM_ARMED_AWAY
elif state == "ARMED_STAY":
self._state = STATE_ALARM_DISARMED
else:
self._state = STATE_ALARM_DISARMED
self._available = True
self._assumed_state = False
_LOGGER.debug("%s: Alarm State: %s", self.account, self.state)
self.async_schedule_update_ha_state()

Expand Down Expand Up @@ -357,3 +365,13 @@ def supported_features(self) -> int:
except ImportError:
return 0
return SUPPORT_ALARM_ARM_AWAY

@property
def available(self):
"""Return the availability of the device."""
return self._available

@property
def assumed_state(self):
"""Return whether the state is an assumed_state."""
return self._assumed_state
2 changes: 1 addition & 1 deletion custom_components/alexa_media/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""
from datetime import timedelta

__version__ = "2.8.8"
__version__ = "2.8.9"
PROJECT_URL = "https://github.com/custom-components/alexa_media_player/"
ISSUE_URL = "{}issues".format(PROJECT_URL)

Expand Down
23 changes: 19 additions & 4 deletions custom_components/alexa_media/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ def __init__(self, device, login):
self._device_owner_customer_id = None
self._software_version = None
self._available = None
self._assumed_state = False
self._capabilities = []
self._cluster_members = []
self._locale = None
Expand Down Expand Up @@ -331,7 +332,7 @@ async def _refresh_if_no_audiopush(already_refreshed=False):
)
if not event_serial:
return
self.available = True
self._available = True
self.async_schedule_update_ha_state()
if "last_called_change" in event:
if event_serial == self.device_serial_number or any(
Expand Down Expand Up @@ -499,6 +500,7 @@ async def refresh(self, device=None, skip_api: bool = False):
session = None
if self.available:
_LOGGER.debug("%s: Refreshing %s", self.account, self.name)
self._assumed_state = False
if "PAIR_BT_SOURCE" in self._capabilities:
self._source = self._get_source()
self._source_list = self._get_source_list()
Expand Down Expand Up @@ -582,7 +584,6 @@ async def refresh(self, device=None, skip_api: bool = False):
)
if self._session.get("state"):
self._media_player_state = self._session["state"]
self._media_pos = self._session.get("progress", {}).get("mediaProgress")
self._media_title = self._session.get("infoText", {}).get("title")
self._media_artist = self._session.get("infoText", {}).get("subText1")
self._media_album_name = self._session.get("infoText", {}).get(
Expand All @@ -593,8 +594,15 @@ async def refresh(self, device=None, skip_api: bool = False):
if self._session.get("mainArt")
else None
)
self._media_duration = self._session.get("progress", {}).get(
"mediaLength"
self._media_pos = (
self._session.get("progress", {}).get("mediaProgress")
if self._session.get("progress")
else None
)
self._media_duration = (
self._session.get("progress", {}).get("mediaLength")
if self._session.get("progress")
else None
)
if not self._session.get("lemurVolume"):
self._media_is_muted = (
Expand Down Expand Up @@ -742,6 +750,11 @@ def available(self, state):
"""Set the availability state."""
self._available = state

@property
def assumed_state(self):
"""Return whether the state is an assumed_state."""
return self._assumed_state

@property
def hidden(self):
"""Return whether the sensor should be hidden."""
Expand Down Expand Up @@ -801,6 +814,8 @@ async def async_update(self):
or email not in self.hass.data[DATA_ALEXAMEDIA]["accounts"]
or self._login.session.closed
):
self._assumed_state = True
self.available = False
return
device = self.hass.data[DATA_ALEXAMEDIA]["accounts"][email]["devices"][
"media_player"
Expand Down
7 changes: 6 additions & 1 deletion custom_components/alexa_media/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,12 @@ def _handle_event(self, event):
@property
def available(self):
"""Return the availabilty of the sensor."""
return True
return self._client.available

@property
def assumed_state(self):
"""Return whether the state is an assumed_state."""
return self._client.assumed_state

@property
def hidden(self):
Expand Down
10 changes: 9 additions & 1 deletion custom_components/alexa_media/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,15 @@ async def async_turn_off(self, **kwargs):
@property
def available(self):
"""Return the availabilty of the switch."""
return getattr(self._client, self._switch_property) is not None
return (
self._client.available
and getattr(self._client, self._switch_property) is not None
)

@property
def assumed_state(self):
"""Return whether the state is an assumed_state."""
return self._client.assumed_state

@property
def unique_id(self):
Expand Down

0 comments on commit 238d04e

Please sign in to comment.