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

fix: endpoint device & group members typing #1330

Merged
merged 2 commits into from
Mar 6, 2025
Merged

Conversation

Nerivec
Copy link
Collaborator

@Nerivec Nerivec commented Mar 2, 2025

  • endpoint.getDevice() could potentially be undefined => log error/stack trace
  • group._members required array conversion for use in some places => use array as base instead (with check on add)

@Koenkk I'm a little worried that this didn't trip coverage, smells like hidden coverage issues, particularly for Group.
The members getter for Group is a little awkward. It also results in lots of chained calls because more often than not, Z2M will chain the result with some other filters on top. And typing is crappy too, because getDevice shouldn't be undefined for resulting items since that's the whole purpose of the getter. See any easy way to change this?

public getDevice(): Device {
return Device.byIeeeAddr(this.deviceIeeeAddress)!; // XXX: no way for device to not exist?
public getDevice(): Device | undefined {
return Device.byIeeeAddr(this.deviceIeeeAddress);
Copy link
Owner

@Koenkk Koenkk Mar 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we just throw an exception when device === undefined? I'm not sure when exactly this can happen.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are places where undefined was being checked against the return value, even though the type didn't allow it, so, I don't think we can just throw.

return Array.from(this._members).filter((e) => e.getDevice());

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And how can the device get undefined?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be only if deleted

public static byIeeeAddr(ieeeAddr: string, includeDeleted: boolean = false): Device | undefined {
Device.loadFromDatabaseIfNecessary();
return includeDeleted ? (Device.deletedDevices.get(ieeeAddr) ?? Device.devices.get(ieeeAddr)) : Device.devices.get(ieeeAddr);
}

@Nerivec Nerivec marked this pull request as ready for review March 6, 2025 19:14
@Koenkk Koenkk merged commit d668227 into Koenkk:master Mar 6, 2025
1 check passed
@Nerivec Nerivec deleted the fix-typing branch March 6, 2025 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants