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 BatterySize enum #3752

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion tests/test_tuya_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
from zigpy.quirks.v2 import CustomDeviceV2
import zigpy.types as t
from zigpy.zcl import foundation
from zigpy.zcl.clusters.general import Basic, BatterySize
from zigpy.zcl.clusters.general import Basic

from tests.common import ClusterListener, MockDatetime, wait_for_zigpy_tasks
import zhaquirks
from zhaquirks.const import BatterySize
from zhaquirks.tuya import (
TUYA_QUERY_DATA,
TUYA_SET_TIME,
Expand Down Expand Up @@ -93,6 +94,9 @@ async def test_convenience_methods(device_mock, method_name, attr_name, exp_clas
(TuyaPowerConfigurationCluster2AAA, None, None, None, BatterySize.AAA, 2, 15),
(None, BatterySize.CR123A, 1, 60, BatterySize.CR123A, 1, 60),
(None, BatterySize.CR123A, 1, None, BatterySize.CR123A, 1, 30),
(None, BatterySize.CR2450, 1, None, BatterySize.CR2450, 1, 30),
(None, BatterySize.CR2032, 1, None, BatterySize.CR2032, 1, 30),
(None, BatterySize.CR1632, 1, None, BatterySize.CR1632, 1, 30),
(None, BatterySize.AA, None, None, BatterySize.AA, None, None),
(None, None, None, None, None, None, None),
],
Expand Down
34 changes: 30 additions & 4 deletions tests/test_xiaomi.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
OUTPUT_CLUSTERS,
PROFILE_ID,
ZONE_STATUS_CHANGE_COMMAND,
BatterySize,
)
from zhaquirks.xiaomi import (
LUMI,
Expand All @@ -58,6 +59,8 @@
XiaomiQuickInitDevice,
handle_quick_init,
)
import zhaquirks.xiaomi.aqara.cube
import zhaquirks.xiaomi.aqara.cube_aqgl01
import zhaquirks.xiaomi.aqara.driver_curtain_e1
from zhaquirks.xiaomi.aqara.feeder_acn001 import (
FEEDER_ATTR,
Expand All @@ -75,9 +78,14 @@
AqaraFeederAcn001,
OppleCluster,
)
import zhaquirks.xiaomi.aqara.magnet_ac01
import zhaquirks.xiaomi.aqara.magnet_acn001
import zhaquirks.xiaomi.aqara.magnet_agl02
import zhaquirks.xiaomi.aqara.magnet_aq2
import zhaquirks.xiaomi.aqara.motion_ac02
import zhaquirks.xiaomi.aqara.motion_acn001
import zhaquirks.xiaomi.aqara.motion_agl02
import zhaquirks.xiaomi.aqara.motion_agl04
import zhaquirks.xiaomi.aqara.motion_aq2
import zhaquirks.xiaomi.aqara.motion_aq2b
import zhaquirks.xiaomi.aqara.plug
Expand All @@ -89,6 +97,7 @@
from zhaquirks.xiaomi.aqara.thermostat_agl001 import ScheduleEvent, ScheduleSettings
import zhaquirks.xiaomi.aqara.weather
import zhaquirks.xiaomi.mija.motion
import zhaquirks.xiaomi.mija.smoke

zhaquirks.setup()

