Ejemplo n.º 1
0
async def getClient(ip_address):
    client = HarmonyAPI(ip_address)

    if await client.connect():
        return client

    return None
Ejemplo n.º 2
0
async def get_client(ip_address, protocol,
                     show_responses) -> Optional[HarmonyAPI]:
    client = HarmonyAPI(ip_address=ip_address, protocol=protocol)

    def output_response(message):
        print(f"{client.name}: {message}")

    if show_responses:
        listen_callback = Handler(handler_obj=output_response,
                                  handler_name='output_response',
                                  once=False)
        client.register_handler(handler=listen_callback)

    print(f"Trying to connect to HUB with IP {ip_address}.")
    try:
        if await client.connect():
            print(
                "Connected to HUB {} ({}) with firmware version {} and HUB ID {} using protocol {}"
                .format(client.name, ip_address, client.fw_version,
                        client.hub_id, client.protocol))
            return client
    except ConnectionRefusedError:
        print(f"Failed to connect to HUB {ip_address}.")

    print("An issue occurred trying to connect")

    return None
Ejemplo n.º 3
0
async def get_client():
    client = HarmonyAPI(HUB_IP, HUB_PROTOCOL)
    try:
        if await client.connect():
            return client
    except:
        pass
    return None
Ejemplo n.º 4
0
async def get_harmony_client_if_available(ip_address: str):
    """Connect to a harmony hub and fetch info."""
    harmony = HarmonyAPI(ip_address=ip_address)

    try:
        if not await harmony.connect():
            await harmony.close()
            return None
    except harmony_exceptions.TimeOut:
        return None

    await harmony.close()

    return harmony
Ejemplo n.º 5
0
async def get_client(ip_address, show_responses) -> Optional[HarmonyAPI]:
    client = HarmonyAPI(ip_address)

    def output_response(message):
        print(message)

    listen_callback = Handler(handler_obj=output_response,
                              handler_name='output_response',
                              once=False)
    if show_responses:
        client.register_handler(handler=listen_callback)

    if await client.connect():
        print("Connected to HUB {} with firmware version {}".format(
            client.name, client.fw_version))
        return client

    print("An issue occured trying to connect")

    return None
Ejemplo n.º 6
0
    async def connect(self):
        # Client
        self.client = HarmonyAPI(self.config.get('host'),
                                 loop=self.context.loop)

        # Connect
        try:
            if not await self.client.connect():
                logger.warning(
                    'Unable to connect to Harmony HUB, name=%s, fw=%s' %
                    (self.client.name, self.client.fw_version))

                await self.client.close()

        except aioexc.TimeOut:
            logger.warning('Harmony HUB connection timed out')

        logger.info('Connected to Harmony HUB, name=%s, fw=%s' %
                    (self.client.name, self.client.fw_version))

        self.state.hub = {
            'name': self.client.name,
            'fw': self.client.fw_version,
        }

        # Register callbacks
        self.client.callbacks = ClientCallbackType(
            new_activity=self.callbacks.new_activity,
            config_updated=self.callbacks.new_config,
            connect=self.callbacks.connected,
            disconnect=self.callbacks.disconnected)

        # Config
        with open('harmony.txt', 'w') as outfile:
            json.dump(self.client.config, outfile)
        # print(json.dumps(self.client.config))

        self.activities = {
            str(a['id']): a['label']
            for a in self.client.config.get('activity', [])
        }
        self.state.activities = self.activities

        logger.debug('> Activities:')

        for aid, name in self.activities.items():
            logger.debug('   %s:%s' % (str(aid), name))

        self.devices = {
            str(a['id']): a['label']
            for a in self.client.config.get('device', [])
        }
        self.state.devices = self.devices

        logger.debug('> Devices:')

        for did, name in self.devices.items():
            logger.debug('   %s:%s' % (str(did), name))

        # Update current activity
        activity_id, activity_name = self.client.current_activity

        self.state.connected = True
        self.state.current_activity = activity_name

        logger.info('> Current activity, id=%s, name=%s' %
                    (str(activity_id), activity_name))