mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add kodi unique id based on discovery (#15093)
* kodi add unique id based on discovery * initialize unique_id to None * use netdisco-extracted mac_address * use an uuid instead of mac for real uniqueness * add missing docstring * verify that there is no entity already for the given unique id * whitespace fix
This commit is contained in:
parent
cf87b76b0c
commit
3208ad27ac
@ -160,6 +160,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|||||||
if DATA_KODI not in hass.data:
|
if DATA_KODI not in hass.data:
|
||||||
hass.data[DATA_KODI] = dict()
|
hass.data[DATA_KODI] = dict()
|
||||||
|
|
||||||
|
unique_id = None
|
||||||
# Is this a manual configuration?
|
# Is this a manual configuration?
|
||||||
if discovery_info is None:
|
if discovery_info is None:
|
||||||
name = config.get(CONF_NAME)
|
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
|
tcp_port = DEFAULT_TCP_PORT
|
||||||
encryption = DEFAULT_PROXY_SSL
|
encryption = DEFAULT_PROXY_SSL
|
||||||
websocket = DEFAULT_ENABLE_WEBSOCKET
|
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
|
# Only add a device once, so discovered devices do not override manual
|
||||||
# config.
|
# 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]:
|
if ip_addr in hass.data[DATA_KODI]:
|
||||||
return
|
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(
|
entity = KodiDevice(
|
||||||
hass,
|
hass,
|
||||||
name=name,
|
name=name,
|
||||||
@ -190,7 +202,8 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|||||||
password=config.get(CONF_PASSWORD),
|
password=config.get(CONF_PASSWORD),
|
||||||
turn_on_action=config.get(CONF_TURN_ON_ACTION),
|
turn_on_action=config.get(CONF_TURN_ON_ACTION),
|
||||||
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,
|
||||||
|
unique_id=unique_id)
|
||||||
|
|
||||||
hass.data[DATA_KODI][ip_addr] = entity
|
hass.data[DATA_KODI][ip_addr] = entity
|
||||||
async_add_devices([entity], update_before_add=True)
|
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,
|
def __init__(self, hass, name, host, port, tcp_port, encryption=False,
|
||||||
username=None, password=None,
|
username=None, password=None,
|
||||||
turn_on_action=None, turn_off_action=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."""
|
"""Initialize the Kodi device."""
|
||||||
import jsonrpc_async
|
import jsonrpc_async
|
||||||
import jsonrpc_websocket
|
import jsonrpc_websocket
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._name = name
|
self._name = name
|
||||||
|
self._unique_id = unique_id
|
||||||
|
|
||||||
kwargs = {
|
kwargs = {
|
||||||
'timeout': timeout,
|
'timeout': timeout,
|
||||||
@ -384,6 +399,11 @@ class KodiDevice(MediaPlayerDevice):
|
|||||||
_LOGGER.debug("Unable to fetch kodi data", exc_info=True)
|
_LOGGER.debug("Unable to fetch kodi data", exc_info=True)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return the unique id of the device."""
|
||||||
|
return self._unique_id
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the device."""
|
"""Return the state of the device."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user