Skip to content

Commit

Permalink
fix: getting data
Browse files Browse the repository at this point in the history
  • Loading branch information
martonborzak committed Jul 24, 2023
1 parent c659999 commit 7e7fd8f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 47 deletions.
2 changes: 1 addition & 1 deletion driver.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"driver_id": "uc_appletv_driver",
"version": "0.10.4",
"version": "0.10.5",
"min_core_api": "0.7.0",
"name": { "en": "Apple TV" },
"icon": "custom:appletv.png",
Expand Down
88 changes: 42 additions & 46 deletions tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def __init__(self, loop):
self._pairingProcess = None
self._polling = None
self._pollInterval = 2
self._prevUpdateHash = None
self._state = None
self._appList = {}

Expand Down Expand Up @@ -283,38 +282,42 @@ async def _stopPolling(self):
async def _getUpdate(self):
LOG.debug('Manually getting update')
update = {}
data = None

if self._state == pyatv.const.DeviceState.Playing:
try:
data = await self._atv.metadata.playing()
except:
LOG.warning('Could not get metadata yet')
return

try:
artwork = await self._atv.metadata.artwork(width=ARTWORK_WIDTH, height=ARTWORK_HEIGHT)
artwork_encoded = 'data:image/png;base64,' + base64.b64encode(artwork.bytes).decode('utf-8')
update['artwork'] = artwork_encoded
except:
LOG.warning('Error while updating the artwork')

if data:
update['total_time'] = data.total_time
update['title'] = data.title

if data.artist is not None:
update['artist'] = data.artist
else:
update['artist'] = ""

if data.album is not None:
update['album'] = data.album
else:
update['album'] = ""

try:
data = await self._atv.metadata.playing()
except:
LOG.warning('Could not get metadata yet')
return

try:
artwork = await self._atv.metadata.artwork(width=ARTWORK_WIDTH, height=ARTWORK_HEIGHT)
artwork_encoded = 'data:image/png;base64,' + base64.b64encode(artwork.bytes).decode('utf-8')
update['artwork'] = artwork_encoded
except:
LOG.warning('Error while updating the artwork')

update['total_time'] = data.total_time
update['title'] = data.title

if data.artist is not None:
update['artist'] = data.artist
else:
update['artist'] = ""

if data.album is not None:
update['album'] = data.album
else:
update['album'] = ""

if data.media_type is not None:
update['media_type'] = data.media_type
if data.media_type is not None:
update['media_type'] = data.media_type

if data:
self.events.emit(EVENTS.UPDATE, update)
if update:
LOG.debug('Manual update done')
self.events.emit(EVENTS.UPDATE, update)


async def _processUpdate(self, data):
Expand All @@ -325,6 +328,7 @@ async def _processUpdate(self, data):
# We only update device state (playing, paused, etc) if the power state is On
# otherwise we'll set the state to Off in the polling method
self._state = data.device_state
update['state'] = data.device_state

if self._atv.power.power_state is pyatv.const.PowerState.On:
update['state'] = data.device_state
Expand All @@ -336,14 +340,13 @@ async def _processUpdate(self, data):
update['position'] = data.position

# image operations are expensive, so we only do it when the hash changed
if data.hash != self._prevUpdateHash:
if self._state == pyatv.const.DeviceState.Playing:
try:
artwork = await self._atv.metadata.artwork(width=ARTWORK_WIDTH, height=ARTWORK_HEIGHT)
artwork_encoded = 'data:image/png;base64,' + base64.b64encode(artwork.bytes).decode('utf-8')
update['artwork'] = artwork_encoded
except:
LOG.warning('Error while updating the artwork')
if self._state == pyatv.const.DeviceState.Playing:
try:
artwork = await self._atv.metadata.artwork(width=ARTWORK_WIDTH, height=ARTWORK_HEIGHT)
artwork_encoded = 'data:image/png;base64,' + base64.b64encode(artwork.bytes).decode('utf-8')
update['artwork'] = artwork_encoded
except:
LOG.warning('Error while updating the artwork')

update['total_time'] = data.total_time
update['title'] = data.title
Expand All @@ -365,7 +368,6 @@ async def _processUpdate(self, data):
# TODO: data.repeat: All, Off, Track
# TODO: data.shuffle

self._prevUpdateHash = data.hash
self.events.emit(EVENTS.UPDATE, update)

async def _updateAppList(self):
Expand All @@ -391,12 +393,6 @@ async def _pollWorker(self):

if self._atv.power.power_state is pyatv.const.PowerState.Off:
update['state'] = self._atv.power.power_state
else:
try:
data = await self._atv.metadata.playing()
update['state'] = data.device_state
except:
LOG.debug('Error while getting playing metadata')

if self._isFeatureAvailable(pyatv.const.FeatureName.App):
update['source'] = self._atv.metadata.app.name
Expand Down

0 comments on commit 7e7fd8f

Please sign in to comment.