Skip to content

Commit

Permalink
Add Meter Pro support to SwitchBot (#128991)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 24, 2024
1 parent d135da6 commit a2c9aa7
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions homeassistant/components/switchbot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
Platform.SENSOR,
],
SupportedModels.HYGROMETER.value: [Platform.SENSOR],
SupportedModels.HYGROMETER_CO2.value: [Platform.SENSOR],
SupportedModels.CONTACT.value: [Platform.BINARY_SENSOR, Platform.SENSOR],
SupportedModels.MOTION.value: [Platform.BINARY_SENSOR, Platform.SENSOR],
SupportedModels.HUMIDIFIER.value: [Platform.HUMIDIFIER, Platform.SENSOR],
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/switchbot/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class SupportedModels(StrEnum):
CEILING_LIGHT = "ceiling_light"
CURTAIN = "curtain"
HYGROMETER = "hygrometer"
HYGROMETER_CO2 = "hygrometer_co2"
LIGHT_STRIP = "light_strip"
CONTACT = "contact"
PLUG = "plug"
Expand Down Expand Up @@ -48,6 +49,8 @@ class SupportedModels(StrEnum):
NON_CONNECTABLE_SUPPORTED_MODEL_TYPES = {
SwitchbotModel.METER: SupportedModels.HYGROMETER,
SwitchbotModel.IO_METER: SupportedModels.HYGROMETER,
SwitchbotModel.METER_PRO: SupportedModels.HYGROMETER,
SwitchbotModel.METER_PRO_C: SupportedModels.HYGROMETER_CO2,
SwitchbotModel.CONTACT_SENSOR: SupportedModels.CONTACT,
SwitchbotModel.MOTION_SENSOR: SupportedModels.MOTION,
}
Expand Down
7 changes: 7 additions & 0 deletions homeassistant/components/switchbot/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
SensorStateClass,
)
from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION,
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
EntityCategory,
Expand Down Expand Up @@ -50,6 +51,12 @@
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
"co2": SensorEntityDescription(
key="co2",
native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.CO2,
),
"lightLevel": SensorEntityDescription(
key="lightLevel",
translation_key="light_level",
Expand Down
25 changes: 25 additions & 0 deletions tests/components/switchbot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,28 @@ async def init_integration(hass: HomeAssistant) -> MockConfigEntry:
connectable=True,
tx_power=-127,
)


WOMETERTHPC_SERVICE_INFO = BluetoothServiceInfoBleak(
name="WoTHPc",
manufacturer_data={
2409: b"\xb0\xe9\xfeT2\x15\xb7\xe4\x07\x9b\xa4\x007\x02\xd5\x00"
},
service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"5\x00d"},
service_uuids=["cba20d00-224d-11e6-9fb8-0002a5d5c51b"],
address="AA:BB:CC:DD:EE:AA",
rssi=-60,
source="local",
advertisement=generate_advertisement_data(
local_name="WoTHPc",
manufacturer_data={
2409: b"\xb0\xe9\xfeT2\x15\xb7\xe4\x07\x9b\xa4\x007\x02\xd5\x00"
},
service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"5\x00d"},
service_uuids=["cba20d00-224d-11e6-9fb8-0002a5d5c51b"],
),
device=generate_ble_device("AA:BB:CC:DD:EE:AA", "WoTHPc"),
time=0,
connectable=True,
tx_power=-127,
)
48 changes: 47 additions & 1 deletion tests/components/switchbot/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

from . import WOHAND_SERVICE_INFO
from . import WOHAND_SERVICE_INFO, WOMETERTHPC_SERVICE_INFO

from tests.common import MockConfigEntry
from tests.components.bluetooth import inject_bluetooth_service_info
Expand Down Expand Up @@ -59,3 +59,49 @@ async def test_sensors(hass: HomeAssistant) -> None:

assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_co2_sensor(hass: HomeAssistant) -> None:
"""Test setting up creates the co2 sensor for a WoTHPc."""
await async_setup_component(hass, DOMAIN, {})
inject_bluetooth_service_info(hass, WOMETERTHPC_SERVICE_INFO)

entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_ADDRESS: "AA:BB:CC:DD:EE:AA",
CONF_NAME: "test-name",
CONF_PASSWORD: "test-password",
CONF_SENSOR_TYPE: "hygrometer_co2",
},
unique_id="aabbccddeeaa",
)
entry.add_to_hass(hass)

assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

assert len(hass.states.async_all("sensor")) == 5

battery_sensor = hass.states.get("sensor.test_name_battery")
battery_sensor_attrs = battery_sensor.attributes
assert battery_sensor.state == "100"
assert battery_sensor_attrs[ATTR_FRIENDLY_NAME] == "test-name Battery"
assert battery_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "%"
assert battery_sensor_attrs[ATTR_STATE_CLASS] == "measurement"

rssi_sensor = hass.states.get("sensor.test_name_bluetooth_signal")
rssi_sensor_attrs = rssi_sensor.attributes
assert rssi_sensor.state == "-60"
assert rssi_sensor_attrs[ATTR_FRIENDLY_NAME] == "test-name Bluetooth signal"
assert rssi_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "dBm"

co2_sensor = hass.states.get("sensor.test_name_carbon_dioxide")
co2_sensor_attrs = co2_sensor.attributes
assert co2_sensor.state == "725"
assert co2_sensor_attrs[ATTR_FRIENDLY_NAME] == "test-name Carbon dioxide"
assert co2_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "ppm"

assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()

0 comments on commit a2c9aa7

Please sign in to comment.