HAP-python icon indicating copy to clipboard operation
HAP-python copied to clipboard

How to run multiple accessories in 1 process?

Open ixje opened this issue 3 years ago • 2 comments

I'm trying to run multiple temperature sensors in a single process (without a bridge). Running an unmodified main.py and pairing a single accessory works fine.

When I modify it slightly to

async def main():
    driver1 = AccessoryDriver(port=51826, persist_file='state1')
    driver1.add_accessory(accessory=TemperatureSensor(driver1, 'kitchen'))
    signal.signal(signal.SIGTERM, driver1.signal_handler)
    await driver1.async_start()

    driver2 = AccessoryDriver(port=51827, persist_file='state2')
    driver2.add_accessory(accessory=TemperatureSensor(driver2, 'office'))
    signal.signal(signal.SIGTERM, driver2.signal_handler)
    await driver2.async_start()

if __name__ == "__main__":
    loop = asyncio.new_event_loop()
    loop.create_task(main())
    loop.run_forever()

then run it, I get

  [selector_events] Using selector: KqueueSelector
  [selector_events] Using selector: KqueueSelector
  [characteristic] set_value: Name to kitchen
  [characteristic] set_value: SerialNumber to default
  [accessory_driver] Storing Accessory state in `state1`
  [accessory_driver] Writing of accessory state to disk
  [accessory_driver] Starting accessory kitchen on address 192.168.1.100, port 51826.
  [accessory_driver] Starting server.
  [accessory_driver] Get accessories response: [{'aid': 1, 'services': [{'iid': 1, 'type': '3E', 'characteristics': [{'iid': 2, 'type': '14', 'perms': ['pw'], 'format': 'bool'}, {'iid': 3, 'type': '20', 'perms': ['pr'], 'format': 'string', 'value': ''}, {'iid': 4, 'type': '21', 'perms': ['pr'], 'format': 'string', 'value': ''}, {'iid': 5, 'type': '23', 'perms': ['pr'], 'format': 'string', 'value': 'kitchen'}, {'iid': 6, 'type': '30', 'perms': ['pr'], 'format': 'string', 'value': 'default'}, {'iid': 7, 'type': '52', 'perms': ['pr'], 'format': 'string', 'value': ''}]}, {'iid': 8, 'type': '8A', 'characteristics': [{'iid': 9, 'type': '11', 'perms': ['pr', 'ev'], 'format': 'float', 'minValue': -273.1, 'maxValue': 1000, 'unit': 'celsius', 'minStep': 0.1, 'value': 0.0}]}, {'iid': 10, 'type': '82', 'characteristics': [{'iid': 11, 'type': '10', 'perms': ['pr', 'ev'], 'format': 'float', 'minValue': 0, 'maxValue': 100, 'unit': 'percentage', 'minStep': 1, 'value': 0}]}]}]
  [accessory_driver] Scheduling write of accessory state to disk
  [accessory_driver] Writing of accessory state to disk
  [accessory_driver] Starting mDNS.
  [accessory_driver] Starting accessory kitchen
  [accessory_driver] AccessoryDriver for kitchen started successfully
  [selector_events] Using selector: KqueueSelector
  [characteristic] set_value: Name to office
  [characteristic] set_value: SerialNumber to default
  [accessory_driver] Storing Accessory state in `state2`
  [accessory_driver] Writing of accessory state to disk
  [accessory_driver] Starting accessory office on address 192.168.1.100, port 51827.
  [accessory_driver] Starting server.
  [accessory_driver] Get accessories response: [{'aid': 1, 'services': [{'iid': 1, 'type': '3E', 'characteristics': [{'iid': 2, 'type': '14', 'perms': ['pw'], 'format': 'bool'}, {'iid': 3, 'type': '20', 'perms': ['pr'], 'format': 'string', 'value': ''}, {'iid': 4, 'type': '21', 'perms': ['pr'], 'format': 'string', 'value': ''}, {'iid': 5, 'type': '23', 'perms': ['pr'], 'format': 'string', 'value': 'office'}, {'iid': 6, 'type': '30', 'perms': ['pr'], 'format': 'string', 'value': 'default'}, {'iid': 7, 'type': '52', 'perms': ['pr'], 'format': 'string', 'value': ''}]}, {'iid': 8, 'type': '8A', 'characteristics': [{'iid': 9, 'type': '11', 'perms': ['pr', 'ev'], 'format': 'float', 'minValue': -273.1, 'maxValue': 1000, 'unit': 'celsius', 'minStep': 0.1, 'value': 0.0}]}, {'iid': 10, 'type': '82', 'characteristics': [{'iid': 11, 'type': '10', 'perms': ['pr', 'ev'], 'format': 'float', 'minValue': 0, 'maxValue': 100, 'unit': 'percentage', 'minStep': 1, 'value': 0}]}]}]
  [accessory_driver] Scheduling write of accessory state to disk
  [accessory_driver] Starting mDNS.
  [accessory_driver] Writing of accessory state to disk
  [accessory_driver] Starting accessory office
  [accessory_driver] AccessoryDriver for office started successfully
  To use the QR Code feature, use 'pip install HAP-python[QRCode]'
  [kitchen] Enter this code in your HomeKit app on your iOS device: 574-69-969
  To use the QR Code feature, use 'pip install HAP-python[QRCode]'
  [office] Enter this code in your HomeKit app on your iOS device: 445-95-191

I see both sensors in the Home app, but after entering the setup code it just hangs trying to connect until it times out. I see no further logs along the lines of "accepted connection". Any tips/hints what I might be missing or should look at?

ixje avatar Dec 07 '22 12:12 ixje

I'm facing the same problem right now, but I just found the solution. You need to pass the loop to AccessoryDriver

jobenni avatar Dec 09 '22 13:12 jobenni

pretty sure I tried that, but will give it another go. Thanks for the tip

ixje avatar Dec 09 '22 14:12 ixje