|
| 1 | +"""SONOFF S60ZBTPF - Smart Socket with power measurement fix. |
| 2 | +
|
| 3 | +This device has a quirk where it continues to report active power consumption |
| 4 | +even when the socket is turned off. This quirk fixes that by setting the |
| 5 | +`active_power` and `rms_current` to 0 when the `on_off` state is False. |
| 6 | +""" |
| 7 | + |
| 8 | +from zigpy.quirks import CustomCluster |
| 9 | +from zigpy.quirks.v2 import QuirkBuilder |
| 10 | +import zigpy.types as t |
| 11 | +from zigpy.zcl.clusters.general import OnOff |
| 12 | +from zigpy.zcl.clusters.homeautomation import ElectricalMeasurement |
| 13 | + |
| 14 | +POWER_ID = ElectricalMeasurement.AttributeDefs.active_power.id |
| 15 | +CURRENT_ID = ElectricalMeasurement.AttributeDefs.rms_current.id |
| 16 | +VOLTAGE_ID = ElectricalMeasurement.AttributeDefs.rms_voltage.id |
| 17 | +ON_OFF_ID = OnOff.AttributeDefs.on_off.id |
| 18 | + |
| 19 | + |
| 20 | +class SonoffS60OnOff(CustomCluster, OnOff): |
| 21 | + """Custom OnOff cluster that resets power readings when the socket is turned off.""" |
| 22 | + |
| 23 | + def _update_attribute(self, attrid, value): |
| 24 | + """Reset attributes to zero when the socket is turned off.""" |
| 25 | + |
| 26 | + if attrid == ON_OFF_ID and value == t.Bool.false: |
| 27 | + self.debug( |
| 28 | + "Socket turned off, resetting power and current measurements to zero" |
| 29 | + ) |
| 30 | + self.endpoint.electrical_measurement.update_attribute(POWER_ID, 0) |
| 31 | + self.endpoint.electrical_measurement.update_attribute(CURRENT_ID, 0) |
| 32 | + self.endpoint.electrical_measurement.update_attribute(VOLTAGE_ID, None) |
| 33 | + |
| 34 | + super()._update_attribute(attrid, value) |
| 35 | + |
| 36 | + |
| 37 | +class SonoffS60ElectricalMeasurement(CustomCluster, ElectricalMeasurement): |
| 38 | + """Custom ElectricalMeasurement cluster that prevents power updates when the socket is turned off.""" |
| 39 | + |
| 40 | + def _update_attribute(self, attrid, value): |
| 41 | + """Prevent updates when the socket is turned off.""" |
| 42 | + |
| 43 | + if self.endpoint.on_off.get(ON_OFF_ID) == t.Bool.false: |
| 44 | + if attrid == POWER_ID: |
| 45 | + self.debug("Socket turned off, preventing power measurement update") |
| 46 | + return |
| 47 | + |
| 48 | + if attrid == CURRENT_ID: |
| 49 | + self.debug("Socket turned off, preventing current measurement update") |
| 50 | + return |
| 51 | + |
| 52 | + if attrid == VOLTAGE_ID: |
| 53 | + self.debug("Socket turned off, preventing voltage measurement update") |
| 54 | + return |
| 55 | + |
| 56 | + super()._update_attribute(attrid, value) |
| 57 | + |
| 58 | + |
| 59 | +( |
| 60 | + QuirkBuilder("SONOFF", "S60ZBTPF") |
| 61 | + .also_applies_to("SONOFF", "S60ZBTPG") |
| 62 | + .replaces(SonoffS60OnOff) |
| 63 | + .replaces(SonoffS60ElectricalMeasurement) |
| 64 | + .add_to_registry() |
| 65 | +) |
0 commit comments