add new sensor for ip6 addresses (#4202)

* add new sensor (draft 1)

* run ktlintcheck

* add docs link and update sensor params

* fix loose ends

* fix toString not nested correctly

* fix unit of measurement

* fix not adding to list

* fix typo that allowed ipv4 addresses to slip in
This commit is contained in:
Zee Aslam 2024-02-22 17:02:13 -05:00 committed by GitHub
parent c69b725fd2
commit f006127b01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 1 deletions

View file

@ -19,6 +19,7 @@ import io.homeassistant.companion.android.database.AppDatabase
import io.homeassistant.companion.android.database.sensor.SensorSetting
import io.homeassistant.companion.android.database.sensor.SensorSettingType
import java.lang.reflect.Method
import java.net.Inet6Address
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
@ -119,6 +120,17 @@ class NetworkSensorManager : SensorManager {
docsLink = "https://companion.home-assistant.io/docs/core/sensors#public-ip-sensor",
entityCategory = SensorManager.ENTITY_CATEGORY_DIAGNOSTIC
)
val ip6Addresses = SensorManager.BasicSensor(
"ip6_addresses",
"sensor",
commonR.string.basic_sensor_name_ip6_addresses,
commonR.string.sensor_description_ip6_addresses,
"mdi:ip",
unitOfMeasurement = "address(es)",
stateClass = SensorManager.STATE_CLASS_MEASUREMENT,
entityCategory = SensorManager.ENTITY_CATEGORY_DIAGNOSTIC,
updateType = SensorManager.BasicSensor.UpdateType.INTENT
)
val networkType = SensorManager.BasicSensor(
"network_type",
"sensor",
@ -158,7 +170,7 @@ class NetworkSensorManager : SensorManager {
listOf(publicIp)
}
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
list.plus(networkType)
list.plus(networkType).plus(ip6Addresses)
} else {
list
}
@ -195,6 +207,7 @@ class NetworkSensorManager : SensorManager {
updatePublicIpSensor(context)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
updateNetworkType(context)
updateIP6Sensor(context)
}
}
@ -356,6 +369,37 @@ class NetworkSensorManager : SensorManager {
)
}
@RequiresApi(Build.VERSION_CODES.M)
private fun updateIP6Sensor(context: Context) {
if (!isEnabled(context, ip6Addresses)) {
return
}
var ipAddressList: List<String> = ArrayList()
var totalAddresses = 0
if (checkPermission(context, ip6Addresses.id)) {
val connectivityManager = context.applicationContext.getSystemService<ConnectivityManager>()
val activeNetwork = connectivityManager?.activeNetwork
val ipAddresses = connectivityManager?.getLinkProperties(activeNetwork)?.linkAddresses
if (!ipAddresses.isNullOrEmpty()) {
val ip6Addresses = ipAddresses.filter { linkAddress -> linkAddress.address is Inet6Address }
if (ip6Addresses.isNotEmpty()) {
ipAddressList = ipAddressList.plus(elements = ip6Addresses.map { linkAddress -> linkAddress.toString() })
totalAddresses += ip6Addresses.size
}
}
}
onSensorUpdated(
context,
ip6Addresses,
totalAddresses,
ip6Addresses.statelessIcon,
mapOf(
"ip6_addresses" to ipAddressList
)
)
}
private fun updateWifiLinkSpeedSensor(context: Context) {
if (!isEnabled(context, wifiLinkSpeed) || !hasWifi(context)) {
return

View file

@ -82,6 +82,7 @@
<string name="basic_sensor_name_high_accuracy_mode">High accuracy mode</string>
<string name="basic_sensor_name_interactive">Interactive</string>
<string name="basic_sensor_name_internal_storage">Internal storage</string>
<string name="basic_sensor_name_ip6_addresses">IPv6 addresses</string>
<string name="basic_sensor_name_keyguard_locked">Keyguard locked</string>
<string name="basic_sensor_name_keyguard_secure">Keyguard secure</string>
<string name="basic_sensor_name_last_notification">Last notification</string>
@ -598,6 +599,7 @@
<string name="sensor_description_hotspot">Whether the WIFI hotspot is enabled</string>
<string name="sensor_description_interactive">Whether the device is in an interactive state</string>
<string name="sensor_description_internal_storage">Information about the total and available storage space internally</string>
<string name="sensor_description_ip6_addresses">The IPv6 addresses bound to the currently active network interface</string>
<string name="sensor_description_keyguard_locked">Whether the keyguard is currently locked</string>
<string name="sensor_description_keyguard_secure">Whether the keyguard is secured by a PIN, pattern or password or a SIM card is currently locked</string>
<string name="sensor_description_last_notification">The details of the last notification. You must set up an allow list or explicitly allow all notifications to be sent.\n\nNote: Sending all notification data will result in heavy battery usage.</string>