ronaldoussoren / pyobjc

The Python <-> Objective-C Bridge with bindings for macOS frameworks
https://pyobjc.readthedocs.io
573 stars 47 forks source link

HomeKit support #501

Open glyph opened 2 years ago

glyph commented 2 years ago

Is your feature request related to a problem? Please describe. Nope, just hoping to use the framework.

Describe the solution you'd like Would it be possible to just get from HomeKit import HMHomeManager to work?

Describe alternatives you've considered The various HomeKit libraries in Python seem to be for cutting Apple out of the loop entirely (implementing a controller on Linux or emulating an accessory), so I don't think there's something that does this.

ronaldoussoren commented 2 years ago

A possible issue is that HomeKit is a catalyst framework. That said, AFAIK it is possible to mix usage of catalyst in cocoa in applications.

ronaldoussoren commented 2 years ago

The following has limited success:

import objc
objc.loadBundle('HomeKit', globals(), '/System/Library/PrivateFrameworks/HomeKit.framework')
manager = HMHomeManager.new()
print(manager.homes())

This works, but prints an empty set of homes even though I see a "home" in Home.app.

Loading /System/iOSSupport/System/Library/Frameworks/HomeKit.framework (which Home.app uses) doesn't work for me, I get a generic "Bundle could not be loaded" error.

Using NSBundle.bundleWithPath_ to load the latter framework gives a clearer error message:

 2022-10-15 21:56:36.971 Python[91353:14405081] Error loading /System/iOSSupport/System/Library/Frameworks/HomeKit.framework/HomeKit:  dlopen(/System/iOSSupport/System/Library/Frameworks/HomeKit.framework/HomeKit, 0x0109): tried: '/System/iOSSupport/System/Library/Frameworks/HomeKit.framework/Versions/A/HomeKit' (wrong platform to load into process), '/System/Library/Frameworks/HomeKit.framework/Versions/A/HomeKit' (no such file)

Some very short research seems to indicate that it is possible to load an AppKit extension into a Catalyst app, but that it is at best a lot harder to do it the other way around (https://indiedevmonday.com/issue-18, https://github.com/steventroughtonsmith/airqualitymonitor, https://twitter.com/stroughtonsmith/status/1313474865467453441).

More importantly: https://developer.apple.com/forums/thread/692356, which contains a definitive statement from an Apple engineer that says loading Catalyst frameworks into an AppKit app is not possible.

That sadly means that using the HomeKit framework requires either using the private HomeKit framework (assuming that can be made to work), or creating a Catalyst version of Python + PyObjC. That's something I'd like to look into, but I have no idea when.

ronaldoussoren commented 2 years ago

A Catalyst version of Python would be an interesting proposition, but is something that will require coordinating with at least the PyPA and PyPI maintainers, as well as with CPython to upstream relevant patches (including CI support).

A major disadvantage of a Catalyst Python is that it likely cannot use existing binary wheels.

ronaldoussoren commented 2 years ago

I've added an issue about a Catalyst build to the CPython project: https://github.com/python/cpython/issues/98297

To be honest I expect little progress on that until I get around to work on this myself (assuming I do get around to that).