diff --git a/homeassistant/components/media_player/kodi.py b/homeassistant/components/media_player/kodi.py index 7fa8d5b3fe84f7..059ab141a00f5e 100644 --- a/homeassistant/components/media_player/kodi.py +++ b/homeassistant/components/media_player/kodi.py @@ -160,6 +160,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): if DATA_KODI not in hass.data: hass.data[DATA_KODI] = dict() + unique_id = None # Is this a manual configuration? if discovery_info is None: name = config.get(CONF_NAME) @@ -175,6 +176,9 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): tcp_port = DEFAULT_TCP_PORT encryption = DEFAULT_PROXY_SSL websocket = DEFAULT_ENABLE_WEBSOCKET + properties = discovery_info.get('properties') + if properties is not None: + unique_id = properties.get('uuid', None) # Only add a device once, so discovered devices do not override manual # config. @@ -182,6 +186,14 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): if ip_addr in hass.data[DATA_KODI]: return + # If we got an unique id, check that it does not exist already. + # This is necessary as netdisco does not deterministally return the same + # advertisement when the service is offered over multiple IP addresses. + if unique_id is not None: + for device in hass.data[DATA_KODI].values(): + if device.unique_id == unique_id: + return + entity = KodiDevice( hass, name=name, @@ -190,7 +202,8 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): password=config.get(CONF_PASSWORD), turn_on_action=config.get(CONF_TURN_ON_ACTION), turn_off_action=config.get(CONF_TURN_OFF_ACTION), - timeout=config.get(CONF_TIMEOUT), websocket=websocket) + timeout=config.get(CONF_TIMEOUT), websocket=websocket, + unique_id=unique_id) hass.data[DATA_KODI][ip_addr] = entity async_add_devices([entity], update_before_add=True) @@ -260,12 +273,14 @@ class KodiDevice(MediaPlayerDevice): def __init__(self, hass, name, host, port, tcp_port, encryption=False, username=None, password=None, turn_on_action=None, turn_off_action=None, - timeout=DEFAULT_TIMEOUT, websocket=True): + timeout=DEFAULT_TIMEOUT, websocket=True, + unique_id=None): """Initialize the Kodi device.""" import jsonrpc_async import jsonrpc_websocket self.hass = hass self._name = name + self._unique_id = unique_id kwargs = { 'timeout': timeout, @@ -384,6 +399,11 @@ def _get_players(self): _LOGGER.debug("Unable to fetch kodi data", exc_info=True) return None + @property + def unique_id(self): + """Return the unique id of the device.""" + return self._unique_id + @property def state(self): """Return the state of the device."""