From 593db3d87eb6348b13349e91938f8109471ae77c Mon Sep 17 00:00:00 2001
From: Marton <hello@martonborzak.com>
Date: Sat, 22 Jul 2023 19:02:33 +0200
Subject: [PATCH] refactor: app list

---
 driver.json |  2 +-
 driver.py   | 17 ++++++++---------
 tv.py       |  8 +++++---
 3 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/driver.json b/driver.json
index a64921b..da00c06 100644
--- a/driver.json
+++ b/driver.json
@@ -1,6 +1,6 @@
 {
 	"driver_id": "uc_appletv_driver",
-	"version": "0.10.1",
+	"version": "0.10.2",
 	"min_core_api": "0.7.0",
 	"name": { "en": "Apple TV" },
 	"icon": "custom:appletv.png",
diff --git a/driver.py b/driver.py
index c0e78ea..1ad223a 100644
--- a/driver.py
+++ b/driver.py
@@ -105,7 +105,7 @@ async def event_handler(websocket, id, data):
 
     if not dropdownItems:
         LOG.warning('No Apple TVs found')
-        await api.driverSetupError(websocket, 'No Apple TVs found')
+        await api.driverSetupError(websocket)
         return
 
     await api.requestDriverSetupUserInput(websocket, 'Please choose your Apple TV', [
@@ -137,7 +137,7 @@ async def event_handler(websocket, id, data):
 
         res = await pairingAppleTv.finishPairing()
         if res is None:
-            await api.driverSetupError(websocket, 'Unable to pair with Apple TV')
+            await api.driverSetupError(websocket)
         else:
             c = {
                 'protocol': res.protocol.name.lower(),
@@ -165,7 +165,7 @@ async def event_handler(websocket, id, data):
         
         res = await pairingAppleTv.finishPairing()
         if res is None:
-            await api.driverSetupError(websocket, 'Unable to pair with Apple TV')
+            await api.driverSetupError(websocket)
         else:
             # Store credentials
             c = {
@@ -204,7 +204,7 @@ async def event_handler(websocket, id, data):
         
         if pairingAppleTv.pairingAtv is None:
             LOG.error('Cannot find the chosen AppleTV')
-            await api.driverSetupError(websocket, 'There was an error during the setup process')
+            await api.driverSetupError(websocket)
             return
         
         await pairingAppleTv.init(choice, name = pairingAppleTv.pairingAtv.name)
@@ -232,7 +232,7 @@ async def event_handler(websocket, id, data):
 
     else:
         LOG.error('No choice was received')
-        await api.driverSetupError(websocket, 'No Apple TV was selected')
+        await api.driverSetupError(websocket)
 
 @api.events.on(uc.uc.EVENTS.CONNECT)
 async def event_handler():
@@ -270,10 +270,9 @@ async def event_handler(entityIds):
         if entityId in configuredAppleTvs:
             LOG.debug('We have a match, start listening to events')
 
-            if api.state != uc.uc.DEVICE_STATES.CONNECTED:
-                api.configuredEntities.updateEntityAttributes(entityId, {
-                    entities.media_player.ATTRIBUTES.STATE: entities.media_player.STATES.UNAVAILABLE
-                })
+            api.configuredEntities.updateEntityAttributes(entityId, {
+                entities.media_player.ATTRIBUTES.STATE: entities.media_player.STATES.UNAVAILABLE
+            })
 
             appleTv = configuredAppleTvs[entityId]
 
diff --git a/tv.py b/tv.py
index 18f6af8..12a91d7 100644
--- a/tv.py
+++ b/tv.py
@@ -359,14 +359,16 @@ async def _pollWorker(self):
                 except:
                     LOG.debug('Error while getting playing metadata')
 
-                if self._isFeatureAvailable(pyatv.const.FeatureName.AppList):
+                if self._atv.features.in_state(pyatv.const.FeatureState.Available, pyatv.const.FeatureName.AppList):
                     try:
                         appList = await self._atv.apps.app_list()
                         for app in appList:
                             self._appList[app.name] = app.identifier
                             update['sourceList'].append(app.name)
-                    except:
-                        LOG.warning('Could not get app list')
+                    except pyatv.exceptions.NotSupportedError:
+                        LOG.warning('App list is not supported')
+                    except pyatv.exceptions.ProtocolError:
+                        LOG.warning('App list: protocol error')
 
                 if self._isFeatureAvailable(pyatv.const.FeatureName.App):
                     update['source'] = self._atv.metadata.app.name