Repository: zigpy/zha-device-handlers

Issue:
[Device Support Request] SML003 sensitivity setting
**Is your feature request related to a problem? Please describe.**
I bought an SML003 (hue motion sensor) to replace my Ikea ones because they support setting the sensitivity.
However once paired in ZHA, that's not an exposed setting and unfortunately the default is way too sensitive.

**Describe the solution you'd like**
Add a way to set the sensitivity, Z2M apparently supports it

**Device signature - this can be acquired by removing the device from ZHA and pairing it again from the add devices screen. Be sure to add the entire content of the log panel after pairing the device to a code block below this line.**

```json
{
  "node_descriptor": "NodeDescriptor(logical_type=<LogicalType.EndDevice: 2>, complex_descriptor_available=0, user_descriptor_available=0, reserved=0, aps_flags=0, frequency_band=<FrequencyBand.Freq2400MHz: 8>, mac_capability_flags=<MACCapabilityFlags.AllocateAddress: 128>, manufacturer_code=4107, maximum_buffer_size=82, maximum_incoming_transfer_size=128, server_mask=11264, maximum_outgoing_transfer_size=128, descriptor_capability_field=<DescriptorCapability.NONE: 0>, *allocate_address=True, *is_alternate_pan_coordinator=False, *is_coordinator=False, *is_end_device=True, *is_full_function_device=False, *is_mains_powered=False, *is_receiver_on_when_idle=False, *is_router=False, *is_security_capable=False)",
  "endpoints": {
    "2": {
      "profile_id": 260,
      "device_type": "0x0107",
      "in_clusters": [
        "0x0000",
        "0x0001",
        "0x0003",
        "0x0400",
        "0x0402",
        "0x0406"
      ],
      "out_clusters": [
        "0x0000",
        "0x0003",
        "0x0006",
        "0x0019"
      ]
    }
  },
  "manufacturer": "Signify Netherlands B.V.",
  "model": "SML003",
  "class": "zigpy.device.Device"
}
```