Expand Down Expand Up @@ -384,10 +393,27 @@ async def test_mija_battery(zigpy_device_from_quirk, voltage, bpr):
@pytest.mark.parametrize(
"quirk, batt_size",
(
(zhaquirks.xiaomi.aqara.vibration_aq1.VibrationAQ1, 0x0A),
(zhaquirks.xiaomi.mija.motion.Motion, 0x09),
(zhaquirks.xiaomi.mija.sensor_switch.MijaButton, 0x0A),
(zhaquirks.xiaomi.mija.sensor_magnet.Magnet, 0x0B),
(zhaquirks.xiaomi.aqara.vibration_aq1.VibrationAQ1, BatterySize.CR2032),
(zhaquirks.xiaomi.aqara.cube.Cube, BatterySize.CR2450),
(zhaquirks.xiaomi.aqara.cube_aqgl01.CubeAQGL01, BatterySize.CR2450),
(zhaquirks.xiaomi.aqara.cube_aqgl01.CubeCAGL02, BatterySize.CR2450),
(zhaquirks.xiaomi.aqara.magnet_ac01.LumiMagnetAC01, BatterySize.CR123A),
(zhaquirks.xiaomi.aqara.magnet_acn001.MagnetE1, BatterySize.CR1632),
(zhaquirks.xiaomi.aqara.magnet_agl02.MagnetT1, BatterySize.CR1632),
(zhaquirks.xiaomi.aqara.magnet_aq2.MagnetAQ2, BatterySize.CR1632),
(zhaquirks.xiaomi.aqara.motion_ac02.LumiMotionAC02, BatterySize.CR1632),
(zhaquirks.xiaomi.aqara.motion_acn001.MotionE1, BatterySize.CR1632),
(zhaquirks.xiaomi.aqara.motion_agl02.MotionT1, BatterySize.CR1632),
(zhaquirks.xiaomi.aqara.motion_agl04.LumiLumiMotionAgl04, BatterySize.CR1632),
(zhaquirks.xiaomi.aqara.motion_aq2.MotionAQ2, BatterySize.CR2450),
(zhaquirks.xiaomi.aqara.motion_aq2b.MotionAQ2, BatterySize.CR2450),
(zhaquirks.xiaomi.mija.motion.Motion, BatterySize.CR2450),
(zhaquirks.xiaomi.mija.sensor_switch.MijaButton, BatterySize.CR2032),
(zhaquirks.xiaomi.mija.sensor_magnet.Magnet, BatterySize.CR1632),
(
zhaquirks.xiaomi.mija.smoke.MijiaHoneywellSmokeDetectorSensor,
BatterySize.CR123A,
),
),
)
async def test_xiaomi_batt_size(zigpy_device_from_quirk, quirk, batt_size):
Expand Down
19 changes: 19 additions & 0 deletions zhaquirks/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
SIG_NODE_DESC,
SIG_SKIP_CONFIG,
)
import zigpy.types as t

ARGS = "args"
ATTR_ID = "attr_id"
Expand Down Expand Up @@ -125,3 +126,21 @@
ZONE_STATE = 0x0000
ZONE_TYPE = 0x0001
ZONE_STATUS = 0x0002


class BatterySize(t.enum8):
"""Battery sizes."""

No_battery = 0x00
Built_in = 0x01
Other = 0x02
AA = 0x03
AAA = 0x04
C = 0x05
D = 0x06
CR2 = 0x07
CR123A = 0x08
CR2450 = 0x09
CR2032 = 0x0A
CR1632 = 0x0B
Unknown = 0xFF
11 changes: 6 additions & 5 deletions zhaquirks/ikea/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from zigpy.zcl.clusters.general import Basic, PowerConfiguration, Scenes

from zhaquirks import EventableCluster
from zhaquirks.const import BatterySize

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -148,7 +149,7 @@ class PowerConfig1AAACluster(CustomCluster, PowerConfiguration):
"""Updating power attributes: 2 AAA."""

_CONSTANT_ATTRIBUTES = {
BATTERY_SIZE: 4,
BATTERY_SIZE: BatterySize.AAA,
BATTERY_QUANTITY: 1,
BATTERY_RATED_VOLTAGE: 15,
}
Expand All @@ -158,7 +159,7 @@ class PowerConfig2AAACluster(CustomCluster, PowerConfiguration):
"""Updating power attributes: 2 AAA."""

_CONSTANT_ATTRIBUTES = {
BATTERY_SIZE: 4,
BATTERY_SIZE: BatterySize.AAA,
BATTERY_QUANTITY: 2,
BATTERY_RATED_VOLTAGE: 15,
}
Expand All @@ -168,7 +169,7 @@ class PowerConfig2CRCluster(CustomCluster, PowerConfiguration):
"""Updating power attributes: 2 CR2032."""

_CONSTANT_ATTRIBUTES = {
BATTERY_SIZE: 10,
BATTERY_SIZE: BatterySize.CR2032,
BATTERY_QUANTITY: 2,
BATTERY_RATED_VOLTAGE: 30,
}
Expand All @@ -178,7 +179,7 @@ class PowerConfig1CRCluster(CustomCluster, PowerConfiguration):
"""Updating power attributes: 1 CR2032."""

_CONSTANT_ATTRIBUTES = {
BATTERY_SIZE: 10,
BATTERY_SIZE: BatterySize.CR2032,
BATTERY_QUANTITY: 1,
BATTERY_RATED_VOLTAGE: 30,
}
Expand All @@ -189,7 +190,7 @@ class PowerConfig1CRXCluster(CustomCluster, PowerConfiguration):

