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 Aqara FP1E presence sensor v2 quirk #3521

Merged
merged 18 commits into from
Nov 26, 2024

Conversation

jeverley
Copy link
Contributor

@jeverley jeverley commented Nov 18, 2024

Proposed change

This quirk adds support for the Aqara FP1E presence sensor, exposing its motion and occupancy sensors in addition to its configuration parameters.

Fixes #3294 and is written using the quirks v2 format.

image

Additional information

There are two issues that we'd ideally want to fix before merging this PR.

Checklist

  • The changes are tested and work correctly
  • pre-commit checks pass / the code has been formatted using Black
  • Tests have been added to verify that the new code works

Copy link

codecov bot commented Nov 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.71%. Comparing base (213ce10) to head (fce8f13).
Report is 1 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #3521      +/-   ##
==========================================
+ Coverage   89.67%   89.71%   +0.04%     
==========================================
  Files         316      317       +1     
  Lines       10281    10324      +43     
==========================================
+ Hits         9219     9262      +43     
  Misses       1062     1062              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jeverley
Copy link
Contributor Author

Hi @dmulcahey, do you have any feedback on the current implementation with regards to quirk v2 conventions?

@TheJulianJES TheJulianJES added Xiaomi Request/PR regarding a Xiaomi device v2 quirk Quirks using v2 API. Might add custom entities that need translation keys in HA. needs review This PR should be reviewed soon, as it generally looks good. labels Nov 24, 2024
Comment on lines 125 to 132
.adds(DeviceTemperature)
.adds(OccupancySensing)
.adds(
IasZone,
constant_attributes={
IasZone.AttributeDefs.zone_type: IasZone.ZoneType.Motion_Sensor
},
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If these clusters are completely virtual/fake (not present on the device), we'd probably want them to be a LocalDataCluster. E.g.:

class IasZoneLocal(LocalDataCluster, IasZone):
    """Virtual cluster for IasZone."""

    # required to make sure ZHA creates sensors when initially pairing
    _VALID_ATTRIBUTES = {IasZone.AttributeDefs.zone_status.id}

_VALID_ATTRIBUTES is probably required to make sure that ZHA creates the sensors when the device is initially paired (so completely removed from ZHA before pairing again), even when there's no initial data in the zigpy DB (which there is not, as the attribute cannot be read on pairing).

Copy link
Collaborator

Choose a reason for hiding this comment

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

It might be a good idea for us to provide a shorthand v2 quirk method for this. But that does't exist yet..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It might be a good idea for us to provide a shorthand v2 quirk method for this. But that does't exist yet..

Yes I think that would be a great addition, in the meantime I've updated the quirk to use Local clusters for motion and occupancy.

Move attribute IDs into the AttributeDefs and ensure that OccupancySensing and IasZone are correctly handled as local virtual clusters.
UnitOfLength.METERS for distance entities (zigpy/zha#307)

EntityType.DIAGNOSTIC for restart device button (zigpy/zha#308)
@TheJulianJES TheJulianJES changed the title Add v2 format quirk for Aqara FP1E presence sensor Add Aqara FP1E presence sensor v2 quirk Nov 26, 2024
Copy link
Collaborator

@TheJulianJES TheJulianJES left a comment

Choose a reason for hiding this comment

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

This looks very good.
I'll make a small change regarding the constant_attributes. There's more info in the two comments. Otherwise, this should be good to go.

zhaquirks/xiaomi/aqara/motion_agl1.py Outdated Show resolved Hide resolved
zhaquirks/xiaomi/aqara/motion_agl1.py Show resolved Hide resolved
@TheJulianJES
Copy link
Collaborator

TheJulianJES commented Nov 26, 2024

I've also added a small test to cover the _update_attribute method: f0d0754

Copy link
Collaborator

@TheJulianJES TheJulianJES left a comment

Choose a reason for hiding this comment

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

This should be good to go IMO. Thanks, @jeverley!

@TheJulianJES TheJulianJES added ready PR should be ready to merge and removed needs review This PR should be reviewed soon, as it generally looks good. labels Nov 26, 2024
@jeverley
Copy link
Contributor Author

jeverley commented Nov 26, 2024

I've also added a small test to cover the _update_attribute method: f0d0754

Thanks for adding these, I was still wrapping my head around how best to introduce a v2 quirk amongst the other xiaomi motion devices.
I think we need to make a small tweak to the expected statuses for motion in here, possible values being:

class AqaraMotion(types.enum8):
    """Aqara motion attribute values."""

    Idle = 0x02 # no presence
    Moving = 0x03 # present and moving
    Still = 0x04 # present and not moving

Therefore the parameters should be (if I'm understanding the test syntax correctly):

@pytest.mark.parametrize(
    "occupancy_value, expected_occ_status, motion_value, expected_motion_status",
    [
        (0, OccupancySensing.Occupancy.Unoccupied, 2, 0),
        (1, OccupancySensing.Occupancy.Occupied, 3, IasZone.ZoneStatus.Alarm_1),
        (1, OccupancySensing.Occupancy.Occupied, 4, 0),
    ],
)

Will add a commit reflecting this shortly.

Edit: I see you already corrected the flipped occupied/unoccupied values in 45c49fc, still good to capture the 3rd scenario of occupied with no movement.

Comment on lines +47 to +53
class IasZoneLocal(LocalDataCluster, IasZone):
"""Virtual cluster for IasZone."""

_CONSTANT_ATTRIBUTES = {
IasZone.AttributeDefs.zone_type.id: IasZone.ZoneType.Motion_Sensor
}
_VALID_ATTRIBUTES = {IasZone.AttributeDefs.zone_status.id}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note for other reviewers: We need a virtual cluster for IasZone here, as we need a virtual attribute for "motion". The Xiaomi motion attribute needs to be parsed in a way that we can't yet do with quirks v2.

Also, for motion/occupancy, it makes sense to use the integrated entities in ZHA IMO. We'd have to duplicate "motion" and "occupancy" quirk entities with all properties everywhere otherwise.

We already use virtual clusters in a lot of quirks and they'll always be needed for some (especially for Tuya and some Xiaomi devices), unless we have some kind of other structure we can map attributes to (like Z2M does).

@TheJulianJES
Copy link
Collaborator

TheJulianJES commented Nov 26, 2024

Looks like Codecov is broken across all repos again. This PR is fully covered.
Also noting that this quirk will require the latest ZHA version. You can't install it on a HA Core 2024.11.x system (or earlier) anymore.

@TheJulianJES TheJulianJES merged commit 3acf032 into zigpy:dev Nov 26, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready PR should be ready to merge smash This PR is close to be merged soon v2 quirk Quirks using v2 API. Might add custom entities that need translation keys in HA. Xiaomi Request/PR regarding a Xiaomi device
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Device Support Request] Aqara FP1e (lumi.sensor_occupy.agl1)
2 participants