-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for sensors in SwitchBot Cloud
- Loading branch information
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
"""Test for the switchbot_cloud sensors.""" | ||
|
||
from switchbot_api import Device | ||
|
||
from homeassistant.components.sensor import ATTR_STATE_CLASS | ||
from homeassistant.config_entries import ConfigEntryState | ||
from homeassistant.const import ( | ||
ATTR_FRIENDLY_NAME, | ||
ATTR_UNIT_OF_MEASUREMENT, | ||
STATE_UNKNOWN, | ||
) | ||
from homeassistant.core import HomeAssistant | ||
|
||
from . import configure_integration | ||
|
||
|
||
async def test_meter(hass: HomeAssistant, mock_list_devices, mock_get_status) -> None: | ||
"""Test turn on and turn off.""" | ||
mock_list_devices.return_value = [ | ||
Device( | ||
deviceId="meter-id-1", | ||
deviceName="meter-1", | ||
deviceType="Meter", | ||
hubDeviceId="test-hub-id", | ||
), | ||
] | ||
|
||
mock_get_status.return_value = { | ||
"temperature": 20.5, | ||
"scale": "CELSIUS", | ||
"humidity": 41, | ||
"battery": 80, | ||
} | ||
|
||
entry = configure_integration(hass) | ||
await hass.config_entries.async_setup(entry.entry_id) | ||
await hass.async_block_till_done() | ||
|
||
assert entry.state is ConfigEntryState.LOADED | ||
|
||
entity_id = "sensor.meter_1" | ||
|
||
battery_sensor = hass.states.get(f"{entity_id}_battery") | ||
battery_sensor_attributes = battery_sensor.attributes | ||
assert battery_sensor.state == "80" | ||
assert battery_sensor_attributes[ATTR_FRIENDLY_NAME] == "meter-1 Battery" | ||
assert battery_sensor_attributes[ATTR_UNIT_OF_MEASUREMENT] == "%" | ||
assert battery_sensor_attributes[ATTR_STATE_CLASS] == "measurement" | ||
|
||
humidity_sensor = hass.states.get(f"{entity_id}_humidity") | ||
humidity_sensor_attributes = humidity_sensor.attributes | ||
assert humidity_sensor.state == "41" | ||
assert humidity_sensor_attributes[ATTR_FRIENDLY_NAME] == "meter-1 Humidity" | ||
assert humidity_sensor_attributes[ATTR_UNIT_OF_MEASUREMENT] == "%" | ||
assert humidity_sensor_attributes[ATTR_STATE_CLASS] == "measurement" | ||
|
||
temperature_sensor = hass.states.get(f"{entity_id}_temperature") | ||
temperature_sensor_attributes = temperature_sensor.attributes | ||
assert temperature_sensor.state == "20.5" | ||
assert temperature_sensor_attributes[ATTR_FRIENDLY_NAME] == "meter-1 Temperature" | ||
assert temperature_sensor_attributes[ATTR_UNIT_OF_MEASUREMENT] == "°C" | ||
assert temperature_sensor_attributes[ATTR_STATE_CLASS] == "measurement" | ||
|
||
|
||
async def test_meter_no_coordinator_data( | ||
hass: HomeAssistant, mock_list_devices, mock_get_status | ||
) -> None: | ||
"""Test turn on and turn off.""" | ||
mock_list_devices.return_value = [ | ||
Device( | ||
deviceId="meter-id-1", | ||
deviceName="meter-1", | ||
deviceType="Meter", | ||
hubDeviceId="test-hub-id", | ||
), | ||
] | ||
|
||
mock_get_status.return_value = None | ||
|
||
entry = configure_integration(hass) | ||
await hass.config_entries.async_setup(entry.entry_id) | ||
await hass.async_block_till_done() | ||
|
||
assert entry.state is ConfigEntryState.LOADED | ||
|
||
entity_id = "sensor.meter_1" | ||
|
||
battery_sensor = hass.states.get(f"{entity_id}_battery") | ||
battery_sensor_attributes = battery_sensor.attributes | ||
assert battery_sensor.state == STATE_UNKNOWN | ||
assert battery_sensor_attributes[ATTR_FRIENDLY_NAME] == "meter-1 Battery" | ||
assert battery_sensor_attributes[ATTR_UNIT_OF_MEASUREMENT] == "%" | ||
assert battery_sensor_attributes[ATTR_STATE_CLASS] == "measurement" | ||
|
||
humidity_sensor = hass.states.get(f"{entity_id}_humidity") | ||
humidity_sensor_attributes = humidity_sensor.attributes | ||
assert humidity_sensor.state == STATE_UNKNOWN | ||
assert humidity_sensor_attributes[ATTR_FRIENDLY_NAME] == "meter-1 Humidity" | ||
assert humidity_sensor_attributes[ATTR_UNIT_OF_MEASUREMENT] == "%" | ||
assert humidity_sensor_attributes[ATTR_STATE_CLASS] == "measurement" | ||
|
||
temperature_sensor = hass.states.get(f"{entity_id}_temperature") | ||
temperature_sensor_attributes = temperature_sensor.attributes | ||
assert temperature_sensor.state == STATE_UNKNOWN | ||
assert temperature_sensor_attributes[ATTR_FRIENDLY_NAME] == "meter-1 Temperature" | ||
assert temperature_sensor_attributes[ATTR_UNIT_OF_MEASUREMENT] == "°C" | ||
assert temperature_sensor_attributes[ATTR_STATE_CLASS] == "measurement" |