_CONSTANT_ATTRIBUTES = {
BATTERY_VOLTAGE: 0,
BATTERY_SIZE: 10,
BATTERY_SIZE: BatterySize.CR2032,
BATTERY_QUANTITY: 1,
BATTERY_RATED_VOLTAGE: 30,
}
Expand Down
9 changes: 2 additions & 7 deletions zhaquirks/tuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@
import zigpy.types as t
from zigpy.zcl import BaseAttributeDefs, foundation
from zigpy.zcl.clusters.closures import WindowCovering
from zigpy.zcl.clusters.general import (
Basic,
BatterySize,
LevelControl,
OnOff,
PowerConfiguration,
)
from zigpy.zcl.clusters.general import Basic, LevelControl, OnOff, PowerConfiguration
from zigpy.zcl.clusters.homeautomation import ElectricalMeasurement
from zigpy.zcl.clusters.hvac import Thermostat, UserInterface
from zigpy.zcl.clusters.smartenergy import Metering
Expand All @@ -30,6 +24,7 @@
RIGHT,
SHORT_PRESS,
ZHA_SEND_EVENT,
BatterySize,
)

# ---------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion zhaquirks/tuya/builder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from zigpy.quirks.v2.homeassistant.sensor import SensorDeviceClass, SensorStateClass
import zigpy.types as t
from zigpy.zcl import foundation
from zigpy.zcl.clusters.general import BatterySize
from zigpy.zcl.clusters.measurement import (
PM25,
CarbonDioxideConcentration,
Expand All @@ -31,6 +30,7 @@
from zigpy.zcl.clusters.smartenergy import Metering
from zigpy.zcl.foundation import BaseAttributeDefs, ZCLAttributeDef

from zhaquirks.const import BatterySize
from zhaquirks.tuya import (
TUYA_CLUSTER_ID,
BaseEnchantedDevice,
Expand All @@ -54,6 +54,9 @@
BatterySize.AA: 15,
BatterySize.CR2: 30,
BatterySize.CR123A: 30,
BatterySize.CR2450: 30,
BatterySize.CR2032: 30,
BatterySize.CR1632: 30,
BatterySize.Unknown: None,
}

Expand Down
2 changes: 1 addition & 1 deletion zhaquirks/tuya/tuya_gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from zigpy.quirks.v2.homeassistant import CONCENTRATION_PARTS_PER_MILLION, UnitOfTime
from zigpy.quirks.v2.homeassistant.sensor import SensorDeviceClass, SensorStateClass
import zigpy.types as t
from zigpy.zcl.clusters.general import BatterySize
from zigpy.zcl.clusters.security import IasZone

from zhaquirks.const import BatterySize
from zhaquirks.tuya import TuyaLocalCluster
from zhaquirks.tuya.builder import TuyaQuirkBuilder

Expand Down
3 changes: 2 additions & 1 deletion zhaquirks/tuya/tuya_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import zigpy.types as t
from zigpy.zcl import foundation

from zhaquirks.const import BatterySize
from zhaquirks.tuya import (
TUYA_SET_TIME,
TuyaPowerConfigurationCluster2AAA,
Expand Down Expand Up @@ -305,7 +306,7 @@ class NoManufTimeTuyaMCUCluster(TuyaMCUCluster):
.applies_to("_TZE200_n8dljorx", "TS0601")
.tuya_illuminance(dp_id=101)
.tuya_contact(dp_id=1)
.tuya_battery(dp_id=2)
.tuya_battery(dp_id=2, battery_type=BatterySize.CR2032, battery_qty=1)
.skip_configuration()
.add_to_registry()
)
2 changes: 1 addition & 1 deletion zhaquirks/tuya/tuya_siren.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from zigpy.quirks.v2.homeassistant import PERCENTAGE, UnitOfTemperature, UnitOfTime
from zigpy.quirks.v2.homeassistant.binary_sensor import BinarySensorDeviceClass
import zigpy.types as t
from zigpy.zcl.clusters.general import BatterySize

from zhaquirks.const import BatterySize
from zhaquirks.tuya.builder import TuyaQuirkBuilder


Expand Down
2 changes: 1 addition & 1 deletion zhaquirks/tuya/tuya_valve.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from zigpy.quirks.v2.homeassistant import UnitOfTime, UnitOfVolume
from zigpy.quirks.v2.homeassistant.sensor import SensorDeviceClass, SensorStateClass
import zigpy.types as t
from zigpy.zcl.clusters.general import BatterySize
from zigpy.zcl.clusters.smartenergy import Metering

from zhaquirks.const import BatterySize
from zhaquirks.tuya import TUYA_CLUSTER_ID
from zhaquirks.tuya.builder import TuyaQuirkBuilder, TuyaValveWaterConsumed
from zhaquirks.tuya.mcu import TuyaMCUCluster
Expand Down
7 changes: 5 additions & 2 deletions zhaquirks/xiaomi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
UNKNOWN,
VALUE,
ZHA_SEND_EVENT,
BatterySize,
)

