Skip to content

Commit

Permalink
Add support for switchbot co2 sensors
Browse files Browse the repository at this point in the history
needs #128990
needs #128947
  • Loading branch information
bdraco committed Oct 22, 2024
1 parent 053eb8a commit 4d228bb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
8 changes: 8 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,13 @@
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
"co2": SensorEntityDescription(
key="co2",
name=None,
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,
)
42 changes: 41 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,43 @@ 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: "bot",
},
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")) == 2

battery_sensor = hass.states.get("sensor.test_name_battery")
battery_sensor_attrs = battery_sensor.attributes
assert battery_sensor.state == "89"
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"

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

0 comments on commit 4d228bb

Please sign in to comment.