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

add new sensor for ip6 addresses #4202

Merged
merged 8 commits into from Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -195,6 +207,7 @@ class NetworkSensorManager : SensorManager {
updatePublicIpSensor(context)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
updateNetworkType(context)
updateIP6Sensor(context)
}
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/res/values/strings.xml
Expand Up @@ -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>
Expand Down Expand Up @@ -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 setup an allow list or explicitly allow all notifications to be sent.\n\nNote: Sending all notification data will result in heavy battery usage.</string>
Expand Down