From c2b8f8d34e18a9549dd28624f6c9f868f8781173 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 10 Jan 2015 10:34:56 -0800 Subject: [PATCH] Prevent devices from being discovered twice --- homeassistant/components/chromecast.py | 5 +++++ homeassistant/components/light/hue.py | 6 ++++++ homeassistant/components/switch/__init__.py | 2 +- homeassistant/components/switch/wemo.py | 5 +++++ homeassistant/helpers.py | 12 +++++++++--- 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/chromecast.py b/homeassistant/components/chromecast.py index b15bd8b08a7..1736f0d2444 100644 --- a/homeassistant/components/chromecast.py +++ b/homeassistant/components/chromecast.py @@ -113,6 +113,11 @@ def media_prev_track(hass, entity_id=None): def setup_chromecast(casts, host): """ Tries to convert host to Chromecast object and set it up. """ + + # Check if already setup + if any(cast.host == host for cast in casts.values()): + return + try: cast = pychromecast.PyChromecast(host) diff --git a/homeassistant/components/light/hue.py b/homeassistant/components/light/hue.py index bf8dae839c5..d8e7d2705b0 100644 --- a/homeassistant/components/light/hue.py +++ b/homeassistant/components/light/hue.py @@ -81,6 +81,12 @@ class HueLight(ToggleDevice): """ Get the mame of the Hue light. """ return self.info['name'] + @property + def unique_id(self): + """ Returns the id of this Hue light """ + return "{}.{}".format( + self.__class__, self.info.get('uniqueid', self.get_name())) + def turn_on(self, **kwargs): """ Turn the specified or all lights on. """ command = {'on': True} diff --git a/homeassistant/components/switch/__init__.py b/homeassistant/components/switch/__init__.py index 3f5e0468040..caf6355d179 100644 --- a/homeassistant/components/switch/__init__.py +++ b/homeassistant/components/switch/__init__.py @@ -86,7 +86,7 @@ def setup(hass, config): switch = platform.device_discovered(hass, config, info) - if switch is not None: + if switch is not None and switch not in switches.values(): switch.entity_id = util.ensure_unique_string( ENTITY_ID_FORMAT.format(util.slugify(switch.get_name())), switches.keys()) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 98bbfcefa0c..63c6fe3b815 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -59,6 +59,11 @@ class WemoSwitch(ToggleDevice): def __init__(self, wemo): self.wemo = wemo + @property + def unique_id(self): + """ Returns the id of this WeMo switch """ + return "{}.{}".format(self.__class__, self.wemo.serialnumber) + def get_name(self): """ Returns the name of the switch if any. """ return self.wemo.name diff --git a/homeassistant/helpers.py b/homeassistant/helpers.py index 4e479de1472..a28befea07e 100644 --- a/homeassistant/helpers.py +++ b/homeassistant/helpers.py @@ -146,9 +146,6 @@ def platform_devices_from_config(config, domain, hass, devices.extend(p_devices) - if len(devices) == 0: - logger.error("No devices found for %s", domain) - # Setup entity IDs for each device no_name_count = 1 @@ -177,6 +174,11 @@ class Device(object): entity_id = None + @property + def unique_id(self): + """ Returns a unique id. """ + return "{}.{}".format(self.__class__, id(self)) + def get_name(self): """ Returns the name of the device if any. """ return "No Name" @@ -208,6 +210,10 @@ class Device(object): return hass.states.set(self.entity_id, self.get_state(), self.get_state_attributes()) + def __eq__(self, other): + return (isinstance(other, Device) and + other.unique_id == self.unique_id) + class ToggleDevice(Device): """ ABC for devices that can be turned on and off. """