Skip to content

Commit

Permalink
Improve Battery-Powered detection
Browse files Browse the repository at this point in the history
... to address more real-world devices
  • Loading branch information
Apollon77 committed Jan 24, 2025
1 parent 601c51b commit fbd5d30
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ The main work (all changes without a GitHub username in brackets in the below li
## __WORK IN PROGRESS__
-->

## __WORK IN PROGRESS__

- @project-chip/matter.js
- Fix: Allows more cases when checking if a device is battery powered to address real world devices

## 0.12.0 (2025-01-23)

- @matter/general
Expand Down
39 changes: 24 additions & 15 deletions packages/matter.js/src/device/DeviceInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,36 @@ export class DeviceInformation {
const networks = await networkCluster.getNetworksAttribute();
if (networks) {
if (networks.some(network => network.connected)) {
const features = await networkCluster.getFeatureMapAttribute();
if (features) {
if (features.ethernetNetworkInterface) {
deviceData.ethernetConnected = true;
} else if (features.wiFiNetworkInterface) {
deviceData.wifiConnected = true;
} else if (features.threadNetworkInterface) {
deviceData.threadConnected = true;
}
const features = networkCluster.supportedFeatures;
if (features.ethernetNetworkInterface) {
deviceData.ethernetConnected = true;
} else if (features.wiFiNetworkInterface) {
deviceData.wifiConnected = true;
} else if (features.threadNetworkInterface) {
deviceData.threadConnected = true;
}
}
}
}
}

const powerSourceCluster = endpoint.getClusterClient(PowerSource.Cluster);
if (powerSourceCluster !== undefined) {
if ((await powerSourceCluster.getStatusAttribute()) === PowerSource.PowerSourceStatus.Active) {
const features = await powerSourceCluster.getFeatureMapAttribute();
if (features?.battery) {
deviceData.isBatteryPowered = true;
if (!deviceData.isBatteryPowered) {
// Only query if PowerSource with Battery not already found
const powerSourceCluster = endpoint.getClusterClient(PowerSource.Cluster);
if (powerSourceCluster !== undefined) {
const features = powerSourceCluster.supportedFeatures;
if (
features.battery ||
!features.wired ||
powerSourceCluster.isAttributeSupportedByName("batChargeLevel") // We saw devices with wrong features
) {
const status = await powerSourceCluster.getStatusAttribute();
if (
status === PowerSource.PowerSourceStatus.Active ||
status === PowerSource.PowerSourceStatus.Unspecified
) {
deviceData.isBatteryPowered = true;
}
}
}
}
Expand Down

0 comments on commit fbd5d30

Please sign in to comment.