From 514532b28208fd9cfc632d79f9a1b65a8231ec95 Mon Sep 17 00:00:00 2001 From: Daniel Shokouhi Date: Tue, 8 Sep 2020 14:52:20 -0700 Subject: [PATCH] Split up WiFi sensor, part 1 (#904) * Split up wifi attributes into their own sensor, part 1 * Add note to remove attributes after next release * Wifi to WiFi for consistency --- .../android/sensors/NetworkSensorManager.kt | 283 +++++++++++++++++- app/src/main/res/values/strings.xml | 17 +- 2 files changed, 291 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/homeassistant/companion/android/sensors/NetworkSensorManager.kt b/app/src/main/java/io/homeassistant/companion/android/sensors/NetworkSensorManager.kt index 4c466f51f..7c1e2a27f 100644 --- a/app/src/main/java/io/homeassistant/companion/android/sensors/NetworkSensorManager.kt +++ b/app/src/main/java/io/homeassistant/companion/android/sensors/NetworkSensorManager.kt @@ -16,6 +16,45 @@ class NetworkSensorManager : SensorManager { R.string.basic_sensor_name_wifi, R.string.sensor_description_wifi_connection ) + val bssidState = SensorManager.BasicSensor( + "wifi_bssid", + "sensor", + R.string.basic_sensor_name_wifi_bssid, + R.string.sensor_description_wifi_bssid + ) + val wifiIp = SensorManager.BasicSensor( + "wifi_ip_address", + "sensor", + R.string.basic_sensor_name_wifi_ip, + R.string.sensor_description_wifi_ip + ) + val wifiLinkSpeed = SensorManager.BasicSensor( + "wifi_link_speed", + "sensor", + R.string.basic_sensor_name_wifi_link_speed, + R.string.sensor_description_wifi_link_speed, + unitOfMeasurement = "Mbps" + ) + val wifiState = SensorManager.BasicSensor( + "wifi_state", + "binary_sensor", + R.string.basic_sensor_name_wifi_state, + R.string.sensor_description_wifi_state + ) + val wifiFrequency = SensorManager.BasicSensor( + "wifi_frequency", + "sensor", + R.string.basic_sensor_name_wifi_frequency, + R.string.sensor_description_wifi_frequency, + unitOfMeasurement = "MHz" + ) + val wifiSignalStrength = SensorManager.BasicSensor( + "wifi_signal_strength", + "sensor", + R.string.basic_sensor_name_wifi_signal, + R.string.sensor_description_wifi_signal, + unitOfMeasurement = "dBm" + ) } override val enabledByDefault: Boolean @@ -23,7 +62,7 @@ class NetworkSensorManager : SensorManager { override val name: Int get() = R.string.sensor_name_network override val availableSensors: List - get() = listOf(wifiConnection) + get() = listOf(wifiConnection, bssidState, wifiIp, wifiLinkSpeed, wifiState, wifiFrequency, wifiSignalStrength) override fun requiredPermissions(): Array { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { @@ -40,6 +79,12 @@ class NetworkSensorManager : SensorManager { context: Context ) { updateWifiConnectionSensor(context) + updateBSSIDSensor(context) + updateWifiIPSensor(context) + updateWifiLinkSpeedSensor(context) + updateWifiSensor(context) + updateWifiFrequencySensor(context) + updateWifiSignalStrengthSensor(context) } private fun updateWifiConnectionSensor(context: Context) { @@ -82,13 +127,13 @@ class NetworkSensorManager : SensorManager { val attributes = conInfo?.let { mapOf( - "bssid" to conInfo.bssid, - "ip_address" to getIpAddress(conInfo.ipAddress), - "link_speed" to conInfo.linkSpeed, + "bssid" to conInfo.bssid, // Remove after next release + "ip_address" to getIpAddress(conInfo.ipAddress), // Remove after next release + "link_speed" to conInfo.linkSpeed, // Remove after next release "is_hidden" to conInfo.hiddenSSID, - "is_wifi_on" to wifiEnabled, - "frequency" to conInfo.frequency, - "signal_level" to lastScanStrength + "is_wifi_on" to wifiEnabled, // Remove after next release + "frequency" to conInfo.frequency, // Remove after next release + "signal_level" to lastScanStrength // Remove after next release ) }.orEmpty() @@ -100,6 +145,230 @@ class NetworkSensorManager : SensorManager { ) } + private fun updateBSSIDSensor(context: Context) { + if (!isEnabled(context, bssidState.id)) + return + + var conInfo: WifiInfo? = null + var lastScanStrength = -1 + + if (checkPermission(context)) { + val wifiManager = + (context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager) + conInfo = wifiManager.connectionInfo + + lastScanStrength = wifiManager.scanResults.firstOrNull { + it.BSSID == conInfo.bssid + }?.level ?: -1 + } + + var signalStrength = -1 + if (lastScanStrength != -1) { + signalStrength = WifiManager.calculateSignalLevel(lastScanStrength, 4) + } + + val icon = "mdi:wifi-strength-" + when (signalStrength) { + -1 -> "off" + 0 -> "outline" + else -> signalStrength + } + + val bssid = if (conInfo!!.bssid == null) "" else conInfo.bssid + onSensorUpdated(context, + bssidState, + bssid, + icon, + mapOf() + ) + } + + private fun updateWifiIPSensor(context: Context) { + if (!isEnabled(context, wifiIp.id)) + return + + var conInfo: WifiInfo? = null + var deviceIp = "Unknown" + var lastScanStrength = -1 + + if (checkPermission(context)) { + val wifiManager = + (context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager) + conInfo = wifiManager.connectionInfo + + deviceIp = if (conInfo.networkId == -1) { + "" + } else { + getIpAddress(conInfo.ipAddress) + } + + lastScanStrength = wifiManager.scanResults.firstOrNull { + it.BSSID == conInfo.bssid + }?.level ?: -1 + } + + var signalStrength = -1 + if (lastScanStrength != -1) { + signalStrength = WifiManager.calculateSignalLevel(lastScanStrength, 4) + } + + val icon = "mdi:wifi-strength-" + when (signalStrength) { + -1 -> "off" + 0 -> "outline" + else -> signalStrength + } + + onSensorUpdated(context, + wifiIp, + deviceIp, + icon, + mapOf() + ) + } + + private fun updateWifiLinkSpeedSensor(context: Context) { + if (!isEnabled(context, wifiLinkSpeed.id)) + return + + var conInfo: WifiInfo? = null + var linkSpeed = 0 + var lastScanStrength = -1 + + if (checkPermission(context)) { + val wifiManager = + (context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager) + conInfo = wifiManager.connectionInfo + + linkSpeed = if (conInfo.networkId == -1) { + 0 + } else { + conInfo.linkSpeed + } + + lastScanStrength = wifiManager.scanResults.firstOrNull { + it.BSSID == conInfo.bssid + }?.level ?: -1 + } + + var signalStrength = -1 + if (lastScanStrength != -1) { + signalStrength = WifiManager.calculateSignalLevel(lastScanStrength, 4) + } + + val icon = "mdi:wifi-strength-" + when (signalStrength) { + -1 -> "off" + 0 -> "outline" + else -> signalStrength + } + + onSensorUpdated(context, + wifiLinkSpeed, + linkSpeed, + icon, + mapOf() + ) + } + + private fun updateWifiSensor(context: Context) { + if (!isEnabled(context, wifiState.id)) + return + + var wifiEnabled = false + + if (checkPermission(context)) { + val wifiManager = + (context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager) + + wifiEnabled = wifiManager.isWifiEnabled + } + val icon = if (wifiEnabled) "mdi:wifi" else "mdi:wifi-off" + + onSensorUpdated(context, + wifiState, + wifiEnabled, + icon, + mapOf() + ) + } + + private fun updateWifiFrequencySensor(context: Context) { + if (!isEnabled(context, wifiFrequency.id)) + return + + var conInfo: WifiInfo? = null + var frequency = 0 + var lastScanStrength = -1 + + if (checkPermission(context)) { + val wifiManager = + (context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager) + conInfo = wifiManager.connectionInfo + + frequency = if (conInfo.networkId == -1) { + 0 + } else { + conInfo.frequency + } + + lastScanStrength = wifiManager.scanResults.firstOrNull { + it.BSSID == conInfo.bssid + }?.level ?: -1 + } + + var signalStrength = -1 + if (lastScanStrength != -1) { + signalStrength = WifiManager.calculateSignalLevel(lastScanStrength, 4) + } + + val icon = "mdi:wifi-strength-" + when (signalStrength) { + -1 -> "off" + 0 -> "outline" + else -> signalStrength + } + + onSensorUpdated(context, + wifiFrequency, + frequency, + icon, + mapOf() + ) + } + + private fun updateWifiSignalStrengthSensor(context: Context) { + if (!isEnabled(context, wifiSignalStrength.id)) + return + + var conInfo: WifiInfo? = null + var lastScanStrength = -1 + + if (checkPermission(context)) { + val wifiManager = + (context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager) + conInfo = wifiManager.connectionInfo + + lastScanStrength = wifiManager.scanResults.firstOrNull { + it.BSSID == conInfo.bssid + }?.level ?: -1 + } + + var signalStrength = -1 + if (lastScanStrength != -1) { + signalStrength = WifiManager.calculateSignalLevel(lastScanStrength, 4) + } + + val icon = "mdi:wifi-strength-" + when (signalStrength) { + -1 -> "off" + 0 -> "outline" + else -> signalStrength + } + + onSensorUpdated(context, + wifiSignalStrength, + lastScanStrength, + icon, + mapOf() + ) + } + private fun getIpAddress(ip: Int): String { return (ip and 0xFF).toString() + "." + (ip shr 8 and 0xFF) + "." + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e8f0eb733..7d901af82 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -175,7 +175,13 @@ like to connect to: Battery State Bluetooth Connection Last Reboot - Wifi Connection + WiFi Connection + WiFi BSSID + WiFi IP Address + WiFi Link Speed + WiFi State + WiFi Frequency + WiFi Signal Strength Next Alarm Phone State SIM 1 @@ -208,7 +214,14 @@ like to connect to: The total number of steps since the last reboot of the device Information about the total and available storage space internally Information about the total and available storage space externally - Information about the currently connected WiFi network + Information about the total and available storage space internally and externally + The name of the network the device is currently connected to + The mac address of the currently connected WiFi access point + The current IP address of the device on the network + The current link speed of the device to the connected network + Whether or not WiFi is enabled on the device + The frequency band of the connected network + The signal strength of the device to the WiFi network Use this to manage what sensors are enabled/disabled. Manage Sensors Sensor