def main(): # init logger logger = logging.getLogger('homekit-flux-led') formatter = logging.Formatter('[%(levelname)s %(asctime)s]: %(message)s') ch = logging.StreamHandler() ch.setFormatter(formatter) logger.addHandler(ch) # parse cli-arguments parser = argparse.ArgumentParser() parser.add_argument( '-v', '--verbose', help='DEBUG logging mode', action='store_true', ) args = parser.parse_args() # set loggin level if args.verbose: logger.setLevel(logging.DEBUG) logger.info('Logging level: DEBUG') else: logger.setLevel(logging.INFO) logger.info('Logging level: INFO') # scan for bulbs logger.debug('Scanning for lightbulbs') scanner = BulbScanner() scanner.scan() logger.debug(f'Found lightbulbs:') logger.debug(scanner.found_bulbs) # initialize AccessoryServer logger.debug('Create HomeKit-AccessoryServer...') driver = AccessoryDriver() bridge = Bridge(driver, 'MagicHome') logger.debug('HomeKit-AccessoryServer created successful.') for bulb in scanner.found_bulbs: # add bulbs to bridge logger bridge.add_accessory( FluxLED( driver, bulb['id'], bulb['ipaddr'], logger=logger, ), ) # add bridge to the driver driver.add_accessory(bridge) try: # start the server logger.info('Start server...') driver.start() except KeyboardInterrupt: logger.info('Stopping server...') driver.stop()
class HomeKit(): """Class to handle all actions between HomeKit and Home Assistant.""" def __init__(self, hass, port): """Initialize a HomeKit object.""" self._hass = hass self._port = port self.bridge = None self.driver = None def setup_bridge(self, pin): """Setup the bridge component to track all accessories.""" from .accessories import HomeBridge self.bridge = HomeBridge(BRIDGE_NAME, 'homekit.bridge', pin) def start_driver(self, event): """Start the accessory driver.""" from pyhap.accessory_driver import AccessoryDriver self._hass.bus.listen_once( EVENT_HOMEASSISTANT_STOP, self.stop_driver) import_types() _LOGGER.debug("Start adding accessories.") for state in self._hass.states.all(): acc = get_accessory(self._hass, state) if acc is not None: self.bridge.add_accessory(acc) ip_address = get_local_ip() path = self._hass.config.path(HOMEKIT_FILE) self.driver = AccessoryDriver(self.bridge, self._port, ip_address, path) _LOGGER.debug("Driver started") self.driver.start() def stop_driver(self, event): """Stop the accessory driver.""" _LOGGER.debug("Driver stop") if self.driver is not None: self.driver.stop()
class Homekit(): """Class to handle all actions between homekit and Home Assistant.""" def __init__(self, hass, port): """Initialize a homekit object.""" self._hass = hass self._port = port self.bridge = None self.driver = None def setup_bridge(self, pin): """Setup the bridge component to track all accessories.""" from .accessories import HomeBridge self.bridge = HomeBridge(BRIDGE_NAME, pincode=pin) self.bridge.set_accessory_info('homekit.bridge') def start_driver(self, event): """Start the accessory driver.""" from pyhap.accessory_driver import AccessoryDriver self._hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, self.stop_driver) import_types() _LOGGER.debug("Start adding accessories.") for state in self._hass.states.all(): acc = get_accessory(self._hass, state) if acc is not None: self.bridge.add_accessory(acc) ip_address = get_local_ip() path = self._hass.config.path(HOMEKIT_FILE) self.driver = AccessoryDriver(self.bridge, self._port, ip_address, path) _LOGGER.debug("Driver started") self.driver.start() def stop_driver(self, event): """Stop the accessory driver.""" _LOGGER.debug("Driver stop") if self.driver is not None: self.driver.stop()