Add a screen off timeout sensor and command for control (#2960)

This commit is contained in:
Daniel Shokouhi 2022-10-19 11:50:25 -07:00 committed by GitHub
parent 47e8957a92
commit 865c911cb0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 22 deletions

View file

@ -171,6 +171,7 @@ class MessagingManager @Inject constructor(
const val COMMAND_STOP_TTS = "command_stop_tts"
const val COMMAND_AUTO_SCREEN_BRIGHTNESS = "command_auto_screen_brightness"
const val COMMAND_SCREEN_BRIGHTNESS_LEVEL = "command_screen_brightness_level"
const val COMMAND_SCREEN_OFF_TIMEOUT = "command_screen_off_timeout"
// DND commands
const val DND_PRIORITY_ONLY = "priority_only"
@ -257,7 +258,8 @@ class MessagingManager @Inject constructor(
COMMAND_PERSISTENT_CONNECTION,
COMMAND_STOP_TTS,
COMMAND_AUTO_SCREEN_BRIGHTNESS,
COMMAND_SCREEN_BRIGHTNESS_LEVEL
COMMAND_SCREEN_BRIGHTNESS_LEVEL,
COMMAND_SCREEN_OFF_TIMEOUT
)
val DND_COMMANDS = listOf(DND_ALARMS_ONLY, DND_ALL, DND_NONE, DND_PRIORITY_ONLY)
val RM_COMMANDS = listOf(RM_NORMAL, RM_SILENT, RM_VIBRATE)
@ -583,7 +585,7 @@ class MessagingManager @Inject constructor(
sendNotification(jsonData)
}
}
COMMAND_SCREEN_BRIGHTNESS_LEVEL -> {
COMMAND_SCREEN_BRIGHTNESS_LEVEL, COMMAND_SCREEN_OFF_TIMEOUT -> {
if (!jsonData[COMMAND].isNullOrEmpty() && jsonData[COMMAND]?.toIntOrNull() != null)
handleDeviceCommands(jsonData)
else
@ -963,14 +965,14 @@ class MessagingManager @Inject constructor(
COMMAND_STOP_TTS -> {
stopTTS()
}
COMMAND_AUTO_SCREEN_BRIGHTNESS, COMMAND_SCREEN_BRIGHTNESS_LEVEL -> {
COMMAND_AUTO_SCREEN_BRIGHTNESS, COMMAND_SCREEN_BRIGHTNESS_LEVEL, COMMAND_SCREEN_OFF_TIMEOUT -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.System.canWrite(context)) {
if (!processScreenBrightness(data))
if (!processScreenCommands(data))
mainScope.launch { sendNotification(data) }
} else
notifyMissingPermission(data[MESSAGE].toString())
} else if (!processScreenBrightness(data))
} else if (!processScreenCommands(data))
mainScope.launch { sendNotification(data) }
}
else -> Log.d(TAG, "No command received")
@ -2108,22 +2110,25 @@ class MessagingManager @Inject constructor(
WebsocketManager.start(context)
}
private fun processScreenBrightness(data: Map<String, String>): Boolean {
private fun processScreenCommands(data: Map<String, String>): Boolean {
val command = data[COMMAND]
val contentResolver = context.contentResolver
val success = Settings.System.putInt(
contentResolver,
if (data[MESSAGE].toString() == COMMAND_SCREEN_BRIGHTNESS_LEVEL)
Settings.System.SCREEN_BRIGHTNESS
else
Settings.System.SCREEN_BRIGHTNESS_MODE,
if (data[MESSAGE].toString() == COMMAND_SCREEN_BRIGHTNESS_LEVEL)
command!!.toInt().coerceIn(0, 255)
else {
if (command == TURN_ON)
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
else
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
when (data[MESSAGE].toString()) {
COMMAND_SCREEN_BRIGHTNESS_LEVEL -> Settings.System.SCREEN_BRIGHTNESS
COMMAND_AUTO_SCREEN_BRIGHTNESS -> Settings.System.SCREEN_BRIGHTNESS_MODE
else -> Settings.System.SCREEN_OFF_TIMEOUT
},
when (data[MESSAGE].toString()) {
COMMAND_SCREEN_BRIGHTNESS_LEVEL -> command!!.toInt().coerceIn(0, 255)
COMMAND_AUTO_SCREEN_BRIGHTNESS -> {
if (command == TURN_ON)
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
else
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
}
else -> command!!.toInt()
}
)
return success

View file

@ -15,12 +15,18 @@ class DisplaySensorManager : SensorManager {
"sensor",
commonR.string.basic_sensor_name_screen_brightness,
commonR.string.sensor_description_screen_brightness,
statelessIcon = "mdi:brightness-6"
statelessIcon = "mdi:brightness-6",
docsLink = "https://companion.home-assistant.io/docs/core/sensors#screen-brightness-sensor"
)
}
override fun docsLink(): String {
return "https://companion.home-assistant.io/docs/core/sensors#screen-brightness-sensor"
val screenOffTimeout = SensorManager.BasicSensor(
"screen_off_timeout",
"sensor",
commonR.string.sensor_name_screen_off_timeout,
commonR.string.sensor_description_screen_off_timeout,
"mdi:cellphone-off",
docsLink = "https://companion.home-assistant.io/docs/core/sensors#screen-off-timeout-sensor"
)
}
override val enabledByDefault: Boolean
@ -29,7 +35,7 @@ class DisplaySensorManager : SensorManager {
get() = commonR.string.sensor_name_display_sensors
override fun getAvailableSensors(context: Context): List<SensorManager.BasicSensor> {
return listOf(screenBrightness)
return listOf(screenBrightness, screenOffTimeout)
}
override fun requiredPermissions(sensorId: String): Array<String> {
@ -40,6 +46,7 @@ class DisplaySensorManager : SensorManager {
context: Context
) {
updateScreenBrightness(context)
updateScreenTimeout(context)
}
private fun updateScreenBrightness(context: Context) {
@ -71,4 +78,26 @@ class DisplaySensorManager : SensorManager {
)
)
}
private fun updateScreenTimeout(context: Context) {
if (!isEnabled(context, screenOffTimeout.id))
return
var timeout = 0
try {
timeout =
Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_OFF_TIMEOUT)
} catch (e: Exception) {
Log.e(TAG, "Unable to get screen off timeout setting", e)
}
onSensorUpdated(
context,
screenOffTimeout,
timeout,
screenOffTimeout.statelessIcon,
mapOf()
)
}
}

View file

@ -929,4 +929,6 @@
<string name="basic_sensor_name_screen_brightness">Screen Brightness</string>
<string name="sensor_description_screen_brightness">The current screen brightness on the device, an attribute also indicates if auto-brightness is enabled</string>
<string name="sensor_name_display_sensors">Display Sensors</string>
<string name="sensor_name_screen_off_timeout">Screen Off Timeout</string>
<string name="sensor_description_screen_off_timeout">The current duration of idle time before the screen turns off in milliseconds</string>
</resources>