BATTERY_LEVEL = "battery_level"
Expand Down Expand Up @@ -116,7 +117,7 @@ class XiaomiCustomDevice(CustomDevice):
def __init__(self, *args, **kwargs):
"""Init."""
if not hasattr(self, BATTERY_SIZE):
self.battery_size = 10
self.battery_size = BatterySize.CR2032
super().__init__(*args, **kwargs)


Expand Down Expand Up @@ -507,7 +508,9 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._CONSTANT_ATTRIBUTES = {
BATTERY_QUANTITY_ATTR: 1,
BATTERY_SIZE_ATTR: getattr(self.endpoint.device, BATTERY_SIZE, 0xFF),
BATTERY_SIZE_ATTR: getattr(
self.endpoint.device, BATTERY_SIZE, BatterySize.Unknown
),
}
self._slope = 200 / (self.MAX_VOLTS_MV - self.MIN_VOLTS_MV)

Expand Down
3 changes: 2 additions & 1 deletion zhaquirks/xiaomi/aqara/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
TURN_ON,
VALUE,
ZHA_SEND_EVENT,
BatterySize,
)
from zhaquirks.xiaomi import (
LUMI,
Expand Down Expand Up @@ -161,7 +162,7 @@ class Cube(XiaomiQuickInitDevice):

def __init__(self, *args, **kwargs):
"""Init."""
self.battery_size = 9
self.battery_size = BatterySize.CR2450
super().__init__(*args, **kwargs)

class MultistateInputCluster(CustomCluster, MultistateInput):
Expand Down
5 changes: 3 additions & 2 deletions zhaquirks/xiaomi/aqara/cube_aqgl01.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
TURN_ON,
VALUE,
ZHA_SEND_EVENT,
BatterySize,
)
from zhaquirks.xiaomi import (
LUMI,
Expand Down Expand Up @@ -214,7 +215,7 @@ class CubeAQGL01(XiaomiCustomDevice):

def __init__(self, *args, **kwargs):
"""Init."""
self.battery_size = 9
self.battery_size = BatterySize.CR2450
super().__init__(*args, **kwargs)

signature = {
Expand Down Expand Up @@ -353,7 +354,7 @@ class CubeCAGL02(XiaomiCustomDevice):

def __init__(self, *args, **kwargs):
"""Init."""
self.battery_size = 9
self.battery_size = BatterySize.CR2450
super().__init__(*args, **kwargs)

signature = {
Expand Down
3 changes: 2 additions & 1 deletion zhaquirks/xiaomi/aqara/magnet_ac01.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
MODELS_INFO,
OUTPUT_CLUSTERS,
PROFILE_ID,
BatterySize,
)
from zhaquirks.xiaomi import (
LUMI,
Expand Down Expand Up @@ -42,7 +43,7 @@ class LumiMagnetAC01(XiaomiCustomDevice):

def __init__(self, *args, **kwargs):
"""Init."""
self.battery_size = 8
self.battery_size = BatterySize.CR123A
super().__init__(*args, **kwargs)

signature = {
Expand Down
3 changes: 2 additions & 1 deletion zhaquirks/xiaomi/aqara/magnet_acn001.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
MODELS_INFO,
OUTPUT_CLUSTERS,
PROFILE_ID,
BatterySize,
)
from zhaquirks.xiaomi import (
LUMI,
Expand All @@ -28,7 +29,7 @@ class MagnetE1(XiaomiCustomDevice):

def __init__(self, *args, **kwargs):
"""Init."""
self.battery_size = 11
self.battery_size = BatterySize.CR1632
super().__init__(*args, **kwargs)

signature = {
Expand Down
3 changes: 2 additions & 1 deletion zhaquirks/xiaomi/aqara/magnet_agl02.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
MODELS_INFO,
OUTPUT_CLUSTERS,
PROFILE_ID,
BatterySize,
)
from zhaquirks.xiaomi import (
BasicCluster,
Expand All @@ -25,7 +26,7 @@ class MagnetT1(XiaomiCustomDevice):

def __init__(self, *args, **kwargs):
"""Init."""
self.battery_size = 11
self.battery_size = BatterySize.CR1632
super().__init__(*args, **kwargs)

signature = {
Expand Down
Loading
Loading