add support for Kodi discovery (#13790)

* add support for Kodi discovery

* remove "too many blank lines"

* register service only once

* optimize "workflow"
This commit is contained in:
escoand 2018-04-14 14:31:12 +02:00 committed by Paulus Schoutsen
parent c3388d63a1
commit 5a5dad689b
2 changed files with 29 additions and 10 deletions

View File

@ -78,6 +78,7 @@ SERVICE_HANDLERS = {
'bose_soundtouch': ('media_player', 'soundtouch'), 'bose_soundtouch': ('media_player', 'soundtouch'),
'bluesound': ('media_player', 'bluesound'), 'bluesound': ('media_player', 'bluesound'),
'songpal': ('media_player', 'songpal'), 'songpal': ('media_player', 'songpal'),
'kodi': ('media_player', 'kodi'),
} }
OPTIONAL_SERVICE_HANDLERS = { OPTIONAL_SERVICE_HANDLERS = {

View File

@ -8,6 +8,7 @@ import asyncio
from collections import OrderedDict from collections import OrderedDict
from functools import wraps from functools import wraps
import logging import logging
import socket
import urllib import urllib
import re import re
@ -157,13 +158,29 @@ def _check_deprecated_turn_off(hass, turn_off_action):
def async_setup_platform(hass, config, async_add_devices, discovery_info=None): def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Set up the Kodi platform.""" """Set up the Kodi platform."""
if DATA_KODI not in hass.data: if DATA_KODI not in hass.data:
hass.data[DATA_KODI] = [] hass.data[DATA_KODI] = dict()
# Is this a manual configuration?
if discovery_info is None:
name = config.get(CONF_NAME) name = config.get(CONF_NAME)
host = config.get(CONF_HOST) host = config.get(CONF_HOST)
port = config.get(CONF_PORT) port = config.get(CONF_PORT)
tcp_port = config.get(CONF_TCP_PORT) tcp_port = config.get(CONF_TCP_PORT)
encryption = config.get(CONF_PROXY_SSL) encryption = config.get(CONF_PROXY_SSL)
websocket = config.get(CONF_ENABLE_WEBSOCKET) websocket = config.get(CONF_ENABLE_WEBSOCKET)
else:
name = "{} ({})".format(DEFAULT_NAME, discovery_info.get('hostname'))
host = discovery_info.get('host')
port = discovery_info.get('port')
tcp_port = DEFAULT_TCP_PORT
encryption = DEFAULT_PROXY_SSL
websocket = DEFAULT_ENABLE_WEBSOCKET
# Only add a device once, so discovered devices do not override manual
# config.
ip_addr = socket.gethostbyname(host)
if ip_addr in hass.data[DATA_KODI]:
return
entity = KodiDevice( entity = KodiDevice(
hass, hass,
@ -175,7 +192,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
turn_off_action=config.get(CONF_TURN_OFF_ACTION), turn_off_action=config.get(CONF_TURN_OFF_ACTION),
timeout=config.get(CONF_TIMEOUT), websocket=websocket) timeout=config.get(CONF_TIMEOUT), websocket=websocket)
hass.data[DATA_KODI].append(entity) hass.data[DATA_KODI][ip_addr] = entity
async_add_devices([entity], update_before_add=True) async_add_devices([entity], update_before_add=True)
@asyncio.coroutine @asyncio.coroutine
@ -189,10 +206,11 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
if key != 'entity_id'} if key != 'entity_id'}
entity_ids = service.data.get('entity_id') entity_ids = service.data.get('entity_id')
if entity_ids: if entity_ids:
target_players = [player for player in hass.data[DATA_KODI] target_players = [player
for player in hass.data[DATA_KODI].values()
if player.entity_id in entity_ids] if player.entity_id in entity_ids]
else: else:
target_players = hass.data[DATA_KODI] target_players = hass.data[DATA_KODI].values()
update_tasks = [] update_tasks = []
for player in target_players: for player in target_players: