dgreif / homebridge-hatch-baby-rest

A homebridge plugin for Hatch Baby Rest night light
MIT License
74 stars 19 forks source link

Doesn't seem to connect properly when started via hb-service #81

Closed dancavallaro closed 2 years ago

dancavallaro commented 2 years ago

I've got this working now with my Hatch Rest and integrated with HomeKit (this is super awesome, thanks for doing this!) but I've noticed something odd where this plugin doesn't seem to work properly when I start Homebridge via sudo hb-service start, but does work when I start it from the command-line by running homebridge. More specifically: when I start HB by running homebridge and watch the logs, I see this plugin startup and configure my cached Rest, then it scans for the device and finds it, then when I try interacting with it via HomeKit (e.g. turn it on) I see Connecting to <device name>... and Connected to <device name> messages in the logs, and the actions work. But when I start HB via sudo hb-service start, I see the same first couple messages about configuring the cached device, but it never scans for the device, and then when I interact via HomeKit I never see any of those connection log messages, and the actions don't have any effect.

Here's a snippet of the logs when I start via hb-service start:

[8/15/2022, 8:05:05 AM] [HatchBabyRest] Initializing HatchBabyRest platform...
[8/15/2022, 8:05:05 AM] [HatchBabyRest] Configuring cached accessory 0c45a68a-df4a-4ec0-a78c-7d1e7c9b6914 Nursery
[8/15/2022, 8:05:06 AM] [HatchBabyRest] Waiting for bluetooth to power on
[8/15/2022, 8:05:06 AM] [HatchBabyRest] Configuring 1 Rest, 0 Rest+, 0 Rest Mini, 0 Rest 2nd Gen, and 0 Restore
Setup Payload:
<truncated>
...
[8/15/2022, 8:05:06 AM] Homebridge v1.5.0 (HAP v0.10.2) (Homebridge B15C) is running on port 51583.
[8/15/2022, 8:07:12 AM] [HatchBabyRest] Turning on Nursery
[8/15/2022, 8:07:14 AM] [HatchBabyRest] Turning off Nursery
[8/15/2022, 8:07:15 AM] [HatchBabyRest] Turning on Nursery
[8/15/2022, 8:07:26 AM] [HatchBabyRest] Turning off Nursery

Here's when I start via homebridge, and you can see the additional log messages:

[8/15/2022, 8:08:20 AM] [HatchBabyRest] Initializing HatchBabyRest platform...
[8/15/2022, 8:08:20 AM] [HatchBabyRest] Configuring cached accessory 0c45a68a-df4a-4ec0-a78c-7d1e7c9b6914 Nursery
[8/15/2022, 8:08:20 AM] [HatchBabyRest] Waiting for bluetooth to power on
[8/15/2022, 8:08:20 AM] [HatchBabyRest] Configuring 1 Rest, 0 Rest+, 0 Rest Mini, 0 Rest 2nd Gen, and 0 Restore
Setup Payload:
<truncated>
...
[8/15/2022, 8:08:20 AM] Homebridge v1.5.0 (HAP v0.10.2) (Homebridge B15C) is running on port 51583.
[8/15/2022, 8:08:20 AM] [HatchBabyRest] Scanning for Nursery
[8/15/2022, 8:08:21 AM] [HatchBabyRest] Found device Nursery  with address ef-d8-7b-5c-59-92
[8/15/2022, 8:08:26 AM] [Homebridge UI] Homebridge Config UI X v4.50.1 is listening on :: port 8581
[8/15/2022, 8:09:27 AM] [HatchBabyRest] Turning on Nursery
[8/15/2022, 8:09:27 AM] [HatchBabyRest] Connecting to Nursery ...
[8/15/2022, 8:09:28 AM] [HatchBabyRest] Connected to Nursery 
[8/15/2022, 8:09:34 AM] [HatchBabyRest] Disconnected from Nursery 
[8/15/2022, 8:09:41 AM] [HatchBabyRest] Turning off Nursery
[8/15/2022, 8:09:41 AM] [HatchBabyRest] Connecting to Nursery ...
[8/15/2022, 8:09:42 AM] [HatchBabyRest] Connected to Nursery

For context I'm testing this on a Mac (OS X 11.6 Big Sur), using Homebridge 1.5.0, using homebridge-hatch-baby-rest@3.3.1, which I installed using homebridge-config-ui-x@4.50.1.

I wasn't sure if this is a problem with this plugin or with homebridge-config-ui-x but I figured I'd ask here first in case you've seen or heard of this behavior before. Let me know if there's any diagnostic information I can provide or any debugging steps you'd suggest -- I'm a software engineer so I know my way around a good bug deep dive ;-).

sonorone commented 2 years ago

I am also having this issue on Big Sur. It seems like the hb-service does not have access to bluetooth. When I run sudo hb-service in terminal it does not prompt for Bluetooth permissions, it does however ask for permissions when running homebridge command. I tried granting access to /Users//.npm/bin/hb-service via System Preferences > Security & Privacy > Privacy > Bluetooth however that is not letting me save the setting. Does anyone know which parent process/deamon should be given access to bluetooth that runs hb-service?

dgreif commented 2 years ago

Sorry y'all are hitting permissions issue. Unfortunately this is very specific to the environment in which you are running homebridge. https://github.com/abandonware/noble#installation is the best set of docs that I know of for getting set up properly, but I'm not sure it goes into much detail on user permissions. Please post back here if you figure it out!

sonorone commented 2 years ago

I was able to find a workaround by granting /usr/local/bin/node access to permissions

image

I can see now it looks good in the logs:

" [8/21/2022, 11:49:59 PM] [HatchBabyRest] Scanning for Hatch Rest [8/21/2022, 11:49:59 PM] [HatchBabyRest] Found device Hatch Rest with address --**-1a-23-02 "

I hope this helps you too

FYI - not a good security practice...

dgreif commented 2 years ago

@dancavallaro I haven't heard back from you on whether you got this working. I'm guessing the steps above should work for you as well

dancavallaro commented 2 years ago

Hey @dgreif, sorry for the silence, I just got back from vacation. @sonorone's workaround did indeed work for me. I first tried giving Bluetooth permissions just to homebridge in my Node bin directory and that didn't work, but when I granted bluetooth permissions to node that did finally work.

FWIW, I did follow https://github.com/abandonware/noble#installation and it does mention giving Bluetooth permissions to Terminal, but that doesn't seem to be sufficient. Maybe that's what allows homebridge to work correctly when started manually from the terminal, but hb-service seems to need blanket permissions for Node for some reason.

Anyway, thanks for the help!