1- import asyncio
21import logging
32import voluptuous as vol
43import json
54import urllib .request
65
7- from homeassistant .components import media_source
8- from homeassistant .helpers .aiohttp_client import async_get_clientsession
9-
10- _LOGGER = logging .getLogger (__name__ )
11-
12- VERSION = '0.9.260'
13-
14- DOMAIN = "yoradio"
15-
6+ from homeassistant .components import mqtt , media_source
7+ from homeassistant .components .media_player .browse_media import async_process_play_media_url
8+ from homeassistant .const import CONF_NAME
169from homeassistant .helpers import config_validation as cv
1710
18- from homeassistant .components .media_player .browse_media import (
19- async_process_play_media_url ,
20- )
2111from homeassistant .components .media_player import (
22- MediaPlayerEntity ,
23- MediaPlayerEnqueue ,
24- BrowseMedia ,
25- PLATFORM_SCHEMA
12+ PLATFORM_SCHEMA as MEDIA_PLAYER_PLATFORM_SCHEMA ,
13+ BrowseMedia ,
14+ MediaPlayerEntity ,
15+ MediaPlayerEntityFeature ,
16+ MediaPlayerState ,
17+ MediaPlayerEnqueue ,
18+ MediaType ,
19+ RepeatMode ,
2620)
2721
28- from homeassistant .components .media_player .const import (
29- MEDIA_TYPE_MUSIC ,
30- MEDIA_TYPE_URL ,
31- SUPPORT_TURN_ON , SUPPORT_TURN_OFF ,
32- SUPPORT_VOLUME_STEP , SUPPORT_VOLUME_SET ,
33- SUPPORT_PAUSE , SUPPORT_PLAY , SUPPORT_STOP ,
34- SUPPORT_PREVIOUS_TRACK , SUPPORT_NEXT_TRACK ,
35- SUPPORT_SELECT_SOURCE , SUPPORT_BROWSE_MEDIA , SUPPORT_PLAY_MEDIA
36- )
22+ VERSION = '0.9.407'
3723
38- from homeassistant .const import (
39- CONF_NAME ,
40- STATE_IDLE ,
41- STATE_PLAYING ,
42- STATE_OFF ,
43- STATE_ON ,
44- )
24+ _LOGGER = logging .getLogger (__name__ )
4525
46- SUPPORT_YORADIO = SUPPORT_PAUSE | SUPPORT_PLAY | SUPPORT_STOP | \
47- SUPPORT_VOLUME_SET | SUPPORT_VOLUME_STEP | \
48- SUPPORT_TURN_ON | SUPPORT_TURN_OFF | \
49- SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \
50- SUPPORT_SELECT_SOURCE | SUPPORT_BROWSE_MEDIA | SUPPORT_PLAY_MEDIA
26+ SUPPORT_YORADIO = (
27+ MediaPlayerEntityFeature .PAUSE
28+ | MediaPlayerEntityFeature .PLAY
29+ | MediaPlayerEntityFeature .STOP
30+ | MediaPlayerEntityFeature .VOLUME_SET
31+ | MediaPlayerEntityFeature .VOLUME_STEP
32+ | MediaPlayerEntityFeature .TURN_OFF
33+ | MediaPlayerEntityFeature .TURN_ON
34+
35+ | MediaPlayerEntityFeature .PREVIOUS_TRACK
36+ | MediaPlayerEntityFeature .NEXT_TRACK
37+ | MediaPlayerEntityFeature .SELECT_SOURCE
38+ | MediaPlayerEntityFeature .BROWSE_MEDIA
39+ | MediaPlayerEntityFeature .PLAY_MEDIA
40+ )
5141
5242DEFAULT_NAME = 'yoRadio'
5343CONF_MAX_VOLUME = 'max_volume'
5444CONF_ROOT_TOPIC = 'root_topic'
5545
56- PLATFORM_SCHEMA = PLATFORM_SCHEMA .extend ({
46+ MEDIA_PLAYER_PLATFORM_SCHEMA = MEDIA_PLAYER_PLATFORM_SCHEMA .extend ({
5747 vol .Required (CONF_ROOT_TOPIC , default = "yoradio" ): cv .string ,
5848 vol .Optional (CONF_NAME , default = DEFAULT_NAME ): cv .string ,
5949 vol .Optional (CONF_MAX_VOLUME , default = '254' ): cv .string
6252def setup_platform (hass , config , add_devices , discovery_info = None ):
6353 root_topic = config .get (CONF_ROOT_TOPIC )
6454 name = config .get (CONF_NAME )
65- max_volume = int (config .get (CONF_MAX_VOLUME ))
66- session = async_get_clientsession (hass )
55+ max_volume = int (config .get (CONF_MAX_VOLUME , 254 ))
6756 playlist = []
68- api = yoradioApi (root_topic , session , hass , playlist )
57+ api = yoradioApi (root_topic , hass , playlist )
6958 add_devices ([yoradioDevice (name , max_volume , api )], True )
7059
7160class yoradioApi ():
72- def __init__ (self , root_topic , session , hass , playlist ):
73- self .session = session
61+ def __init__ (self , root_topic , hass , playlist ):
7462 self .hass = hass
75- self .mqtt = hass . components . mqtt
63+ self .mqtt = mqtt
7664 self .root_topic = root_topic
7765 self .playlist = playlist
7866 self .playlisturl = ""
@@ -95,7 +83,7 @@ def fetch_data(self):
9583 html = urllib .request .urlopen (self .playlisturl ).read ().decode ("utf-8" )
9684 return str (html )
9785 except Exception as e :
98- _LOGGER .error ("Unable to fetch data : " + str (e ))
86+ _LOGGER .error (f "Unable to fetch playlist from { self . playlisturl } : " + str (e ))
9987 return ""
10088
10189 async def set_source (self , source ):
@@ -117,7 +105,7 @@ async def load_playlist(self, msg):
117105 self .playlisturl = msg .payload
118106 file = await self .hass .async_add_executor_job (self .fetch_data )
119107 except uException as e :
120- _LOGGER .error (' Error downloading ' + msg . payload )
108+ _LOGGER .error (f" Error load_playlist from { self . playlisturl } " )
121109 else :
122110 file = file .split ('\n ' )
123111 counter = 1
@@ -133,26 +121,27 @@ class yoradioDevice(MediaPlayerEntity):
133121 def __init__ (self , name , max_volume , api ):
134122 self ._name = name
135123 self .api = api
136- self ._state = STATE_OFF
124+ self ._state = MediaPlayerState . OFF
137125 self ._current_source = None
138126 self ._media_title = ''
139127 self ._track_artist = ''
140128 self ._track_album_name = ''
141129 self ._volume = 0
142- self ._muted = False
143130 self ._max_volume = max_volume
144- self .api .mqtt .subscribe (self .api .root_topic + '/status' , self .status_listener , 0 , "utf-8" )
145- self .api .mqtt .subscribe (self .api .root_topic + '/playlist' , self .playlist_listener , 0 , "utf-8" )
146- self .api .mqtt .subscribe (self .api .root_topic + '/volume' , self .volume_listener , 0 , "utf-8" )
147131
132+ async def async_added_to_hass (self ):
133+ await mqtt .async_subscribe (self .api .hass , self .api .root_topic + '/status' , self .status_listener , 0 , "utf-8" )
134+ await mqtt .async_subscribe (self .api .hass , self .api .root_topic + '/playlist' , self .playlist_listener , 0 , "utf-8" )
135+ await mqtt .async_subscribe (self .api .hass , self .api .root_topic + '/volume' , self .volume_listener , 0 , "utf-8" )
136+
148137 async def status_listener (self , msg ):
149138 js = json .loads (msg .payload )
150139 self ._media_title = js ['title' ]
151140 self ._track_artist = js ['name' ]
152141 if js ['on' ]== 1 :
153- self ._state = STATE_PLAYING if js ['status' ]== 1 else STATE_IDLE
142+ self ._state = MediaPlayerState . PLAYING if js ['status' ]== 1 else MediaPlayerState . IDLE
154143 else :
155- self ._state = STATE_PLAYING if js ['status' ]== 1 else STATE_OFF
144+ self ._state = MediaPlayerState . PLAYING if js ['status' ]== 1 else MediaPlayerState . OFF
156145 self ._current_source = str (js ['station' ]) + '. ' + js ['name' ]
157146 try :
158147 self .async_schedule_update_ha_state ()
@@ -215,27 +204,22 @@ def source_list(self):
215204 async def async_browse_media (
216205 self , media_content_type : str | None = None , media_content_id : str | None = None
217206 ) -> BrowseMedia :
218- #await self.api.set_browse_media(media_content_id)
219- """Implement the websocket media browsing helper."""
220207 return await media_source .async_browse_media (
221208 self .hass ,
222209 media_content_id ,
223210 )
224-
211+
225212 async def async_play_media (
226213 self ,
227214 media_type : str ,
228215 media_id : str ,
229216 enqueue : MediaPlayerEnqueue | None = None ,
230217 announce : bool | None = None , ** kwargs
231218 ) -> None :
232- """Play a piece of media."""
233219 if media_source .is_media_source_id (media_id ):
234- media_type = MEDIA_TYPE_URL
220+ media_type = MediaType . URL
235221 play_item = await media_source .async_resolve_media (self .hass , media_id , self .entity_id )
236222 media_id = async_process_play_media_url (self .hass , play_item .url )
237- #if media_type in (MEDIA_TYPE_URL):
238- # media_id = async_process_play_media_url(self.hass, media_id)
239223 await self .api .set_browse_media (media_id )
240224
241225 async def async_select_source (self , source ):
@@ -260,25 +244,21 @@ async def async_media_previous_track(self):
260244
261245 async def async_media_stop (self ):
262246 await self .api .set_command ("stop" )
263- self ._state = STATE_IDLE
264-
265- async def async_turn_on (self ):
266- await self .api .set_command ("start" )
267- self ._state = STATE_PLAYING
247+ self ._state = MediaPlayerState .IDLE
268248
269249 async def async_media_play (self ):
270250 await self .api .set_command ("start" )
271- self ._state = STATE_PLAYING
251+ self ._state = MediaPlayerState . PLAYING
272252
273253 async def async_media_pause (self ):
274254 await self .api .set_command ("stop" )
275- self ._state = STATE_IDLE
255+ self ._state = MediaPlayerState . IDLE
276256
277257 async def async_turn_off (self ):
278258 await self .api .set_command ("turnoff" )
279- self ._state = STATE_OFF
259+ self ._state = MediaPlayerState . OFF
280260
281261 async def async_turn_on (self , ** kwargs ):
282262 await self .api .set_command ("turnon" )
283- self ._state = STATE_ON
263+ self ._state = MediaPlayerState . ON
284264
0 commit comments