Prevent devices from being discovered twice

This commit is contained in:
Paulus Schoutsen 2015-01-10 10:34:56 -08:00
parent 9db1a58cb1
commit c2b8f8d34e
5 changed files with 26 additions and 4 deletions

View File

@ -113,6 +113,11 @@ def media_prev_track(hass, entity_id=None):
def setup_chromecast(casts, host): def setup_chromecast(casts, host):
""" Tries to convert host to Chromecast object and set it up. """ """ 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: try:
cast = pychromecast.PyChromecast(host) cast = pychromecast.PyChromecast(host)

View File

@ -81,6 +81,12 @@ class HueLight(ToggleDevice):
""" Get the mame of the Hue light. """ """ Get the mame of the Hue light. """
return self.info['name'] 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): def turn_on(self, **kwargs):
""" Turn the specified or all lights on. """ """ Turn the specified or all lights on. """
command = {'on': True} command = {'on': True}

View File

@ -86,7 +86,7 @@ def setup(hass, config):
switch = platform.device_discovered(hass, config, info) 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( switch.entity_id = util.ensure_unique_string(
ENTITY_ID_FORMAT.format(util.slugify(switch.get_name())), ENTITY_ID_FORMAT.format(util.slugify(switch.get_name())),
switches.keys()) switches.keys())

View File

@ -59,6 +59,11 @@ class WemoSwitch(ToggleDevice):
def __init__(self, wemo): def __init__(self, wemo):
self.wemo = 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): def get_name(self):
""" Returns the name of the switch if any. """ """ Returns the name of the switch if any. """
return self.wemo.name return self.wemo.name

View File

@ -146,9 +146,6 @@ def platform_devices_from_config(config, domain, hass,
devices.extend(p_devices) devices.extend(p_devices)
if len(devices) == 0:
logger.error("No devices found for %s", domain)
# Setup entity IDs for each device # Setup entity IDs for each device
no_name_count = 1 no_name_count = 1
@ -177,6 +174,11 @@ class Device(object):
entity_id = None entity_id = None
@property
def unique_id(self):
""" Returns a unique id. """
return "{}.{}".format(self.__class__, id(self))
def get_name(self): def get_name(self):
""" Returns the name of the device if any. """ """ Returns the name of the device if any. """
return "No Name" return "No Name"
@ -208,6 +210,10 @@ class Device(object):
return hass.states.set(self.entity_id, self.get_state(), return hass.states.set(self.entity_id, self.get_state(),
self.get_state_attributes()) self.get_state_attributes())
def __eq__(self, other):
return (isinstance(other, Device) and
other.unique_id == self.unique_id)
class ToggleDevice(Device): class ToggleDevice(Device):
""" ABC for devices that can be turned on and off. """ """ ABC for devices that can be turned on and off. """