def main(): # Discover Nuimo Controllers nuimo = NuimoController('CE:D8:42:A7:9A:78') #adapter = 'hci0' # Typical bluetooth adapter name #nuimo_manager = NuimoDiscoveryManager(bluetooth_adapter=adapter, delegate=DiscoveryLogger()) #nuimo_manager.start_discovery() # Were any Nuimos found? #if len(nuimo_manager.nuimos) == 0: # print('No Nuimos detected') # sys.exit(0) # Take the first Nuimo found. #nuimo = nuimo_manager.nuimos[0] # Set up handling of Nuimo events. # In this case just log each incoming event. # NuimoLogger is defined below. nuimo_event_delegate = NuimoLightController() nuimo.set_delegate(nuimo_event_delegate) # Attach to the Nuimo. nuimo.connect() # Display an icon for 2 seconds interval = 2.0 MATRIX_LIGHTBULB = ( " " + " ... " + " . . " + " . . " + " . . " + " ... " + " ... " + " ... " + " . ") nuimo.write_matrix(MATRIX_LIGHTBULB, interval) # Nuimo events are dispatched in the background while (True): time.sleep(1) nuimo.disconnect()
class NuimoThread(threading.Thread): """Manage one Nuimo controller.""" def __init__(self, hass, mac, name): """Initialize thread object.""" super(NuimoThread, self).__init__() self._hass = hass self._mac = mac self._name = name self._hass_is_running = True self._nuimo = None hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, self.stop) def run(self): """Setup connection or be idle.""" while self._hass_is_running: if not self._nuimo or not self._nuimo.is_connected(): self._attach() self._connect() else: time.sleep(1) if self._nuimo: self._nuimo.disconnect() self._nuimo = None # pylint: disable=unused-argument def stop(self, event): """Terminate Thread by unsetting flag.""" _LOGGER.debug('Stopping thread for Nuimo %s', self._mac) self._hass_is_running = False def _attach(self): """Create a nuimo object from mac address or discovery.""" # pylint: disable=import-error from nuimo import NuimoController, NuimoDiscoveryManager if self._nuimo: self._nuimo.disconnect() self._nuimo = None if self._mac: self._nuimo = NuimoController(self._mac) else: nuimo_manager = NuimoDiscoveryManager( bluetooth_adapter=DEFAULT_ADAPTER, delegate=DiscoveryLogger()) nuimo_manager.start_discovery() # Were any Nuimos found? if not nuimo_manager.nuimos: _LOGGER.debug('No Nuimos detected') return # Take the first Nuimo found. self._nuimo = nuimo_manager.nuimos[0] self._mac = self._nuimo.addr def _connect(self): """Build up connection and set event delegator and service.""" if not self._nuimo: return try: self._nuimo.connect() _LOGGER.debug('connected to %s', self._mac) except RuntimeError as error: _LOGGER.error('could not connect to %s: %s', self._mac, error) time.sleep(1) return nuimo_event_delegate = NuimoLogger(self._hass, self._name) self._nuimo.set_delegate(nuimo_event_delegate) def handle_write_matrix(call): """Handle led matrix service.""" matrix = call.data.get('matrix', None) name = call.data.get(CONF_NAME, DEFAULT_NAME) interval = call.data.get('interval', DEFAULT_INTERVAL) if self._name == name and matrix: self._nuimo.write_matrix(matrix, interval) self._hass.services.register(DOMAIN, SERVICE_NUIMO, handle_write_matrix, schema=SERVICE_NUIMO_SCHEMA) self._nuimo.write_matrix(HOMEASSIST_LOGO, 2.0)
class NuimoThread(threading.Thread): """Manage one Nuimo controller.""" def __init__(self, hass, mac, name): """Initialize thread object.""" super(NuimoThread, self).__init__() self._hass = hass self._mac = mac self._name = name self._hass_is_running = True self._nuimo = None hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, self.stop) def run(self): """Set up the connection or be idle.""" while self._hass_is_running: if not self._nuimo or not self._nuimo.is_connected(): self._attach() self._connect() else: time.sleep(1) if self._nuimo: self._nuimo.disconnect() self._nuimo = None def stop(self, event): """Terminate Thread by unsetting flag.""" _LOGGER.debug('Stopping thread for Nuimo %s', self._mac) self._hass_is_running = False def _attach(self): """Create a Nuimo object from MAC address or discovery.""" # pylint: disable=import-error from nuimo import NuimoController, NuimoDiscoveryManager if self._nuimo: self._nuimo.disconnect() self._nuimo = None if self._mac: self._nuimo = NuimoController(self._mac) else: nuimo_manager = NuimoDiscoveryManager( bluetooth_adapter=DEFAULT_ADAPTER, delegate=DiscoveryLogger()) nuimo_manager.start_discovery() # Were any Nuimos found? if not nuimo_manager.nuimos: _LOGGER.debug("No Nuimo devices detected") return # Take the first Nuimo found. self._nuimo = nuimo_manager.nuimos[0] self._mac = self._nuimo.addr def _connect(self): """Build up connection and set event delegator and service.""" if not self._nuimo: return try: self._nuimo.connect() _LOGGER.debug("Connected to %s", self._mac) except RuntimeError as error: _LOGGER.error("Could not connect to %s: %s", self._mac, error) time.sleep(1) return nuimo_event_delegate = NuimoLogger(self._hass, self._name) self._nuimo.set_delegate(nuimo_event_delegate) def handle_write_matrix(call): """Handle led matrix service.""" matrix = call.data.get('matrix', None) name = call.data.get(CONF_NAME, DEFAULT_NAME) interval = call.data.get('interval', DEFAULT_INTERVAL) if self._name == name and matrix: self._nuimo.write_matrix(matrix, interval) self._hass.services.register( DOMAIN, SERVICE_NUIMO, handle_write_matrix, schema=SERVICE_NUIMO_SCHEMA) self._nuimo.write_matrix(HOMEASSIST_LOGO, 2.0)