hass-core/tests/components/canary/test_sensor.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

217 lines
7.1 KiB
Python
Raw Normal View History

"""The tests for the Canary sensor platform."""
from datetime import timedelta
2021-01-01 22:31:56 +01:00
from unittest.mock import patch
from homeassistant.components.canary.const import DOMAIN, MANUFACTURER
Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 14:07:39 +08:00
from homeassistant.components.canary.sensor import (
ATTR_AIR_QUALITY,
STATE_AIR_QUALITY_ABNORMAL,
STATE_AIR_QUALITY_NORMAL,
STATE_AIR_QUALITY_VERY_ABNORMAL,
)
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
2020-09-13 11:32:41 -05:00
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow
2020-09-13 11:32:41 -05:00
from . import mock_device, mock_location, mock_reading
from tests.common import async_fire_time_changed
2020-09-13 11:32:41 -05:00
async def test_sensors_pro(
hass: HomeAssistant,
canary,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
2020-09-13 11:32:41 -05:00
"""Test the creation and values of the sensors for Canary Pro."""
online_device_at_home = mock_device(20, "Dining Room", True, "Canary Pro")
instance = canary.return_value
instance.get_locations.return_value = [
mock_location(100, "Home", True, devices=[online_device_at_home]),
]
instance.get_latest_readings.return_value = [
mock_reading("temperature", "21.12"),
mock_reading("humidity", "50.46"),
mock_reading("air_quality", "0.59"),
]
config = {DOMAIN: {"username": "test-username", "password": "test-password"}}
with patch("homeassistant.components.canary.PLATFORMS", ["sensor"]):
2020-09-13 11:32:41 -05:00
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
sensors = {
"home_dining_room_temperature": (
"20_temperature",
"21.12",
UnitOfTemperature.CELSIUS,
SensorDeviceClass.TEMPERATURE,
2020-09-13 11:32:41 -05:00
None,
),
"home_dining_room_humidity": (
"20_humidity",
"50.46",
PERCENTAGE,
SensorDeviceClass.HUMIDITY,
2020-09-13 11:32:41 -05:00
None,
),
"home_dining_room_air_quality": (
"20_air_quality",
"0.59",
None,
None,
"mdi:weather-windy",
),
}
2023-02-02 18:35:24 +01:00
for sensor_id, data in sensors.items():
entity_entry = entity_registry.async_get(f"sensor.{sensor_id}")
2020-09-13 11:32:41 -05:00
assert entity_entry
assert entity_entry.original_device_class == data[3]
2020-09-13 11:32:41 -05:00
assert entity_entry.unique_id == data[0]
assert entity_entry.original_icon == data[4]
state = hass.states.get(f"sensor.{sensor_id}")
assert state
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == data[2]
assert state.state == data[1]
device = device_registry.async_get_device(identifiers={(DOMAIN, "20")})
assert device
assert device.manufacturer == MANUFACTURER
assert device.name == "Dining Room"
assert device.model == "Canary Pro"
2020-09-13 11:32:41 -05:00
async def test_sensors_attributes_pro(hass: HomeAssistant, canary) -> None:
2020-09-13 11:32:41 -05:00
"""Test the creation and values of the sensors attributes for Canary Pro."""
online_device_at_home = mock_device(20, "Dining Room", True, "Canary Pro")
instance = canary.return_value
instance.get_locations.return_value = [
mock_location(100, "Home", True, devices=[online_device_at_home]),
]
instance.get_latest_readings.return_value = [
mock_reading("temperature", "21.12"),
mock_reading("humidity", "50.46"),
mock_reading("air_quality", "0.59"),
]
config = {DOMAIN: {"username": "test-username", "password": "test-password"}}
with patch("homeassistant.components.canary.PLATFORMS", ["sensor"]):
2020-09-13 11:32:41 -05:00
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
entity_id = "sensor.home_dining_room_air_quality"
2021-08-10 09:19:28 +02:00
state1 = hass.states.get(entity_id)
assert state1
assert state1.state == "0.59"
assert state1.attributes[ATTR_AIR_QUALITY] == STATE_AIR_QUALITY_ABNORMAL
2020-09-13 11:32:41 -05:00
instance.get_latest_readings.return_value = [
mock_reading("temperature", "21.12"),
mock_reading("humidity", "50.46"),
mock_reading("air_quality", "0.4"),
]
future = utcnow() + timedelta(seconds=30)
async_fire_time_changed(hass, future)
await async_update_entity(hass, entity_id)
2020-09-13 11:32:41 -05:00
await hass.async_block_till_done()
2021-08-10 09:19:28 +02:00
state2 = hass.states.get(entity_id)
assert state2
assert state2.state == "0.4"
assert state2.attributes[ATTR_AIR_QUALITY] == STATE_AIR_QUALITY_VERY_ABNORMAL
2020-09-13 11:32:41 -05:00
instance.get_latest_readings.return_value = [
mock_reading("temperature", "21.12"),
mock_reading("humidity", "50.46"),
mock_reading("air_quality", "1.0"),
]
future += timedelta(seconds=30)
async_fire_time_changed(hass, future)
await async_update_entity(hass, entity_id)
2020-09-13 11:32:41 -05:00
await hass.async_block_till_done()
2021-08-10 09:19:28 +02:00
state3 = hass.states.get(entity_id)
assert state3
assert state3.state == "1.0"
assert state3.attributes[ATTR_AIR_QUALITY] == STATE_AIR_QUALITY_NORMAL
2020-09-13 11:32:41 -05:00
async def test_sensors_flex(
hass: HomeAssistant,
canary,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
2020-09-13 11:32:41 -05:00
"""Test the creation and values of the sensors for Canary Flex."""
online_device_at_home = mock_device(20, "Dining Room", True, "Canary Flex")
instance = canary.return_value
instance.get_locations.return_value = [
mock_location(100, "Home", True, devices=[online_device_at_home]),
]
instance.get_latest_readings.return_value = [
mock_reading("battery", "70.4567"),
mock_reading("wifi", "-57"),
]
config = {DOMAIN: {"username": "test-username", "password": "test-password"}}
with patch("homeassistant.components.canary.PLATFORMS", ["sensor"]):
2020-09-13 11:32:41 -05:00
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
sensors = {
"home_dining_room_battery": (
"20_battery",
"70.46",
PERCENTAGE,
SensorDeviceClass.BATTERY,
None,
),
"home_dining_room_wifi": (
"20_wifi",
"-57.0",
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
SensorDeviceClass.SIGNAL_STRENGTH,
2020-09-13 11:32:41 -05:00
None,
),
}
2023-02-02 18:35:24 +01:00
for sensor_id, data in sensors.items():
entity_entry = entity_registry.async_get(f"sensor.{sensor_id}")
2020-09-13 11:32:41 -05:00
assert entity_entry
assert entity_entry.original_device_class == data[3]
2020-09-13 11:32:41 -05:00
assert entity_entry.unique_id == data[0]
assert entity_entry.original_icon == data[4]
state = hass.states.get(f"sensor.{sensor_id}")
assert state
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == data[2]
assert state.state == data[1]
device = device_registry.async_get_device(identifiers={(DOMAIN, "20")})
assert device
assert device.manufacturer == MANUFACTURER
assert device.name == "Dining Room"
assert device.model == "Canary Flex"