Skip to content

Commit

Permalink
Fix refresh device token
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Feb 7, 2020
1 parent f398b48 commit e456209
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 0.1.5 - 2020-02-07

### Changed

- Теперь выводится ошибка, при неподдерживаемом типе `play_media` (например использование сторонних TTS с Яндекс.Станцией)

### Fixed

- Обновление токена колонки (пропадало управление колонкой через сутки после старта HA)

## 0.1.5 - 2020-02-04

### Added
Expand Down
20 changes: 15 additions & 5 deletions custom_components/yandex_station/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import requests
import websockets
from websockets import ConnectionClosed

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -53,9 +54,9 @@ def get_devices(yandex_token: str) -> dict:
return r.json()['devices']


@lru_cache()
def get_device_token(yandex_token: str, device_id: str,
device_platform: str) -> str:
_LOGGER.debug(f"Refresh device token {device_id}")
r = requests.get('https://quasar.yandex.net/glagol/token', params={
'device_id': device_id,
'platform': device_platform
Expand All @@ -65,8 +66,11 @@ def get_device_token(yandex_token: str, device_id: str,


async def send_to_station(device: dict, message: dict = None):
device_token = get_device_token(device['yandex_token'], device['id'],
device['platform'])
if 'device_token' not in device:
device['device_token'] = get_device_token(
device['yandex_token'], device['id'], device['platform'])

device_token = device['device_token']

uri = f"wss://{device['host']}:{device['port']}"
try:
Expand All @@ -79,10 +83,16 @@ async def send_to_station(device: dict, message: dict = None):
_LOGGER.debug(res)
return res

except ConnectionClosed as e:
if e.code == 4000:
device.pop('device_token')
return await send_to_station(device, message)

except Exception as e:
_LOGGER.error(f"Station connect error: {e}")
pass

return None
_LOGGER.error(f"Station connect error: {e}")
return None


UA = "Mozilla/5.0 (Windows NT 10.0; rv:40.0) Gecko/20100101 Firefox/40.0"
Expand Down

0 comments on commit e456209

Please sign in to comment.