core/tests/components/switchbot/test_sensor.py

60 lines
2.0 KiB
Python

"""Test the switchbot sensors."""
from homeassistant.components.sensor import ATTR_STATE_CLASS
from homeassistant.components.switchbot.const import DOMAIN
from homeassistant.const import (
ATTR_FRIENDLY_NAME,
ATTR_UNIT_OF_MEASUREMENT,
CONF_ADDRESS,
CONF_NAME,
CONF_PASSWORD,
CONF_SENSOR_TYPE,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from . import WOHAND_SERVICE_INFO
from tests.common import MockConfigEntry
from tests.components.bluetooth import inject_bluetooth_service_info
async def test_sensors(hass: HomeAssistant, entity_registry_enabled_by_default) -> None:
"""Test setting up creates the sensors."""
await async_setup_component(hass, DOMAIN, {})
inject_bluetooth_service_info(hass, WOHAND_SERVICE_INFO)
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_ADDRESS: "aa:bb:cc:dd:ee:ff",
CONF_NAME: "test-name",
CONF_PASSWORD: "test-password",
CONF_SENSOR_TYPE: "bot",
},
unique_id="aabbccddeeff",
)
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_strength")
rssi_sensor_attrs = rssi_sensor.attributes
assert rssi_sensor.state == "-60"
assert (
rssi_sensor_attrs[ATTR_FRIENDLY_NAME] == "test-name Bluetooth signal strength"
)
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()