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

Fix Active Notification Count sensor showing 0 notifications when upd… #4525

Merged

Conversation

marazmarci
Copy link
Contributor

Summary

I noticed that after my previous changes (#4516) were merged, this error log showed up in Logcat:

SensorReceiver  io.homeassistant.companion.android   E  Issue requesting updates for Notification sensors
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:154)
    at io.homeassistant.companion.android.sensors.NotificationSensorManager.updateActiveNotificationCount(NotificationSensorManager.kt:236)
    at io.homeassistant.companion.android.sensors.NotificationSensorManager.requestSensorUpdate(NotificationSensorManager.kt:102)
    at io.homeassistant.companion.android.common.sensors.SensorManager$DefaultImpls.requestSensorUpdate(SensorManager.kt:138)
    at io.homeassistant.companion.android.sensors.NotificationSensorManager.requestSensorUpdate(NotificationSensorManager.kt:25)
    at io.homeassistant.companion.android.common.sensors.SensorReceiverBase.updateSensors(SensorReceiverBase.kt:184)
    at io.homeassistant.companion.android.common.sensors.SensorWorkerBase$doWork$2.invokeSuspend(SensorWorkerBase.kt:67)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
    at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:111)
    at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:99)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:811)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:715)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:702)

And also, the Active Notification Count sensor always shows 0 notifications when the sensor update was triggered through requestSensorUpdate(). I noticed that there are two instances created from NotificationSensorManager: one which is instantiated here from the code, and the other is instantiated by the Android framework because it is a Service. The problematic case was when it was instantiated from the code because that instance was added to SensorReceiver.MANAGERS, and requestSensorUpdate() is usually called on instances from that list.

Related discussion from the previous PR.

I added the updateActiveNotificationCount() call to make the newly added setting appear earlier. A long-term solution for this could be this refactor idea. (Also, I think it would be good not to have multiple instances of a single SensorManager. Every SensorManager that inherits from BroadcastReceiver or Service (and thus instantiated by Android) is prone to errors like this.)

Link to pull request in Documentation repository

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

Any other notes

…ate was triggered through requestSensorUpdate()
@dshokouhi dshokouhi merged commit 7e6baba into home-assistant:master Jul 22, 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