Updated own integrations for new HA version

This commit is contained in:
Martin Bauer
2021-05-01 11:54:54 +00:00
parent 6ee8c9c348
commit f6379fe687
25 changed files with 286 additions and 51 deletions

View File

@@ -4,5 +4,6 @@
"documentation": "",
"requirements": [],
"dependencies": [],
"codeowners": ["@mabau"]
"codeowners": ["@mabau"],
"version": "0.1"
}

View File

@@ -6,10 +6,10 @@ import sys
from typing import List, Tuple
import voluptuous as vol
from ..reconnecting_client import ReconnectingClient
from .radio import find_local_radio_url_by_name
from .radio import find_local_radio_url_by_name, fill_station_cache_async
from homeassistant.components.media_player import (
MediaPlayerDevice, PLATFORM_SCHEMA)
MediaPlayerEntity, PLATFORM_SCHEMA)
from homeassistant.components.media_player.const import (
ATTR_MEDIA_ENQUEUE, DOMAIN, MEDIA_TYPE_MUSIC,
SUPPORT_CLEAR_PLAYLIST, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE,
@@ -22,6 +22,7 @@ from homeassistant.const import (
STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING)
import homeassistant.helpers.config_validation as cv
from homeassistant.util.dt import utcnow
from homeassistant.helpers.aiohttp_client import async_get_clientsession
_LOGGER = logging.getLogger(__name__)
@@ -66,6 +67,8 @@ async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the squeezebox platform."""
import socket
await fill_station_cache_async(async_get_clientsession(hass))
known_servers = hass.data.get(KNOWN_SERVERS)
if known_servers is None:
@@ -249,7 +252,7 @@ class LogitechMediaServer(ReconnectingClient):
_LOGGER.warning("LMS Ignoring line " + line)
class SqueezeBoxDevice(MediaPlayerDevice):
class SqueezeBoxDevice(MediaPlayerEntity):
"""Representation of a SqueezeBox device."""
def __init__(self, lms, playerid, name, **attributes):

View File

@@ -1,7 +1,14 @@
import xml.etree.ElementTree as ET
from urllib.request import urlopen
import aiohttp
import asyncio
ompl_radio_browse_url = 'http://opml.radiotime.com/Browse.ashx'
# goto ompl.radiotime with parameter ?c=local and look for your city there then paste id here
query_url = ompl_radio_browse_url + '?id=r100765'
STATION_CACHE = {}
def radio_name_cleanup(radio_name):
@@ -17,15 +24,7 @@ def radio_name_cleanup(radio_name):
return " ".join(res)
def get_sender_information(queryURL):
"""
Example Query: GET http://opml.radiotime.com/Browse.ashx?partnerId=<partnerid>&serial=<serial>
partnerid and serial does not seem to be necessary - so we do not use it here,
"""
xmlfile = urlopen(queryURL)
tree = ET.ElementTree(file=xmlfile)
rootnode = tree.getroot()
def parse_sender_information(rootnode):
result = dict()
for node in rootnode.iter('outline'):
if 'type' not in node.attrib or node.attrib['type'] != "audio":
@@ -39,16 +38,15 @@ def get_sender_information(queryURL):
return result
def get_related_radio_stations(stationID):
query_url = ompl_radio_browse_url + '?id=' + stationID
return get_sender_information(query_url)
def get_local_radio_stations():
# goto ompl.radiotime with parameter ?c=local and look for your city there then paste id here
query_url = ompl_radio_browse_url + '?id=r100765'
result = get_sender_information(query_url)
return result
"""
Example Query: GET http://opml.radiotime.com/Browse.ashx?partnerId=<partnerid>&serial=<serial>
partnerid and serial does not seem to be necessary - so we do not use it here,
"""
xmlfile = urlopen(query_url)
tree = ET.ElementTree(file=xmlfile)
return parse_sender_information(tree.getroot()
)
def find_local_radio_url_by_name(search_name):
@@ -58,20 +56,24 @@ def find_local_radio_url_by_name(search_name):
return search_name
# first try: find a station that starts with the search expression
for radio_id, radio in find_local_radio_url_by_name.stations.items():
for radio_id, radio in STATION_CACHE.items():
if radio['name'].lower().startswith(search_name):
return radio['url']
# second try: containment is enough
for radio_id, radio in find_local_radio_url_by_name.stations.items():
for radio_id, radio in STATION_CACHE.items():
if search_name in radio['name'].lower():
return radio['url']
raise ValueError("Could not find radio station " + search_name)
find_local_radio_url_by_name.stations = get_local_radio_stations()
async def fill_station_cache_async(client_session):
print("-------- Fill station cache async")
global STATION_CACHE
async with client_session.get(query_url) as resp:
STATION_CACHE = parse_sender_information(ET.fromstring(await resp.text()))
if __name__ == '__main__':
print(find_local_radio_url_by_name("B 5 aktuell"))