**Additional context**
[This thread](https://community.home-assistant.io/t/zha-phillips-hue-motion/191855/13) seems to suggest the way to do it is by setting ultrasonic_u_to_o_threshold and pir_u_to_o_threshold clusters but that doesn't seem to do much currently. Might be better to look into how Z2M does it.

Thanks



Discussion:
The sensitivity is implanted on the cluster 
https://github.com/zigpy/zha-device-handlers/blob/e4e62504738ced0c54c983ee5094a3a6af0a741d/zhaquirks/philips/__init__.py#L78-L80
But its looks your device is heaving one other Endpoint / cluster setting and the quirk is not being loaded for it.
Can you reading the software version on basic cluster and posting it ?

Also trying deleting it and reading it and look if you is getting one other cluster setting.
Hey,

Sure, not sure which one exactly you want so here goes : 
sw_build_id (0x4000) : 2.53.6
zcl_version (0x0000) : 2
app_version (0x0001) : 2
stack_version (0x0002) : 1
hw_version (0x0003) : 1

Let me know if that's not what you were looking for.
It's a brand new SML003 bought on amazon and received this morning.
Thanks
I only have 2 SML001 and cant comparing the version but perhaps some other user can and can saying if its updated or not.

I think the quirk need being updated with your device signature and the new manufacture name and also need some testing so knowing if its working OK.
Sure. The entities that show up do seem to be working fine as far as I can tell : 

![image](https://user-images.githubusercontent.com/873423/157076992-ac19a7d8-2493-459b-844c-5b7255a4781d.png)

I've bound it directly to a group of lights and it's been turning them on and off fine, and also reporting occupancy and 'on/off' to HA at the same time without a hitch. Luminance and temperature also look about right.

And if you tell me what files I can edit them in my container to test it out
The quirk is in https://github.com/zigpy/zha-device-handlers/blob/dev/zhaquirks/philips/motion.py.
You need downloading and configuring local quirks in HA and adding the quirk in the new folder  https://github.com/zigpy/zha-device-handlers/discussions/693#discussioncomment-857274.
Then you need edeting or bets making one new device class for your new version (for testing editing the old is OK but need one new class for adding it in ZHA).

The old device class is here:
https://github.com/zigpy/zha-device-handlers/blob/e4e62504738ced0c54c983ee5094a3a6af0a741d/zhaquirks/philips/motion.py#L32
The signature must looks 100% as your device for the system shall loading it also the manufacture name and its can being imported from the INIT https://github.com/zigpy/zha-device-handlers/blob/e4e62504738ced0c54c983ee5094a3a6af0a741d/zhaquirks/philips/__init__.py#L36.
The replacement shall looking the same but this line must being other then in the signature for getting the extra attributes on the cluster.

After every change you need restarting HA for the system using the changes.
Add the `SIGNIFY` at the end of this line and its being imported in the quirk:
https://github.com/zigpy/zha-device-handlers/blob/e4e62504738ced0c54c983ee5094a3a6af0a741d/zhaquirks/philips/motion.py#L29 like this:  `from zhaquirks.philips import PHILIPS, OccupancyCluster, SIGNIFY`
And then putting  `SIGNIFY` as manufacture.
I don't really follow.
I created the quirks directory and added the config bit to load it, then downloaded the existing motion.py file into it. In there I renamed the class and added SIGNIFY to the import list.

Then I understand I need to edit the signature, but I don't know what to add or where. Presumably I need to add something to the input clusters in 2 ? If so, what do I need to add ?
I also don't know where I need to put SIGNIFY, I don't see any manufacturer parameter anywhere in that file.

Thanks
Like is being done here:
https://github.com/zigpy/zha-device-handlers/blob/e4e62504738ced0c54c983ee5094a3a6af0a741d/zhaquirks/philips/rwl022.py#L42 (Its one other quirk and wrong device ID you need to fixing that in the motion quirk).

Then all this:
https://github.com/zigpy/zha-device-handlers/blob/e4e62504738ced0c54c983ee5094a3a6af0a741d/zhaquirks/philips/motion.py#L42-L79
Need being as your device i looking.

If that is OK is the system loading the quirk for your device.

Next step is fixing the 
https://github.com/zigpy/zha-device-handlers/blob/e4e62504738ced0c54c983ee5094a3a6af0a741d/zhaquirks/philips/motion.py#L81
section so its using all cluster your device is having.
Okay so I'm not sure that's what you meant, but I did this : 

```python
"""Quirk for Philips motion sensors."""
from zigpy.profiles import zha, zll
from zigpy.quirks import CustomDevice
from zigpy.zcl.clusters.general import (
    Basic,
    Groups,
    Identify,
    LevelControl,
    OnOff,
    Ota,
    PowerConfiguration,
    Scenes,
)
from zigpy.zcl.clusters.lighting import Color
from zigpy.zcl.clusters.measurement import (
    IlluminanceMeasurement,
    OccupancySensing,
    TemperatureMeasurement,
)

from zhaquirks.const import (
    DEVICE_TYPE,
    ENDPOINTS,
    INPUT_CLUSTERS,
    MODELS_INFO,
    OUTPUT_CLUSTERS,
    PROFILE_ID,
)
from zhaquirks.philips import PHILIPS, OccupancyCluster, SIGNIFY


class PhilipsMotionNew(CustomDevice):
    """Philips motion sensor device."""

    signature = {
        MODELS_INFO: [
            (SIGNIFY, "SML003"),
        ],
        ENDPOINTS: {
    "2": {
      "profile_id": 260,
      "device_type": "0x0107",
      "in_clusters": [
        "0x0000",
        "0x0001",
        "0x0003",
        "0x0400",
        "0x0402",
        "0x0406"
      ],
      "out_clusters": [
        "0x0000",
        "0x0003",
        "0x0006",
        "0x0019"
      ]
    }
        },
    }

    replacement = {
        ENDPOINTS: {
            1: {
                PROFILE_ID: zll.PROFILE_ID,
                DEVICE_TYPE: zll.DeviceType.ON_OFF_SENSOR,
                INPUT_CLUSTERS: [Basic.cluster_id],
                OUTPUT_CLUSTERS: [
                    Basic.cluster_id,
                    Identify.cluster_id,
                    Groups.cluster_id,
                    Scenes.cluster_id,
                    OnOff.cluster_id,
                    LevelControl.cluster_id,
                    Color.cluster_id,
                ],
            },
            2: {
                PROFILE_ID: zha.PROFILE_ID,
                DEVICE_TYPE: zha.DeviceType.OCCUPANCY_SENSOR,
                INPUT_CLUSTERS: [
                    Basic.cluster_id,
                    PowerConfiguration.cluster_id,
                    Identify.cluster_id,
                    IlluminanceMeasurement.cluster_id,
                    TemperatureMeasurement.cluster_id,
                    OccupancyCluster,
                ],
                OUTPUT_CLUSTERS: [Ota.cluster_id],
            },
        }
    }
```

Also added this to my configuration.yaml : 
```yaml
zha:
  custom_quirks_path: /config/custom_zha_quirks/
```

Restarted HA and no change, doesn't look like it loaded that quirk.
Do I need to forget the device and re-pair for it to take effect or should restart have done it ? TBH the fact that I didn't get a syntax error with the above file makes me think it didn't get loaded at all.
You dont need repairing the device only restarting HA.
If you is getting on the device card or device signature   `"class": "zigpy.device.Device"` = the system was not  loading the quirk for the device = Signature is not OK.

If you is making some _bad things_ in the quirk it can resulting ZHA is not starting.
HA is starting, and no errors in the log.
I'm using the file I posted above so I'm not sure what else to do, it's a copy paste of the signature as you can see
Just noticed that my coordinator firmware was outdated (slightly newer zstack available) so I did the upgrade to 20211217 just now to be sure but no luck, that didn't change anything.
I think (= not knowing) using the cluster number shall being OK in the signature but try using the real names if some its not OK defined like this shall being all your cluster with names:
```
                INPUT_CLUSTERS: [
                    Basic.cluster_id,
                    PowerConfiguration.cluster_id,
                    Identify.cluster_id,
                    IlluminanceMeasurement.cluster_id,
                    TemperatureMeasurement.cluster_id,
                    OccupancySensing.cluster_id,
                ],
                OUTPUT_CLUSTERS: [
                    Basic.cluster_id,
                    Identify.cluster_id,
                    OnOff.cluster_id,
                    Ota.cluster_id,
                ],
```
If you is setting up loging for quirks in HA you can see if the system is loading your new quirk (its loading the local after the internal ones in the log) in the logfiles then HA is starting by setting this in HA log setting:
```
    zhaquirks: debug
    custom_zha_quirks: debug
```
Changing the `DEVICE_TYPE: zha.DeviceType.OCCUPANCY_SENSOR,` in the signature.
And the `PROFILE_ID: zll.PROFILE_ID,`
Sorry the last was wrong shall being `PROFILE_ID: zha.PROFILE_ID,`
I have fixing some commas that was little strange but its looks like this (without replacement):
```
class PhilipsMotionNew(CustomDevice):
    """Philips motion sensor device."""

    signature = {
        MODELS_INFO: [(SIGNIFY, "SML003")],
        ENDPOINTS: {
            2: {
                PROFILE_ID: zha.PROFILE_ID,
                DEVICE_TYPE: zha.DeviceType.OCCUPANCY_SENSOR,
                INPUT_CLUSTERS: [
                    Basic.cluster_id,
                    PowerConfiguration.cluster_id,
                    Identify.cluster_id,
                    IlluminanceMeasurement.cluster_id,
                    TemperatureMeasurement.cluster_id,
                    OccupancySensing.cluster_id,
                ],
                OUTPUT_CLUSTERS: [
                    Basic.cluster_id,
                    Identify.cluster_id,
                    OnOff.cluster_id,
                    Ota.cluster_id,
                ],
            },
        },
    }

    replacement = {
        ENDPOINTS: {
            2: {
                PROFILE_ID: zha.PROFILE_ID,
                DEVICE_TYPE: zha.DeviceType.OCCUPANCY_SENSOR,
                INPUT_CLUSTERS: [
                    Basic.cluster_id,
                    PowerConfiguration.cluster_id,
                    Identify.cluster_id,
                    IlluminanceMeasurement.cluster_id,
                    TemperatureMeasurement.cluster_id,
                    OccupancyCluster,
                ],
                OUTPUT_CLUSTERS: [
                    Basic.cluster_id,
                    Identify.cluster_id,
                    OnOff.cluster_id,
                    Ota.cluster_id,
                ],
            },
        }
    }

```

Edit: have adding the replacement.
Hey,

That code works, it did load the new quirk.

![image](https://user-images.githubusercontent.com/873423/157223860-98f36a26-e2c9-4ca4-8641-23cf42140296.png)

I'm not seeing anything different though, should I ?
Thanks !
Ah I see, I can now find the occupancy sensitivity in the manage clusters menu. I just set it to 1 and that seems to have worked.
Thanks for the help ! Would be nice to expose those as entities, if that's possible maybe ?

Can we get that added to the official quirk somehow ?
So only the new import and the new device class was making all :-))

If you like you can making one PR for adding the changes of i can doing it for you.
I suggesting changing the name of the device class to `SignifyMotion` then its not coming any new with the name Philips and its making it "cleaner".

If Philips have putting in more functions we can adding them then we finding out how its working and you is getting them with HA updates.

And for info ZHA is looking implanting more switches and GUI elements for known extra functions like we have doing with all tuya TRVs (but not so god done for the user but looks nice and working OK) but **if** its coming we dont knowing but the sensitivity is one of the good function that being in the GUI.

Resolving patch:
diff --git a/zhaquirks/philips/signifymotion.py b/zhaquirks/philips/signifymotion.py
new file mode 100644
--- /dev/null
+++ b/zhaquirks/philips/signifymotion.py
@@ -0,0 +1,70 @@
+"""Quirk for Philips motion sensors."""
+from zigpy.profiles import zha
+from zigpy.quirks import CustomDevice
+from zigpy.zcl.clusters.general import Basic, Identify, OnOff, Ota, PowerConfiguration
+from zigpy.zcl.clusters.measurement import (
+    IlluminanceMeasurement,
+    OccupancySensing,
+    TemperatureMeasurement,
+)
+
+from zhaquirks.const import (
+    DEVICE_TYPE,
+    ENDPOINTS,
+    INPUT_CLUSTERS,
+    MODELS_INFO,
+    OUTPUT_CLUSTERS,
+    PROFILE_ID,
+)
+from zhaquirks.philips import SIGNIFY, OccupancyCluster
+
+
+class SignifyMotion(CustomDevice):
+    """Philips motion sensor device."""
+
+    signature = {
+        MODELS_INFO: [(SIGNIFY, "SML003")],
+        ENDPOINTS: {
+            2: {
+                PROFILE_ID: zha.PROFILE_ID,
+                DEVICE_TYPE: zha.DeviceType.OCCUPANCY_SENSOR,
+                INPUT_CLUSTERS: [
+                    Basic.cluster_id,
+                    PowerConfiguration.cluster_id,
+                    Identify.cluster_id,
+                    IlluminanceMeasurement.cluster_id,
+                    TemperatureMeasurement.cluster_id,
+                    OccupancySensing.cluster_id,
+                ],
+                OUTPUT_CLUSTERS: [
+                    Basic.cluster_id,
+                    Identify.cluster_id,
+                    OnOff.cluster_id,
+                    Ota.cluster_id,
+                ],
+            },
+        },
+    }
+
+    replacement = {
+        ENDPOINTS: {
+            2: {
+                PROFILE_ID: zha.PROFILE_ID,
+                DEVICE_TYPE: zha.DeviceType.OCCUPANCY_SENSOR,
+                INPUT_CLUSTERS: [
+                    Basic.cluster_id,
+                    PowerConfiguration.cluster_id,
+                    Identify.cluster_id,
+                    IlluminanceMeasurement.cluster_id,
+                    TemperatureMeasurement.cluster_id,
+                    OccupancyCluster,
+                ],
+                OUTPUT_CLUSTERS: [
+                    Basic.cluster_id,
+                    Identify.cluster_id,
+                    OnOff.cluster_id,
+                    Ota.cluster_id,
+                ],
+            },
+        }
+    }


Test patch: