component_HA_2023.7.3

This commit is contained in:
e2002
2023-07-31 17:50:13 +03:00
parent 775538ae8b
commit 341e5185d7

View File

@@ -1,15 +1,15 @@
import aiohttp
import asyncio import asyncio
import logging import logging
import voluptuous as vol import voluptuous as vol
import json import json
import urllib.request
from homeassistant.components import media_source from homeassistant.components import media_source
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
VERSION = '0.8.962' VERSION = '0.9.260'
DOMAIN = "yoradio" DOMAIN = "yoradio"
@@ -75,6 +75,7 @@ class yoradioApi():
self.mqtt = hass.components.mqtt self.mqtt = hass.components.mqtt
self.root_topic = root_topic self.root_topic = root_topic
self.playlist = playlist self.playlist = playlist
self.playlisturl = ""
async def set_command(self, command): async def set_command(self, command):
try: try:
@@ -89,6 +90,14 @@ class yoradioApi():
except: except:
await self.mqtt.async_publish(self.hass, self.root_topic + '/command', command) await self.mqtt.async_publish(self.hass, self.root_topic + '/command', command)
def fetch_data(self):
try:
html = urllib.request.urlopen(self.playlisturl).read().decode("utf-8")
return str(html)
except Exception as e:
_LOGGER.error("Unable to fetch data: " + str(e))
return ""
async def set_source(self, source): async def set_source(self, source):
number = source.split('.') number = source.split('.')
command = "play " + number[0] command = "play " + number[0]
@@ -105,10 +114,9 @@ class yoradioApi():
async def load_playlist(self, msg): async def load_playlist(self, msg):
try: try:
async with aiohttp.ClientSession() as session: self.playlisturl = msg.payload
async with session.get(msg.payload) as resp: file = await self.hass.async_add_executor_job(self.fetch_data)
file = await resp.text() except uException as e:
except aiohttp.ClientConnectorError as e:
_LOGGER.error('Error downloading ' + msg.payload) _LOGGER.error('Error downloading ' + msg.payload)
else: else:
file = file.split('\n') file = file.split('\n')