| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076 |
- """Display and calibration settings of the Meter Pro (and Meter Pro CO2)."""
- from unittest.mock import AsyncMock
- import pytest
- from bleak.backends.device import BLEDevice
- from switchbot import (
- MeterProAlarmMode,
- MeterProAlarmVolume,
- MeterProCO2AlarmMode,
- MeterProCO2AlarmStatus,
- MeterProComfortStatus,
- MeterProDateFormat,
- MeterProDisplaySource,
- MeterProDisplayZone,
- MeterProTemperatureHumidityAlarmMode,
- MeterProTemperatureHumidityAlarmStatus,
- MeterProZoneIcon,
- SwitchbotOperationError,
- )
- from switchbot.devices.meter_pro import SwitchbotMeterPro, SwitchbotMeterProCO2
- def create_device(cls=SwitchbotMeterPro):
- ble_device = BLEDevice(
- address="aa:bb:cc:dd:ee:ff", name="any", details={"rssi": -80}
- )
- device = cls(ble_device)
- device._send_command = AsyncMock(return_value=bytes.fromhex("01"))
- return device
- # Captured from a Meter Pro (firmware 1.9): middle zone shows this device with
- # the indoor icon, bottom zone shows an Indoor/Outdoor Meter with the outdoor
- # icon, data update every 600 s, DD/MM, battery icon always shown.
- DISPLAY_SETTINGS = "011253000000000000d4e5f6a1b2c30258010101"
- @pytest.mark.asyncio
- async def test_get_display_settings():
- device = create_device()
- device._send_command.return_value = bytes.fromhex(DISPLAY_SETTINGS)
- settings = await device.get_display_settings()
- device._send_command.assert_called_with("570f690701")
- assert settings == {
- "middle": {
- "icon": MeterProZoneIcon.INDOOR,
- "source": MeterProDisplaySource.METER_PRO,
- "source_mac": None,
- },
- "bottom": {
- "icon": MeterProZoneIcon.OUTDOOR,
- "source": MeterProDisplaySource.INDOOR_OUTDOOR_METER,
- "source_mac": "D4:E5:F6:A1:B2:C3",
- },
- "data_update_interval": 600,
- "date_format": MeterProDateFormat.DAY_MONTH,
- "battery_icon_always": True,
- }
- @pytest.mark.asyncio
- async def test_get_display_settings_factory_state():
- """Factory layout: unused bottom zone, MM/DD, battery icon only when low."""
- device = create_device()
- device._send_command.return_value = bytes.fromhex(
- "0110500000000000000000000000000258000100"
- )
- settings = await device.get_display_settings()
- assert settings["bottom"] == {
- "icon": None,
- "source": MeterProDisplaySource.NONE,
- "source_mac": None,
- }
- assert settings["date_format"] is MeterProDateFormat.MONTH_DAY
- assert settings["battery_icon_always"] is False
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("date_code", "battery_code", "date_format", "battery_icon_always"),
- [
- (0x02, 0x00, None, False),
- (0x00, 0xFE, MeterProDateFormat.MONTH_DAY, None),
- (0xFF, 0xFF, None, None),
- ],
- )
- async def test_get_display_settings_unknown_values(
- date_code: int,
- battery_code: int,
- date_format: MeterProDateFormat | None,
- battery_icon_always: bool | None,
- ):
- device = create_device()
- response = bytearray.fromhex(DISPLAY_SETTINGS)
- response[17] = date_code
- response[19] = battery_code
- device._send_command.return_value = bytes(response)
- settings = await device.get_display_settings()
- assert settings["date_format"] is date_format
- assert settings["battery_icon_always"] is battery_icon_always
- assert settings["data_update_interval"] == 600
- @pytest.mark.asyncio
- async def test_get_display_settings_short_response():
- """Older firmware may stop after the MAC addresses."""
- device = create_device()
- device._send_command.return_value = bytes.fromhex(DISPLAY_SETTINGS[:30])
- settings = await device.get_display_settings()
- assert settings["bottom"]["source_mac"] == "D4:E5:F6:A1:B2:C3"
- assert settings["data_update_interval"] is None
- assert settings["date_format"] is None
- assert settings["battery_icon_always"] is None
- @pytest.mark.asyncio
- async def test_get_display_settings_unknown_source_kept_raw():
- device = create_device()
- device._send_command.return_value = bytes.fromhex(
- "011257000000000000aabbccddeeff0258010101"
- )
- settings = await device.get_display_settings()
- assert settings["bottom"]["source"] == 7
- @pytest.mark.asyncio
- async def test_get_display_settings_unknown_icon_kept_raw():
- device = create_device()
- device._send_command.return_value = bytes.fromhex(
- "013053000000000000aabbccddeeff0258010101"
- )
- settings = await device.get_display_settings()
- assert settings["middle"]["icon"] == 3
- assert settings["bottom"]["icon"] is None
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("icon_byte", "middle", "bottom"),
- [
- ("f1", None, MeterProZoneIcon.INDOOR),
- ("2f", MeterProZoneIcon.OUTDOOR, None),
- ],
- )
- async def test_get_display_settings_hold_icon(
- icon_byte: str, middle: MeterProZoneIcon | None, bottom: MeterProZoneIcon | None
- ):
- device = create_device()
- device._send_command.return_value = bytes.fromhex(
- DISPLAY_SETTINGS[:2] + icon_byte + DISPLAY_SETTINGS[4:]
- )
- settings = await device.get_display_settings()
- assert settings["middle"]["icon"] is middle
- assert settings["bottom"]["icon"] is bottom
- @pytest.mark.asyncio
- async def test_get_display_settings_weather_station_and_hub3():
- device = create_device()
- device._send_command.return_value = bytes.fromhex(
- "011289aabbccddee01aabbccddee020258010101"
- )
- settings = await device.get_display_settings()
- assert settings["middle"]["source"] is MeterProDisplaySource.WEATHER_STATION
- assert settings["bottom"]["source"] is MeterProDisplaySource.HUB3
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- "response",
- ["02", "011253000000"], # error status, then response too short
- )
- async def test_get_display_settings_failure(response: str):
- device = create_device()
- device._send_command.return_value = bytes.fromhex(response)
- with pytest.raises(SwitchbotOperationError):
- await device.get_display_settings()
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("zone", "source", "mac", "icon", "expected_payload"),
- [
- (
- MeterProDisplayZone.BOTTOM,
- MeterProDisplaySource.INDOOR_OUTDOOR_METER,
- "D4:E5:F6:A1:B2:C3",
- MeterProZoneIcon.OUTDOOR,
- "570f680701020203d4e5f6a1b2c3",
- ),
- (
- MeterProDisplayZone.BOTTOM,
- MeterProDisplaySource.NETWORK,
- None,
- MeterProZoneIcon.OUTDOOR,
- "570f680701020206000000000000",
- ),
- (
- MeterProDisplayZone.BOTTOM,
- MeterProDisplaySource.NONE,
- None,
- None,
- "570f68070102ff00000000000000",
- ),
- (
- MeterProDisplayZone.MIDDLE,
- MeterProDisplaySource.METER_PRO,
- None,
- MeterProZoneIcon.INDOOR,
- "570f680701010105000000000000",
- ),
- (
- MeterProDisplayZone.MIDDLE,
- MeterProDisplaySource.METER_PLUS,
- "aa-bb-cc-dd-ee-ff",
- None,
- "570f68070101ff02aabbccddeeff",
- ),
- ],
- )
- async def test_set_display_zone(
- zone: MeterProDisplayZone,
- source: MeterProDisplaySource,
- mac: str | None,
- icon: MeterProZoneIcon | None,
- expected_payload: str,
- ):
- device = create_device()
- await device.set_display_zone(zone, source, mac, icon)
- device._send_command.assert_called_with(expected_payload)
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("source", "mac"),
- [
- (MeterProDisplaySource.METER, None), # needs a MAC
- (MeterProDisplaySource.NETWORK, "aa:bb:cc:dd:ee:ff"), # takes none
- (MeterProDisplaySource.NONE, "aa:bb:cc:dd:ee:ff"), # takes none
- (MeterProDisplaySource.METER, "aa:bb:cc"), # malformed
- (MeterProDisplaySource.METER, "zz:bb:cc:dd:ee:ff"), # malformed
- ],
- )
- async def test_set_display_zone_invalid(source: MeterProDisplaySource, mac: str):
- device = create_device()
- with pytest.raises(SwitchbotOperationError):
- await device.set_display_zone(MeterProDisplayZone.BOTTOM, source, mac)
- device._send_command.assert_not_called()
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("middle", "bottom", "expected_payload"),
- [
- (MeterProZoneIcon.INDOOR, MeterProZoneIcon.OUTDOOR, "570f680701030102"),
- (None, MeterProZoneIcon.INDOOR, "570f680701030f01"),
- (MeterProZoneIcon.OUTDOOR, None, "570f68070103020f"),
- ],
- )
- async def test_set_zone_icons(
- middle: MeterProZoneIcon | None,
- bottom: MeterProZoneIcon | None,
- expected_payload: str,
- ):
- device = create_device()
- await device.set_zone_icons(middle, bottom)
- device._send_command.assert_called_with(expected_payload)
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("seconds", "expected_payload"),
- [(300, "570f68070105012c"), (600, "570f680701050258"), (1800, "570f680701050708")],
- )
- async def test_set_data_update_interval(seconds: int, expected_payload: str):
- device = create_device()
- await device.set_data_update_interval(seconds)
- device._send_command.assert_called_with(expected_payload)
- @pytest.mark.asyncio
- @pytest.mark.parametrize("seconds", [0, -1, 0x10000, True, 300.0, "300"])
- async def test_set_data_update_interval_invalid(seconds: object):
- device = create_device()
- with pytest.raises(SwitchbotOperationError):
- await device.set_data_update_interval(seconds)
- device._send_command.assert_not_called()
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("date_format", "expected_payload"),
- [
- (MeterProDateFormat.MONTH_DAY, "570f6807010700"),
- (MeterProDateFormat.DAY_MONTH, "570f6807010701"),
- ],
- )
- async def test_set_date_format(date_format: MeterProDateFormat, expected_payload: str):
- device = create_device()
- await device.set_date_format(date_format)
- device._send_command.assert_called_with(expected_payload)
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("always", "expected_payload"),
- [(True, "570f6807010801"), (False, "570f6807010800")],
- )
- async def test_set_battery_icon(always: bool, expected_payload: str):
- device = create_device()
- await device.set_battery_icon(always)
- device._send_command.assert_called_with(expected_payload)
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("fahrenheit", "expected_payload"),
- [(True, "570f6807010901"), (False, "570f6807010900")],
- )
- async def test_set_temperature_unit(fahrenheit: bool, expected_payload: str):
- device = create_device()
- await device.set_temperature_unit(fahrenheit)
- device._send_command.assert_called_with(expected_payload)
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("response", "expected"),
- [
- ("010101", {"unit_switch": False, "source_switch": True}),
- ("010000", {"unit_switch": True, "source_switch": False}),
- ("01ff01", {"unit_switch": None, "source_switch": True}),
- ("0100ff", {"unit_switch": True, "source_switch": None}),
- ("01ffff", {"unit_switch": None, "source_switch": None}),
- ("010202", {"unit_switch": None, "source_switch": None}),
- ],
- )
- async def test_get_button_functions(response: str, expected: dict[str, bool | None]):
- device = create_device()
- device._send_command.return_value = bytes.fromhex(response)
- assert await device.get_button_functions() == expected
- device._send_command.assert_called_with("570f690704")
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("unit_switch", "source_switch", "expected_payload"),
- [
- (True, False, "570f680701060000"),
- (False, True, "570f680701060101"),
- (None, True, "570f68070106ff01"),
- (False, None, "570f6807010601ff"),
- ],
- )
- async def test_set_button_functions(
- unit_switch: bool | None, source_switch: bool | None, expected_payload: str
- ):
- device = create_device()
- await device.set_button_functions(unit_switch, source_switch)
- device._send_command.assert_called_with(expected_payload)
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("response", "temperature", "humidity"),
- [
- ("01000000", 0.0, 0), # factory state: sign bits cleared
- ("01808000", 0.0, 0),
- ("01018305", -1.5, 3),
- ("01820a03", 2.3, -10),
- ("01800005", 0.5, 0),
- ("01808015", 0.5, 0),
- ],
- )
- async def test_get_calibration(response: str, temperature: float, humidity: int):
- device = create_device()
- device._send_command.return_value = bytes.fromhex(response)
- calibration = await device.get_calibration()
- device._send_command.assert_called_with("570f6903")
- assert calibration == {"temperature": temperature, "humidity": humidity}
- assert str(calibration["temperature"]) != "-0.0"
- @pytest.mark.asyncio
- @pytest.mark.parametrize("response", ["0180800a", "0180e400", "01806400", "018080fa"])
- async def test_get_calibration_rejects_invalid_values(response: str):
- device = create_device()
- device._send_command.return_value = bytes.fromhex(response)
- with pytest.raises(SwitchbotOperationError, match="Invalid calibration response"):
- await device.get_calibration()
- @pytest.mark.asyncio
- async def test_get_calibration_short_response():
- device = create_device()
- device._send_command.return_value = bytes.fromhex("0101")
- with pytest.raises(SwitchbotOperationError):
- await device.get_calibration()
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("temperature", "humidity", "expected_payload"),
- [
- (0, 0, "570f6803808000"),
- (-1.5, 3, "570f6803018305"),
- (2.3, -10, "570f6803820a03"),
- (0.5, 0, "570f6803808005"), # positive below one degree keeps its sign
- (-0.5, 0, "570f6803008005"),
- (1.25, 0, "570f6803818003"), # rounded to one decimal
- ],
- )
- async def test_set_calibration(
- temperature: float, humidity: int, expected_payload: str
- ):
- device = create_device()
- await device.set_calibration(temperature, humidity)
- device._send_command.assert_called_with(expected_payload)
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("temperature", "humidity"),
- [(128, 0), (-128, 0), (0, 100), (0, -100), (0, True), (0, 0.5), (0, "1")],
- )
- async def test_set_calibration_invalid(temperature: float, humidity: object):
- device = create_device()
- with pytest.raises(SwitchbotOperationError):
- await device.set_calibration(temperature, humidity)
- device._send_command.assert_not_called()
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("method", "args"),
- [
- ("set_calibration", (1.0, 1)),
- ("set_display_zone", (MeterProDisplayZone.BOTTOM, MeterProDisplaySource.NONE)),
- ("set_zone_icons", (MeterProZoneIcon.INDOOR,)),
- ("set_data_update_interval", (600,)),
- ("set_date_format", (MeterProDateFormat.DAY_MONTH,)),
- ("set_battery_icon", (True,)),
- ("set_temperature_unit", (False,)),
- ("set_button_functions", (True, True)),
- ],
- )
- async def test_setters_raise_on_device_error(method: str, args: tuple):
- device = create_device()
- device._send_command.return_value = bytes.fromhex("05") # not supported
- with pytest.raises(SwitchbotOperationError):
- await getattr(device, method)(*args)
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("method", "args", "expected_payload"),
- [
- ("set_calibration", (1.0, 1), "570f6803818100"),
- (
- "set_display_zone",
- (MeterProDisplayZone.BOTTOM, MeterProDisplaySource.NETWORK),
- "570f68070102ff06000000000000",
- ),
- ("set_zone_icons", (MeterProZoneIcon.INDOOR,), "570f68070103010f"),
- ("set_data_update_interval", (600,), "570f680701050258"),
- ("set_date_format", (MeterProDateFormat.DAY_MONTH,), "570f6807010701"),
- ("set_battery_icon", (True,), "570f6807010801"),
- ("set_temperature_unit", (False,), "570f6807010900"),
- ("set_button_functions", (True, True), "570f680701060001"),
- ],
- )
- async def test_co2_variant_shares_display_settings(
- method: str, args: tuple, expected_payload: str
- ):
- device = create_device(SwitchbotMeterProCO2)
- await getattr(device, method)(*args)
- device._send_command.assert_called_with(expected_payload)
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("device_class", "response", "expected"),
- [
- (
- SwitchbotMeterPro,
- "01009c4600921e00",
- {
- "custom": False,
- "temperature_low_c": 18.0,
- "temperature_high_c": 28.0,
- "humidity_low": 30,
- "humidity_high": 70,
- "status": MeterProComfortStatus.COMFORTABLE,
- },
- ),
- (
- SwitchbotMeterProCO2,
- "010180e3730080fe",
- {
- "custom": True,
- "temperature_low_c": -0.3,
- "temperature_high_c": 0.7,
- "humidity_low": 0,
- "humidity_high": 99,
- "status": MeterProComfortStatus.WET,
- },
- ),
- ],
- )
- async def test_get_comfort_level(
- device_class: type[SwitchbotMeterPro],
- response: str,
- expected: dict[str, float | int | bool | MeterProComfortStatus],
- ):
- device = create_device(device_class)
- device._send_command.return_value = bytes.fromhex(response)
- assert await device.get_comfort_level() == expected
- device._send_command.assert_awaited_once_with("570f69020101")
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("limits", "custom", "expected"),
- [
- ((0.5, 22.34, 40, 82), True, "889652358028"),
- ((-0.5, 0.66, 0, 99), True, "888063750000"),
- ((18.0, 28.0, 30, 70), False, "809c4600921e"),
- ],
- )
- async def test_set_comfort_level(
- limits: tuple[float, float, int, int], custom: bool, expected: str
- ):
- device = create_device()
- await device.set_comfort_level(*limits, custom=custom)
- device._send_command.assert_awaited_once_with("570f680201" + expected)
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("limits", "custom", "message"),
- [
- ((-128, 28.0, 30, 70), True, "Temperature must be"),
- ((True, 28.0, 30, 70), True, "Temperature must be"),
- ((18.01, 18.04, 30, 70), True, "Lower comfort limits"),
- ((18, 28, 30, 100), True, "Humidity limits"),
- ((18, 28, True, 70), True, "Humidity limits"),
- ((18, 28, 70, 70), True, "Lower comfort limits"),
- ((18, 28, 30, 70), 1, "custom must be a boolean"),
- ],
- )
- async def test_set_comfort_level_rejects_invalid_input(
- limits: tuple[float, float, int, int], custom: bool, message: str
- ):
- device = create_device()
- with pytest.raises(SwitchbotOperationError, match=message):
- await device.set_comfort_level(*limits, custom=custom)
- device._send_command.assert_not_awaited()
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- "response", ["02", "01", "01029c4600921e00", "01009c460a921e00", "01009c6400921e00"]
- )
- async def test_get_comfort_level_rejects_invalid_response(response: str):
- device = create_device()
- device._send_command.return_value = bytes.fromhex(response)
- with pytest.raises(SwitchbotOperationError):
- await device.get_comfort_level()
- @pytest.mark.asyncio
- async def test_set_comfort_level_rejects_device_error():
- device = create_device()
- device._send_command.return_value = b"\x02"
- with pytest.raises(SwitchbotOperationError, match="set_comfort_level"):
- await device.set_comfort_level(18, 28, 30, 70)
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("device_class", "response", "mode", "volume"),
- [
- (SwitchbotMeterPro, "010000", MeterProAlarmMode.DISABLED, None),
- (
- SwitchbotMeterProCO2,
- "010402",
- MeterProAlarmMode.SOUND_AND_FLASH,
- MeterProAlarmVolume.HIGH,
- ),
- (SwitchbotMeterPro, "01ff01", MeterProAlarmMode.FLASH_ONLY, None),
- ],
- )
- async def test_get_alarm_settings(
- device_class: type[SwitchbotMeterPro],
- response: str,
- mode: MeterProAlarmMode,
- volume: MeterProAlarmVolume | None,
- ):
- device = create_device(device_class)
- device._send_command.return_value = bytes.fromhex(response)
- assert await device.get_alarm_settings() == {"mode": mode, "volume": volume}
- device._send_command.assert_awaited_once_with("570f690204")
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("mode", "volume", "payload"),
- [
- (MeterProAlarmMode.DISABLED, None, "ff00"),
- (MeterProAlarmMode.FLASH_ONLY, MeterProAlarmVolume.OFF, "0101"),
- (MeterProAlarmMode.SOUND_AND_FLASH, MeterProAlarmVolume.LOW, "0202"),
- (MeterProAlarmMode.SOUND_AND_FLASH, MeterProAlarmVolume.MEDIUM, "0302"),
- (MeterProAlarmMode.SOUND_AND_FLASH, MeterProAlarmVolume.HIGH, "0402"),
- ],
- )
- async def test_set_alarm_settings(
- mode: MeterProAlarmMode, volume: MeterProAlarmVolume | None, payload: str
- ):
- device = create_device()
- await device.set_alarm_settings(mode, volume)
- device._send_command.assert_awaited_once_with("570f680204" + payload)
- @pytest.mark.asyncio
- @pytest.mark.parametrize("response", ["02", "0100", "010403"])
- async def test_get_alarm_settings_rejects_invalid_response(response: str):
- device = create_device()
- device._send_command.return_value = bytes.fromhex(response)
- with pytest.raises(SwitchbotOperationError):
- await device.get_alarm_settings()
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("mode", "volume", "message"),
- [(True, None, "mode must be"), (MeterProAlarmMode.DISABLED, 5, "volume must be")],
- )
- async def test_set_alarm_settings_rejects_invalid_input(
- mode: object, volume: object, message: str
- ):
- device = create_device()
- with pytest.raises(SwitchbotOperationError, match=message):
- await device.set_alarm_settings(mode, volume)
- device._send_command.assert_not_awaited()
- @pytest.mark.asyncio
- async def test_set_alarm_settings_rejects_device_error():
- device = create_device()
- device._send_command.return_value = b"\x02"
- with pytest.raises(SwitchbotOperationError, match="set_alarm_settings"):
- await device.set_alarm_settings(MeterProAlarmMode.DISABLED)
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("response", "mode", "status"),
- [
- ("01000578019000", MeterProCO2AlarmMode.DISABLED, MeterProCO2AlarmStatus.NONE),
- (
- "010b05780190fe",
- MeterProCO2AlarmMode.CUSTOM
- | MeterProCO2AlarmMode.LOW
- | MeterProCO2AlarmMode.HIGH,
- MeterProCO2AlarmStatus.HIGH,
- ),
- (
- "01840578019003",
- MeterProCO2AlarmMode(0x84),
- MeterProCO2AlarmStatus.IN_RANGE,
- ),
- ],
- )
- async def test_get_co2_alarm(
- response: str, mode: MeterProCO2AlarmMode, status: MeterProCO2AlarmStatus
- ):
- device = create_device(SwitchbotMeterProCO2)
- device._send_command.return_value = bytes.fromhex(response)
- assert await device.get_co2_alarm() == {
- "mode": mode,
- "low_ppm": 400,
- "high_ppm": 1400,
- "status": status,
- }
- device._send_command.assert_awaited_once_with("570f69020301")
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("mode", "low", "high", "payload"),
- [
- (MeterProCO2AlarmMode.DISABLED, 400, 1400, "0005780190"),
- (MeterProCO2AlarmMode.LOW, 401, 1101, "01044d0191"),
- (MeterProCO2AlarmMode.HIGH, 0, 0xFFFF, "02ffff0000"),
- (
- MeterProCO2AlarmMode.LOW | MeterProCO2AlarmMode.HIGH,
- 425,
- 2000,
- "0307d001a9",
- ),
- (MeterProCO2AlarmMode.IN_RANGE, 1200, 1200, "0404b004b0"),
- (
- MeterProCO2AlarmMode.HIGH | MeterProCO2AlarmMode.CUSTOM,
- 425,
- 2000,
- "0a07d001a9",
- ),
- ],
- )
- async def test_set_co2_alarm(
- mode: MeterProCO2AlarmMode, low: int, high: int, payload: str
- ):
- device = create_device(SwitchbotMeterProCO2)
- await device.set_co2_alarm(mode, low, high)
- device._send_command.assert_awaited_once_with("570f68020301" + payload)
- @pytest.mark.asyncio
- @pytest.mark.parametrize("response", ["02", "010005780190"])
- async def test_get_co2_alarm_rejects_invalid_response(response: str):
- device = create_device(SwitchbotMeterProCO2)
- device._send_command.return_value = bytes.fromhex(response)
- with pytest.raises(SwitchbotOperationError, match="get_co2_alarm"):
- await device.get_co2_alarm()
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("mode", "low", "high", "message"),
- [
- (True, 400, 1400, "Invalid CO2 alarm mode"),
- (MeterProCO2AlarmMode(0x10), 400, 1400, "Invalid CO2 alarm mode"),
- (MeterProCO2AlarmMode.LOW, True, 1400, "unsigned 16-bit integers"),
- (MeterProCO2AlarmMode.LOW, -1, 1400, "unsigned 16-bit integers"),
- (MeterProCO2AlarmMode.LOW, 400, 0x10000, "unsigned 16-bit integers"),
- (MeterProCO2AlarmMode.LOW, 400, 1400.0, "unsigned 16-bit integers"),
- (MeterProCO2AlarmMode.IN_RANGE, 1400, 400, "Lower CO2 alarm limit"),
- ],
- )
- async def test_set_co2_alarm_rejects_invalid_input(
- mode: object, low: object, high: object, message: str
- ):
- device = create_device(SwitchbotMeterProCO2)
- with pytest.raises(SwitchbotOperationError, match=message):
- await device.set_co2_alarm(mode, low, high)
- device._send_command.assert_not_awaited()
- @pytest.mark.asyncio
- async def test_set_co2_alarm_rejects_device_error():
- device = create_device(SwitchbotMeterProCO2)
- device._send_command.return_value = b"\x02"
- with pytest.raises(SwitchbotOperationError, match="set_co2_alarm"):
- await device.set_co2_alarm(MeterProCO2AlarmMode.DISABLED, 400, 1400)
- def test_co2_alarm_is_only_available_on_the_co2_model():
- device = create_device()
- assert not hasattr(device, "get_co2_alarm")
- assert not hasattr(device, "set_co2_alarm")
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("device_class", "response", "expected"),
- [
- (
- SwitchbotMeterPro,
- "0100000000000000",
- {
- "mode": MeterProTemperatureHumidityAlarmMode.DISABLED,
- "temperature_low_c": 0.0,
- "temperature_high_c": 0.0,
- "humidity_low": 0,
- "humidity_high": 0,
- "temperature_status": MeterProTemperatureHumidityAlarmStatus.NONE,
- "humidity_status": MeterProTemperatureHumidityAlarmStatus.NONE,
- },
- ),
- (
- SwitchbotMeterProCO2,
- "014297a745009423",
- {
- "mode": (
- MeterProTemperatureHumidityAlarmMode.TEMPERATURE_HIGH
- | MeterProTemperatureHumidityAlarmMode.HUMIDITY_IN_RANGE
- ),
- "temperature_low_c": -0.5,
- "temperature_high_c": 23.4,
- "humidity_low": 20,
- "humidity_high": 39,
- "temperature_status": MeterProTemperatureHumidityAlarmStatus.HIGH,
- "humidity_status": MeterProTemperatureHumidityAlarmStatus.IN_RANGE,
- },
- ),
- ],
- )
- async def test_get_temperature_humidity_alarm(
- device_class: type[SwitchbotMeterPro], response: str, expected: dict[str, object]
- ):
- device = create_device(device_class)
- device._send_command.return_value = bytes.fromhex(response)
- assert await device.get_temperature_humidity_alarm() == expected
- device._send_command.assert_awaited_once_with("570f69020100")
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("response", "temperature_status", "humidity_status"),
- [
- (
- "0122963695000a20",
- MeterProTemperatureHumidityAlarmStatus.HIGH,
- MeterProTemperatureHumidityAlarmStatus.NONE,
- ),
- (
- "0122a32795000a02",
- MeterProTemperatureHumidityAlarmStatus.NONE,
- MeterProTemperatureHumidityAlarmStatus.HIGH,
- ),
- (
- "0122972735001822",
- MeterProTemperatureHumidityAlarmStatus.HIGH,
- MeterProTemperatureHumidityAlarmStatus.HIGH,
- ),
- (
- "0122963695000aec",
- MeterProTemperatureHumidityAlarmStatus.HIGH,
- MeterProTemperatureHumidityAlarmStatus.NONE,
- ),
- (
- "0122a32795000ace",
- MeterProTemperatureHumidityAlarmStatus.NONE,
- MeterProTemperatureHumidityAlarmStatus.HIGH,
- ),
- (
- "014297a7450094ff",
- MeterProTemperatureHumidityAlarmStatus.IN_RANGE,
- MeterProTemperatureHumidityAlarmStatus.IN_RANGE,
- ),
- ],
- )
- async def test_get_temperature_humidity_alarm_status(
- response: str,
- temperature_status: MeterProTemperatureHumidityAlarmStatus,
- humidity_status: MeterProTemperatureHumidityAlarmStatus,
- ):
- device = create_device()
- device._send_command.return_value = bytes.fromhex(response)
- result = await device.get_temperature_humidity_alarm()
- assert result["temperature_status"] is temperature_status
- assert result["humidity_status"] is humidity_status
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("mode", "limits", "payload"),
- [
- (MeterProTemperatureHumidityAlarmMode.DISABLED, (0, 0, 0, 0), "008000008000"),
- (
- MeterProTemperatureHumidityAlarmMode.TEMPERATURE_HIGH
- | MeterProTemperatureHumidityAlarmMode.HUMIDITY_HIGH,
- (-0.5, 23.34, 20, 39),
- "229727350014",
- ),
- (
- MeterProTemperatureHumidityAlarmMode.TEMPERATURE_IN_RANGE
- | MeterProTemperatureHumidityAlarmMode.HUMIDITY_IN_RANGE,
- (0.5, 0.5, 0, 99),
- "448063558000",
- ),
- ],
- )
- async def test_set_temperature_humidity_alarm(
- mode: MeterProTemperatureHumidityAlarmMode,
- limits: tuple[float, float, int, int],
- payload: str,
- ):
- device = create_device()
- await device.set_temperature_humidity_alarm(mode, *limits)
- device._send_command.assert_awaited_once_with("570f680201" + payload)
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("mode", "limits", "message"),
- [
- (True, (18, 28, 30, 70), "Invalid temperature and humidity alarm mode"),
- (
- MeterProTemperatureHumidityAlarmMode(0x88),
- (18, 28, 30, 70),
- "Invalid temperature and humidity alarm mode",
- ),
- (
- MeterProTemperatureHumidityAlarmMode(0x08),
- (18, 28, 30, 70),
- "Invalid temperature and humidity alarm mode",
- ),
- (
- MeterProTemperatureHumidityAlarmMode.DISABLED,
- (-128, 28, 30, 70),
- "Temperature must be",
- ),
- (
- MeterProTemperatureHumidityAlarmMode.DISABLED,
- (18, 28, True, 70),
- "Humidity limits",
- ),
- (
- MeterProTemperatureHumidityAlarmMode.DISABLED,
- (18, 28, 30, 100),
- "Humidity limits",
- ),
- (
- MeterProTemperatureHumidityAlarmMode.TEMPERATURE_IN_RANGE,
- (28, 18, 30, 70),
- "Lower in-range alarm limits",
- ),
- (
- MeterProTemperatureHumidityAlarmMode.HUMIDITY_IN_RANGE,
- (18, 28, 70, 30),
- "Lower in-range alarm limits",
- ),
- ],
- )
- async def test_set_temperature_humidity_alarm_rejects_invalid_inputs(
- mode: object, limits: tuple[float, float, int, int], message: str
- ):
- device = create_device()
- with pytest.raises(SwitchbotOperationError, match=message):
- await device.set_temperature_humidity_alarm(mode, *limits)
- device._send_command.assert_not_awaited()
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- "response",
- [
- "02",
- "01000000000000",
- "01009c460a921e00",
- "01009c6400921e00",
- ],
- )
- async def test_get_temperature_humidity_alarm_rejects_invalid_response(response: str):
- device = create_device()
- device._send_command.return_value = bytes.fromhex(response)
- with pytest.raises(SwitchbotOperationError):
- await device.get_temperature_humidity_alarm()
- @pytest.mark.asyncio
- async def test_set_temperature_humidity_alarm_rejects_device_error():
- device = create_device()
- device._send_command.return_value = b"\x02"
- with pytest.raises(SwitchbotOperationError, match="set_temperature_humidity_alarm"):
- await device.set_temperature_humidity_alarm(
- MeterProTemperatureHumidityAlarmMode.DISABLED, 0, 0, 0, 0
- )
- @pytest.mark.asyncio
- async def test_enum_setters_accept_raw_ints():
- device = create_device()
- await device.set_display_zone(2, 3, "D4:E5:F6:A1:B2:C3", icon=2)
- device._send_command.assert_called_with("570f6807010202" + "03d4e5f6a1b2c3")
- await device.set_zone_icons(middle=1, bottom=2)
- device._send_command.assert_called_with("570f680701030102")
- await device.set_date_format(1)
- device._send_command.assert_called_with("570f6807010701")
- @pytest.mark.asyncio
- @pytest.mark.parametrize(
- ("method", "args", "kwargs"),
- [
- ("set_display_zone", (3, MeterProDisplaySource.NONE), {}),
- ("set_display_zone", ("bottom", MeterProDisplaySource.NONE), {}),
- ("set_display_zone", (MeterProDisplayZone.BOTTOM, 15), {}),
- ("set_display_zone", (MeterProDisplayZone.BOTTOM, True), {}),
- (
- "set_display_zone",
- (MeterProDisplayZone.BOTTOM, MeterProDisplaySource.NONE),
- {"icon": 9},
- ),
- ("set_zone_icons", (), {"middle": 3}),
- ("set_zone_icons", (), {"bottom": "outdoor"}),
- ("set_date_format", (2,), {}),
- ("set_date_format", ("DD/MM",), {}),
- ("set_battery_icon", ("false",), {}),
- ("set_battery_icon", (1,), {}),
- ("set_temperature_unit", (0.0,), {}),
- ("set_button_functions", (), {"unit_switch": "off"}),
- ("set_button_functions", (), {"source_switch": 1}),
- ],
- )
- async def test_setters_reject_wrong_types(method: str, args: tuple, kwargs: dict):
- device = create_device()
- with pytest.raises(SwitchbotOperationError):
- await getattr(device, method)(*args, **kwargs)
- device._send_command.assert_not_called()
|