Create tests for sense integration (#128418)

* Create tests for sense integration

* Rearrange files

* Update to use snapshots

* Update tests/components/sense/__init__.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/sense/__init__.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/sense/test_binary_sensor.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/sense/test_sensor.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Add missing imports

---------

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
pull/128670/head
Keilin Bickar 2024-10-23 12:22:21 -04:00 committed by GitHub
parent 5a0e47be48
commit 8aa25af014
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 2279 additions and 11 deletions

View File

@ -1 +1,23 @@
"""Tests for the Sense integration."""
from unittest.mock import patch
from homeassistant.components.sense.const import DOMAIN
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
async def setup_platform(
hass: HomeAssistant, config_entry: MockConfigEntry, platform: Platform
) -> MockConfigEntry:
"""Set up the Sense platform."""
config_entry.add_to_hass(hass)
with patch("homeassistant.components.sense.PLATFORMS", [platform]):
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
return config_entry

View File

@ -0,0 +1,70 @@
"""Common methods for Sense."""
from __future__ import annotations
from collections.abc import Generator
import datetime
from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch
import pytest
from homeassistant.components.sense.const import DOMAIN
from .const import (
DEVICE_1_DATA,
DEVICE_1_NAME,
DEVICE_2_DATA,
DEVICE_2_NAME,
MOCK_CONFIG,
MONITOR_ID,
)
from tests.common import MockConfigEntry
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.sense.async_setup_entry", return_value=True
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture
def config_entry() -> MockConfigEntry:
"""Mock sense config entry."""
return MockConfigEntry(
domain=DOMAIN,
data=MOCK_CONFIG,
unique_id="test-email",
)
@pytest.fixture
def mock_sense() -> Generator[MagicMock]:
"""Mock an ASyncSenseable object with a split foundation."""
with patch("homeassistant.components.sense.ASyncSenseable", autospec=True) as mock:
gateway = mock.return_value
gateway._devices = [DEVICE_1_NAME, DEVICE_2_NAME]
gateway.sense_monitor_id = MONITOR_ID
gateway.get_monitor_data.return_value = None
gateway.get_discovered_device_data.return_value = [DEVICE_1_DATA, DEVICE_2_DATA]
gateway.update_realtime.return_value = None
type(gateway).active_power = PropertyMock(return_value=100)
type(gateway).active_solar_power = PropertyMock(return_value=500)
type(gateway).active_voltage = PropertyMock(return_value=[120, 240])
gateway.get_trend.return_value = 15
gateway.trend_start.return_value = datetime.datetime.fromisoformat(
"2024-01-01 01:01:00+00:00"
)
def get_realtime():
yield {"devices": []}
yield {"devices": [DEVICE_1_DATA]}
while True:
yield {"devices": [DEVICE_1_DATA, DEVICE_2_DATA]}
gateway.get_realtime.side_effect = get_realtime()
yield gateway

View File

@ -0,0 +1,39 @@
"""Cosntants for the Sense integration tests."""
MOCK_CONFIG = {
"timeout": 6,
"email": "test-email",
"password": "test-password",
"access_token": "ABC",
"user_id": "123",
"monitor_id": "456",
"device_id": "789",
"refresh_token": "XYZ",
}
DEVICE_1_NAME = "Car"
DEVICE_1_ID = "abc123"
DEVICE_1_ICON = "car-electric"
DEVICE_1_POWER = 100.0
DEVICE_1_DATA = {
"name": DEVICE_1_NAME,
"id": DEVICE_1_ID,
"icon": "car",
"tags": {"DeviceListAllowed": "true"},
"w": DEVICE_1_POWER,
}
DEVICE_2_NAME = "Oven"
DEVICE_2_ID = "def456"
DEVICE_2_ICON = "stove"
DEVICE_2_POWER = 50.0
DEVICE_2_DATA = {
"name": DEVICE_2_NAME,
"id": DEVICE_2_ID,
"icon": "stove",
"tags": {"DeviceListAllowed": "true"},
"w": DEVICE_2_POWER,
}
MONITOR_ID = "12345"

View File

@ -0,0 +1,99 @@
# serializer version: 1
# name: test_binary_sensors[binary_sensor.car-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': None,
'entity_id': 'binary_sensor.car',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.POWER: 'power'>,
'original_icon': 'mdi:car-electric',
'original_name': 'Car',
'platform': 'sense',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '12345-abc123',
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensors[binary_sensor.car-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by Sense.com',
'device_class': 'power',
'friendly_name': 'Car',
'icon': 'mdi:car-electric',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.car',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unavailable',
})
# ---
# name: test_binary_sensors[binary_sensor.oven-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': None,
'entity_id': 'binary_sensor.oven',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.POWER: 'power'>,
'original_icon': 'mdi:stove',
'original_name': 'Oven',
'platform': 'sense',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '12345-def456',
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensors[binary_sensor.oven-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by Sense.com',
'device_class': 'power',
'friendly_name': 'Oven',
'icon': 'mdi:stove',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.oven',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unavailable',
})
# ---

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,73 @@
"""The tests for Sense binary sensor platform."""
from datetime import timedelta
from unittest.mock import MagicMock
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.sense.const import ACTIVE_UPDATE_RATE
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.util.dt import utcnow
from . import setup_platform
from .const import DEVICE_1_NAME, DEVICE_2_NAME
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
async def test_binary_sensors(
hass: HomeAssistant,
mock_sense: MagicMock,
config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test Sensor."""
await setup_platform(hass, config_entry, Platform.BINARY_SENSOR)
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
async def test_on_off_sensors(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_sense: MagicMock,
config_entry: MockConfigEntry,
) -> None:
"""Test the Sense binary sensors."""
await setup_platform(hass, config_entry, BINARY_SENSOR_DOMAIN)
state = hass.states.get(f"binary_sensor.{DEVICE_1_NAME.lower()}")
assert state.state == STATE_UNAVAILABLE
state = hass.states.get(f"binary_sensor.{DEVICE_2_NAME.lower()}")
assert state.state == STATE_UNAVAILABLE
async_fire_time_changed(hass, utcnow() + timedelta(seconds=ACTIVE_UPDATE_RATE))
await hass.async_block_till_done()
state = hass.states.get(f"binary_sensor.{DEVICE_1_NAME.lower()}")
assert state.state == STATE_OFF
state = hass.states.get(f"binary_sensor.{DEVICE_2_NAME.lower()}")
assert state.state == STATE_OFF
async_fire_time_changed(hass, utcnow() + timedelta(seconds=ACTIVE_UPDATE_RATE))
await hass.async_block_till_done()
state = hass.states.get(f"binary_sensor.{DEVICE_1_NAME.lower()}")
assert state.state == STATE_ON
state = hass.states.get(f"binary_sensor.{DEVICE_2_NAME.lower()}")
assert state.state == STATE_OFF
async_fire_time_changed(hass, utcnow() + timedelta(seconds=ACTIVE_UPDATE_RATE))
await hass.async_block_till_done()
state = hass.states.get(f"binary_sensor.{DEVICE_1_NAME.lower()}")
assert state.state == STATE_ON
state = hass.states.get(f"binary_sensor.{DEVICE_2_NAME.lower()}")
assert state.state == STATE_ON

View File

@ -16,18 +16,9 @@ from homeassistant.const import CONF_CODE
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
from .const import MOCK_CONFIG
MOCK_CONFIG = {
"timeout": 6,
"email": "test-email",
"password": "test-password",
"access_token": "ABC",
"user_id": "123",
"monitor_id": "456",
"device_id": "789",
"refresh_token": "XYZ",
}
from tests.common import MockConfigEntry
@pytest.fixture(name="mock_sense")

View File

@ -0,0 +1,215 @@
"""The tests for Sense sensor platform."""
from datetime import timedelta
from unittest.mock import MagicMock, PropertyMock
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.sense.const import ACTIVE_UPDATE_RATE, CONSUMPTION_ID
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.const import STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.util.dt import utcnow
from . import setup_platform
from .const import DEVICE_1_NAME, DEVICE_1_POWER, DEVICE_2_NAME, DEVICE_2_POWER
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_sensors(
hass: HomeAssistant,
mock_sense: MagicMock,
config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test Sensor."""
await setup_platform(hass, config_entry, Platform.SENSOR)
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
async def test_device_power_sensors(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_sense: MagicMock,
config_entry: MockConfigEntry,
) -> None:
"""Test the Sense device power sensors."""
await setup_platform(hass, config_entry, SENSOR_DOMAIN)
state = hass.states.get(f"sensor.{DEVICE_1_NAME.lower()}_{CONSUMPTION_ID}")
assert state.state == STATE_UNAVAILABLE
state = hass.states.get(f"sensor.{DEVICE_2_NAME.lower()}_{CONSUMPTION_ID}")
assert state.state == STATE_UNAVAILABLE
async_fire_time_changed(hass, utcnow() + timedelta(seconds=ACTIVE_UPDATE_RATE))
await hass.async_block_till_done()
state = hass.states.get(f"sensor.{DEVICE_1_NAME.lower()}_{CONSUMPTION_ID}")
assert state.state == "0"
state = hass.states.get(f"sensor.{DEVICE_2_NAME.lower()}_{CONSUMPTION_ID}")
assert state.state == "0"
async_fire_time_changed(hass, utcnow() + timedelta(seconds=ACTIVE_UPDATE_RATE))
await hass.async_block_till_done()
state = hass.states.get(f"sensor.{DEVICE_1_NAME.lower()}_{CONSUMPTION_ID}")
assert state.state == f"{DEVICE_1_POWER:.0f}"
state = hass.states.get(f"sensor.{DEVICE_2_NAME.lower()}_{CONSUMPTION_ID}")
assert state.state == "0"
async_fire_time_changed(hass, utcnow() + timedelta(seconds=ACTIVE_UPDATE_RATE))
await hass.async_block_till_done()
state = hass.states.get(f"sensor.{DEVICE_1_NAME.lower()}_{CONSUMPTION_ID}")
assert state.state == f"{DEVICE_1_POWER:.0f}"
state = hass.states.get(f"sensor.{DEVICE_2_NAME.lower()}_{CONSUMPTION_ID}")
assert state.state == f"{DEVICE_2_POWER:.0f}"
async def test_voltage_sensors(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_sense: MagicMock,
config_entry: MockConfigEntry,
) -> None:
"""Test the Sense voltage sensors."""
type(mock_sense).active_voltage = PropertyMock(return_value=[0, 0])
await setup_platform(hass, config_entry, SENSOR_DOMAIN)
state = hass.states.get("sensor.l1_voltage")
assert state.state == STATE_UNAVAILABLE
state = hass.states.get("sensor.l2_voltage")
assert state.state == STATE_UNAVAILABLE
type(mock_sense).active_voltage = PropertyMock(return_value=[120, 121])
async_fire_time_changed(hass, utcnow() + timedelta(seconds=ACTIVE_UPDATE_RATE))
await hass.async_block_till_done()
state = hass.states.get("sensor.l1_voltage")
assert state.state == "120"
state = hass.states.get("sensor.l2_voltage")
assert state.state == "121"
type(mock_sense).active_voltage = PropertyMock(return_value=[122, 123])
async_fire_time_changed(hass, utcnow() + timedelta(seconds=ACTIVE_UPDATE_RATE))
await hass.async_block_till_done()
state = hass.states.get("sensor.l1_voltage")
assert state.state == "122"
state = hass.states.get("sensor.l2_voltage")
assert state.state == "123"
async def test_active_power_sensors(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_sense: MagicMock,
config_entry: MockConfigEntry,
) -> None:
"""Test the Sense power sensors."""
await setup_platform(hass, config_entry, SENSOR_DOMAIN)
state = hass.states.get("sensor.energy_usage")
assert state.state == STATE_UNAVAILABLE
state = hass.states.get("sensor.energy_production")
assert state.state == STATE_UNAVAILABLE
type(mock_sense).active_power = PropertyMock(return_value=400)
type(mock_sense).active_solar_power = PropertyMock(return_value=500)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=ACTIVE_UPDATE_RATE))
await hass.async_block_till_done()
state = hass.states.get("sensor.energy_usage")
assert state.state == "400"
state = hass.states.get("sensor.energy_production")
assert state.state == "500"
type(mock_sense).active_power = PropertyMock(return_value=600)
type(mock_sense).active_solar_power = PropertyMock(return_value=700)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=ACTIVE_UPDATE_RATE))
await hass.async_block_till_done()
state = hass.states.get("sensor.energy_usage")
assert state.state == "600"
state = hass.states.get("sensor.energy_production")
assert state.state == "700"
async def test_trend_energy_sensors(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_sense: MagicMock,
config_entry: MockConfigEntry,
) -> None:
"""Test the Sense power sensors."""
mock_sense.get_trend.side_effect = lambda sensor_type, variant: {
("DAY", "usage"): 100,
("DAY", "production"): 200,
("DAY", "from_grid"): 300,
("DAY", "to_grid"): 400,
("DAY", "net_production"): 500,
("DAY", "production_pct"): 600,
("DAY", "solar_powered"): 700,
}.get((sensor_type, variant), 0)
await setup_platform(hass, config_entry, SENSOR_DOMAIN)
state = hass.states.get("sensor.daily_usage")
assert state.state == "100"
state = hass.states.get("sensor.daily_production")
assert state.state == "200"
state = hass.states.get("sensor.daily_from_grid")
assert state.state == "300"
state = hass.states.get("sensor.daily_to_grid")
assert state.state == "400"
state = hass.states.get("sensor.daily_net_production")
assert state.state == "500"
mock_sense.get_trend.side_effect = lambda sensor_type, variant: {
("DAY", "usage"): 1000,
("DAY", "production"): 2000,
("DAY", "from_grid"): 3000,
("DAY", "to_grid"): 4000,
("DAY", "net_production"): 5000,
("DAY", "production_pct"): 6000,
("DAY", "solar_powered"): 7000,
}.get((sensor_type, variant), 0)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=600))
await hass.async_block_till_done()
state = hass.states.get("sensor.daily_usage")
assert state.state == "1000"
state = hass.states.get("sensor.daily_production")
assert state.state == "2000"
state = hass.states.get("sensor.daily_from_grid")
assert state.state == "3000"
state = hass.states.get("sensor.daily_to_grid")
assert state.state == "4000"
state = hass.states.get("sensor.daily_net_production")
assert state.state == "5000"