Add do not disturb sensor (#813)

* Initial commit of DND sensor

* Update sensor name

* Adapt to new sensor model

* Review comments
This commit is contained in:
Daniel Shokouhi 2020-08-25 20:04:25 -07:00 committed by GitHub
parent a958b0f95c
commit 9cae26b771
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package io.homeassistant.companion.android
import android.app.Application
import android.app.NotificationManager
import android.bluetooth.BluetoothAdapter
import android.content.Intent
import android.content.IntentFilter
@ -77,6 +78,14 @@ open class HomeAssistantApplication : Application(), GraphComponentAccessor {
}
)
}
// Add receiver for DND changes on devices that support it
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
registerReceiver(
sensorReceiver,
IntentFilter(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED)
)
}
}
override val appComponent: AppComponent

View file

@ -0,0 +1,58 @@
package io.homeassistant.companion.android.sensors
import android.content.Context
import android.provider.Settings.Global
import android.util.Log
class DNDSensorManager : SensorManager {
companion object {
private const val TAG = "DNDSensor"
private val dndSensor = SensorManager.BasicSensor(
"dnd_sensor",
"sensor",
"Do Not Disturb Sensor"
)
}
override val name: String
get() = "Do Not Disturb Sensor"
override val availableSensors: List<SensorManager.BasicSensor>
get() = listOf(dndSensor)
override fun requiredPermissions(): Array<String> {
return emptyArray()
}
override fun requestSensorUpdate(context: Context) {
updateDNDState(context)
}
private fun updateDNDState(context: Context) {
if (!isEnabled(context, dndSensor.id))
return
var dndState = "unavailable"
try {
dndState = when (Global.getInt(context.contentResolver, "zen_mode")) {
0 -> "off"
1 -> "priority_only"
2 -> "total_silence"
3 -> "alarms_only"
else -> "unknown"
}
val icon = "mdi:do-not-disturb"
onSensorUpdated(context,
dndSensor,
dndState,
icon,
mapOf()
)
} catch (e: Exception) {
Log.e(TAG, "Error getting the devices DND mode", e)
}
}
}

View file

@ -24,6 +24,7 @@ class SensorReceiver : BroadcastReceiver() {
AudioSensorManager(),
BatterySensorManager(),
BluetoothSensorManager(),
DNDSensorManager(),
GeocodeSensorManager(),
LastRebootSensorManager(),
LightSensorManager(),