Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notification command for turning the flashlight on/off #4766

Merged

Conversation

marazmarci
Copy link
Contributor

@marazmarci marazmarci commented Oct 30, 2024

Summary

I added the command_flashlight notification command. It accepts two values in the command field: turn_on or turn_off.

Currently, my implementation only supports API 23 and above, while the app has minSdkVersion = 21. The main reason is my laziness lack of time, and because the Camera2 API is available starting from API 23, and I haven't found a trivial ...Compat solution. If someone mentions needing this on API 21-22, it can be implemented as a separate feature request. Please let me know if this approach is OK for you.

The PR is marked as a draft because I still have to implement requesting the Camera permission, but I first wanted to ask for your advice on it. The problem is that in MessagingManager only so-called "special permissions" are being requested, and there are no runtime permissions, and it would require an Activity. I thought of adding another registerForActivityResult(ActivityResultContracts.RequestPermission()) in BaseActivity and making BaseActivity subscribe to an event-like Flow from a singleton class, which would trigger a permission request, and MessagingManager could trigger this event. WDYT?

Related feature request: #1606

Screenshots

N/A

Link to pull request in Documentation repository

Documentation: home-assistant/companion.home-assistant#1131

Any other notes

Future improvement possibilities:

  • Support API 21 and 22
  • Add command: toggle
  • Add a binary_sensor representing the current state of the torch (maybe even a light entity?)
  • Add torch strength control

@dshokouhi
Copy link
Member

dshokouhi commented Oct 30, 2024

The problem is that in MessagingManager there are only so-called "special permissions" being requested, and there are no runtime permissions,

we actually request the phone permission for one specific intent I think you can use the same method. Take user to webview, then they grant the permission and try again.

https://github.com/home-assistant/android/blob/master/app/src/main/java/io/homeassistant/companion/android/notifications/MessagingManager.kt#L689-L697

(maybe even a light entity?)

this would require HA core changes, we dont have any controllable entities and have not discussed about how it should work. Think this probably requires an arch discussion.

@marazmarci
Copy link
Contributor Author

I created a PR in the documentation repository: home-assistant/companion.home-assistant#1131

… the notification command will be posted as a regular notification on < Android M devices
Copy link
Member

@dshokouhi dshokouhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please submit a PR to the mobile app FCM repo adding the new command to the list commands so it can be excluded from the rate limit?

https://github.com/home-assistant/mobile-apps-fcm-push/blob/main/functions/android.js#L70-L74

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

Copy link
Member

@dshokouhi dshokouhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested debug APK and works as expected

@dshokouhi dshokouhi merged commit 53d5ea7 into home-assistant:master Oct 31, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants