6565 SERV_TELEVISION ,
6666 SERV_TELEVISION_SPEAKER ,
6767)
68+ from .util import get_media_player_features
6869
6970_LOGGER = logging .getLogger (__name__ )
7071
8485 # 15: "Information",
8586}
8687
88+ # Names may not contain special characters
89+ # or emjoi (/ is a special character for Apple)
8790MODE_FRIENDLY_NAME = {
8891 FEATURE_ON_OFF : "Power" ,
89- FEATURE_PLAY_PAUSE : "Play/ Pause" ,
90- FEATURE_PLAY_STOP : "Play/ Stop" ,
92+ FEATURE_PLAY_PAUSE : "Play- Pause" ,
93+ FEATURE_PLAY_STOP : "Play- Stop" ,
9194 FEATURE_TOGGLE_MUTE : "Mute" ,
9295}
9396
@@ -106,7 +109,9 @@ def __init__(self, *args):
106109 FEATURE_PLAY_STOP : None ,
107110 FEATURE_TOGGLE_MUTE : None ,
108111 }
109- feature_list = self .config [CONF_FEATURE_LIST ]
112+ feature_list = self .config .get (
113+ CONF_FEATURE_LIST , get_media_player_features (state )
114+ )
110115
111116 if FEATURE_ON_OFF in feature_list :
112117 name = self .generate_service_name (FEATURE_ON_OFF )
@@ -214,7 +219,7 @@ def update_state(self, new_state):
214219 self .chars [FEATURE_PLAY_STOP ].set_value (hk_state )
215220
216221 if self .chars [FEATURE_TOGGLE_MUTE ]:
217- current_state = new_state .attributes .get (ATTR_MEDIA_VOLUME_MUTED )
222+ current_state = bool ( new_state .attributes .get (ATTR_MEDIA_VOLUME_MUTED ) )
218223 _LOGGER .debug (
219224 '%s: Set current state for "toggle_mute" to %s' ,
220225 self .entity_id ,
@@ -240,9 +245,7 @@ def __init__(self, *args):
240245 # Add additional characteristics if volume or input selection supported
241246 self .chars_tv = []
242247 self .chars_speaker = []
243- features = self .hass .states .get (self .entity_id ).attributes .get (
244- ATTR_SUPPORTED_FEATURES , 0
245- )
248+ features = state .attributes .get (ATTR_SUPPORTED_FEATURES , 0 )
246249
247250 if features & (SUPPORT_PLAY | SUPPORT_PAUSE ):
248251 self .chars_tv .append (CHAR_REMOTE_KEY )
@@ -253,7 +256,8 @@ def __init__(self, *args):
253256 if features & SUPPORT_VOLUME_SET :
254257 self .chars_speaker .append (CHAR_VOLUME )
255258
256- if features & SUPPORT_SELECT_SOURCE :
259+ source_list = state .attributes .get (ATTR_INPUT_SOURCE_LIST , [])
260+ if source_list and features & SUPPORT_SELECT_SOURCE :
257261 self .support_select_source = True
258262
259263 serv_tv = self .add_preload_service (SERV_TELEVISION , self .chars_tv )
@@ -298,9 +302,7 @@ def __init__(self, *args):
298302 )
299303
300304 if self .support_select_source :
301- self .sources = self .hass .states .get (self .entity_id ).attributes .get (
302- ATTR_INPUT_SOURCE_LIST , []
303- )
305+ self .sources = source_list
304306 self .char_input_source = serv_tv .configure_char (
305307 CHAR_ACTIVE_IDENTIFIER , setter_callback = self .set_input_source
306308 )
@@ -380,35 +382,30 @@ def update_state(self, new_state):
380382 hk_state = 0
381383 if current_state not in ("None" , STATE_OFF , STATE_UNKNOWN ):
382384 hk_state = 1
383-
384385 _LOGGER .debug ("%s: Set current active state to %s" , self .entity_id , hk_state )
385386 if self .char_active .value != hk_state :
386387 self .char_active .set_value (hk_state )
387388
388389 # Set mute state
389390 if CHAR_VOLUME_SELECTOR in self .chars_speaker :
390- current_mute_state = new_state .attributes .get (ATTR_MEDIA_VOLUME_MUTED )
391+ current_mute_state = bool ( new_state .attributes .get (ATTR_MEDIA_VOLUME_MUTED ) )
391392 _LOGGER .debug (
392393 "%s: Set current mute state to %s" , self .entity_id , current_mute_state ,
393394 )
394395 if self .char_mute .value != current_mute_state :
395396 self .char_mute .set_value (current_mute_state )
396397
397398 # Set active input
398- if self .support_select_source :
399+ if self .support_select_source and self . sources :
399400 source_name = new_state .attributes .get (ATTR_INPUT_SOURCE )
400- if self .sources :
401- _LOGGER .debug (
402- "%s: Set current input to %s" , self .entity_id , source_name
401+ _LOGGER .debug ("%s: Set current input to %s" , self .entity_id , source_name )
402+ if source_name in self .sources :
403+ index = self .sources .index (source_name )
404+ if self .char_input_source .value != index :
405+ self .char_input_source .set_value (index )
406+ else :
407+ _LOGGER .warning (
408+ "%s: Sources out of sync. Restart Home Assistant" , self .entity_id ,
403409 )
404- if source_name in self .sources :
405- index = self .sources .index (source_name )
406- if self .char_input_source .value != index :
407- self .char_input_source .set_value (index )
408- else :
409- _LOGGER .warning (
410- "%s: Sources out of sync. Restart Home Assistant" ,
411- self .entity_id ,
412- )
413- if self .char_input_source .value != 0 :
414- self .char_input_source .set_value (0 )
410+ if self .char_input_source .value != 0 :
411+ self .char_input_source .set_value (0 )
0 commit comments