init home-assistant
This commit is contained in:
1
home-assistant/tests/components/luftdaten/__init__.py
Normal file
1
home-assistant/tests/components/luftdaten/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Define tests for the Luftdaten component."""
|
||||
79
home-assistant/tests/components/luftdaten/conftest.py
Normal file
79
home-assistant/tests/components/luftdaten/conftest.py
Normal file
@@ -0,0 +1,79 @@
|
||||
"""Fixtures for Luftdaten tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.luftdaten.const import CONF_SENSOR_ID, DOMAIN
|
||||
from homeassistant.const import CONF_SHOW_ON_MAP
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_entry() -> MockConfigEntry:
|
||||
"""Return the default mocked config entry."""
|
||||
return MockConfigEntry(
|
||||
title="12345",
|
||||
domain=DOMAIN,
|
||||
data={CONF_SENSOR_ID: 12345, CONF_SHOW_ON_MAP: True},
|
||||
unique_id="12345",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[None]:
|
||||
"""Mock setting up a config entry."""
|
||||
with patch(
|
||||
"homeassistant.components.luftdaten.async_setup_entry", return_value=True
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_luftdaten() -> Generator[MagicMock]:
|
||||
"""Return a mocked Luftdaten client."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.luftdaten.Luftdaten", autospec=True
|
||||
) as luftdaten_mock,
|
||||
patch(
|
||||
"homeassistant.components.luftdaten.config_flow.Luftdaten",
|
||||
new=luftdaten_mock,
|
||||
),
|
||||
):
|
||||
luftdaten = luftdaten_mock.return_value
|
||||
luftdaten.validate_sensor.return_value = True
|
||||
luftdaten.sensor_id = 12345
|
||||
luftdaten.meta = {
|
||||
"altitude": 123.456,
|
||||
"latitude": 56.789,
|
||||
"longitude": 12.345,
|
||||
"sensor_id": 12345,
|
||||
}
|
||||
luftdaten.values = {
|
||||
"humidity": 34.70,
|
||||
"P1": 8.5,
|
||||
"P2": 4.07,
|
||||
"pressure_at_sealevel": 103102.13,
|
||||
"pressure": 98545.00,
|
||||
"temperature": 22.30,
|
||||
}
|
||||
yield luftdaten
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def init_integration(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_luftdaten: MagicMock
|
||||
) -> MockConfigEntry:
|
||||
"""Set up the Luftdaten integration for testing."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return mock_config_entry
|
||||
133
home-assistant/tests/components/luftdaten/test_config_flow.py
Normal file
133
home-assistant/tests/components/luftdaten/test_config_flow.py
Normal file
@@ -0,0 +1,133 @@
|
||||
"""Define tests for the Luftdaten config flow."""
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from luftdaten.exceptions import LuftdatenConnectionError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.luftdaten import DOMAIN
|
||||
from homeassistant.components.luftdaten.const import CONF_SENSOR_ID
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_SHOW_ON_MAP
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_duplicate_error(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result.get("type") is FlowResultType.FORM
|
||||
assert result.get("step_id") == "user"
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_SENSOR_ID: 12345},
|
||||
)
|
||||
|
||||
assert result2.get("type") is FlowResultType.ABORT
|
||||
assert result2.get("reason") == "already_configured"
|
||||
|
||||
|
||||
async def test_communication_error(
|
||||
hass: HomeAssistant, mock_luftdaten: MagicMock
|
||||
) -> None:
|
||||
"""Test that no sensor is added while unable to communicate with API."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result.get("type") is FlowResultType.FORM
|
||||
assert result.get("step_id") == "user"
|
||||
|
||||
mock_luftdaten.get_data.side_effect = LuftdatenConnectionError
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_SENSOR_ID: 12345},
|
||||
)
|
||||
|
||||
assert result2.get("type") is FlowResultType.FORM
|
||||
assert result2.get("step_id") == "user"
|
||||
assert result2.get("errors") == {CONF_SENSOR_ID: "cannot_connect"}
|
||||
|
||||
mock_luftdaten.get_data.side_effect = None
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
user_input={CONF_SENSOR_ID: 12345},
|
||||
)
|
||||
|
||||
assert result3.get("type") is FlowResultType.CREATE_ENTRY
|
||||
assert result3.get("title") == "12345"
|
||||
assert result3.get("data") == {
|
||||
CONF_SENSOR_ID: 12345,
|
||||
CONF_SHOW_ON_MAP: False,
|
||||
}
|
||||
|
||||
|
||||
async def test_invalid_sensor(hass: HomeAssistant, mock_luftdaten: MagicMock) -> None:
|
||||
"""Test that an invalid sensor throws an error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result.get("type") is FlowResultType.FORM
|
||||
assert result.get("step_id") == "user"
|
||||
|
||||
mock_luftdaten.validate_sensor.return_value = False
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_SENSOR_ID: 11111},
|
||||
)
|
||||
|
||||
assert result2.get("type") is FlowResultType.FORM
|
||||
assert result2.get("step_id") == "user"
|
||||
assert result2.get("errors") == {CONF_SENSOR_ID: "invalid_sensor"}
|
||||
|
||||
mock_luftdaten.validate_sensor.return_value = True
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
user_input={CONF_SENSOR_ID: 12345},
|
||||
)
|
||||
|
||||
assert result3.get("type") is FlowResultType.CREATE_ENTRY
|
||||
assert result3.get("title") == "12345"
|
||||
assert result3.get("data") == {
|
||||
CONF_SENSOR_ID: 12345,
|
||||
CONF_SHOW_ON_MAP: False,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_setup_entry", "mock_luftdaten")
|
||||
async def test_step_user(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Test that the user step works."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result.get("type") is FlowResultType.FORM
|
||||
assert result.get("step_id") == "user"
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_SENSOR_ID: 12345,
|
||||
CONF_SHOW_ON_MAP: True,
|
||||
},
|
||||
)
|
||||
|
||||
assert result2.get("type") is FlowResultType.CREATE_ENTRY
|
||||
assert result2.get("title") == "12345"
|
||||
assert result2.get("data") == {
|
||||
CONF_SENSOR_ID: 12345,
|
||||
CONF_SHOW_ON_MAP: True,
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
"""Tests for the diagnostics data provided by the Sensor.Community integration."""
|
||||
|
||||
from homeassistant.components.diagnostics import REDACTED
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
|
||||
async def test_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
init_integration: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test diagnostics."""
|
||||
assert await get_diagnostics_for_config_entry(
|
||||
hass, hass_client, init_integration
|
||||
) == {
|
||||
"P1": 8.5,
|
||||
"P2": 4.07,
|
||||
"altitude": 123.456,
|
||||
"humidity": 34.7,
|
||||
"latitude": REDACTED,
|
||||
"longitude": REDACTED,
|
||||
"pressure": 98545.0,
|
||||
"pressure_at_sealevel": 103102.13,
|
||||
"sensor_id": REDACTED,
|
||||
"temperature": 22.3,
|
||||
}
|
||||
76
home-assistant/tests/components/luftdaten/test_init.py
Normal file
76
home-assistant/tests/components/luftdaten/test_init.py
Normal file
@@ -0,0 +1,76 @@
|
||||
"""Tests for the Luftdaten integration."""
|
||||
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
from luftdaten.exceptions import LuftdatenError
|
||||
|
||||
from homeassistant.components.luftdaten.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_load_unload_config_entry(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_luftdaten: AsyncMock,
|
||||
) -> None:
|
||||
"""Test the Luftdaten configuration entry loading/unloading."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert not hass.data.get(DOMAIN)
|
||||
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
|
||||
|
||||
@patch(
|
||||
"homeassistant.components.luftdaten.Luftdaten.get_data",
|
||||
side_effect=LuftdatenError,
|
||||
)
|
||||
async def test_config_entry_not_ready(
|
||||
mock_get_data: MagicMock,
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the Luftdaten configuration entry not ready."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_get_data.call_count == 1
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
async def test_config_entry_not_ready_no_data(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_luftdaten: MagicMock,
|
||||
) -> None:
|
||||
"""Test the Luftdaten configuration entry not ready."""
|
||||
mock_luftdaten.values = {}
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_luftdaten.get_data.assert_called()
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
async def test_setting_unique_id(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_luftdaten: MagicMock
|
||||
) -> None:
|
||||
"""Test we set unique ID if not set yet."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
hass.config_entries.async_update_entry(mock_config_entry, unique_id=None)
|
||||
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.unique_id == "12345"
|
||||
134
home-assistant/tests/components/luftdaten/test_sensor.py
Normal file
134
home-assistant/tests/components/luftdaten/test_sensor.py
Normal file
@@ -0,0 +1,134 @@
|
||||
"""Tests for the sensors provided by the Luftdaten integration."""
|
||||
|
||||
from homeassistant.components.luftdaten.const import DOMAIN
|
||||
from homeassistant.components.sensor import (
|
||||
ATTR_STATE_CLASS,
|
||||
SensorDeviceClass,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_FRIENDLY_NAME,
|
||||
ATTR_ICON,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||
PERCENTAGE,
|
||||
UnitOfPressure,
|
||||
UnitOfTemperature,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_luftdaten_sensors(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
init_integration: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the Luftdaten sensors."""
|
||||
|
||||
entry = entity_registry.async_get("sensor.sensor_12345_temperature")
|
||||
assert entry
|
||||
assert entry.device_id
|
||||
assert entry.unique_id == "12345_temperature"
|
||||
|
||||
state = hass.states.get("sensor.sensor_12345_temperature")
|
||||
assert state
|
||||
assert state.state == "22.3"
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Sensor 12345 Temperature"
|
||||
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TEMPERATURE
|
||||
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTemperature.CELSIUS
|
||||
assert ATTR_ICON not in state.attributes
|
||||
|
||||
entry = entity_registry.async_get("sensor.sensor_12345_humidity")
|
||||
assert entry
|
||||
assert entry.device_id
|
||||
assert entry.unique_id == "12345_humidity"
|
||||
|
||||
state = hass.states.get("sensor.sensor_12345_humidity")
|
||||
assert state
|
||||
assert state.state == "34.7"
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Sensor 12345 Humidity"
|
||||
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.HUMIDITY
|
||||
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
|
||||
assert ATTR_ICON not in state.attributes
|
||||
|
||||
entry = entity_registry.async_get("sensor.sensor_12345_pressure")
|
||||
assert entry
|
||||
assert entry.device_id
|
||||
assert entry.unique_id == "12345_pressure"
|
||||
|
||||
state = hass.states.get("sensor.sensor_12345_pressure")
|
||||
assert state
|
||||
assert state.state == "98545.0"
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Sensor 12345 Pressure"
|
||||
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE
|
||||
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPressure.PA
|
||||
assert ATTR_ICON not in state.attributes
|
||||
|
||||
entry = entity_registry.async_get("sensor.sensor_12345_pressure_at_sealevel")
|
||||
assert entry
|
||||
assert entry.device_id
|
||||
assert entry.unique_id == "12345_pressure_at_sealevel"
|
||||
|
||||
state = hass.states.get("sensor.sensor_12345_pressure_at_sealevel")
|
||||
assert state
|
||||
assert state.state == "103102.13"
|
||||
assert (
|
||||
state.attributes.get(ATTR_FRIENDLY_NAME) == "Sensor 12345 Pressure at sealevel"
|
||||
)
|
||||
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE
|
||||
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPressure.PA
|
||||
assert ATTR_ICON not in state.attributes
|
||||
|
||||
entry = entity_registry.async_get("sensor.sensor_12345_pm10")
|
||||
assert entry
|
||||
assert entry.device_id
|
||||
assert entry.unique_id == "12345_P1"
|
||||
|
||||
state = hass.states.get("sensor.sensor_12345_pm10")
|
||||
assert state
|
||||
assert state.state == "8.5"
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Sensor 12345 PM10"
|
||||
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PM10
|
||||
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
|
||||
assert (
|
||||
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||
== CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
|
||||
)
|
||||
assert ATTR_ICON not in state.attributes
|
||||
|
||||
entry = entity_registry.async_get("sensor.sensor_12345_pm2_5")
|
||||
assert entry
|
||||
assert entry.device_id
|
||||
assert entry.unique_id == "12345_P2"
|
||||
|
||||
state = hass.states.get("sensor.sensor_12345_pm2_5")
|
||||
assert state
|
||||
assert state.state == "4.07"
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Sensor 12345 PM2.5"
|
||||
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PM25
|
||||
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
|
||||
assert (
|
||||
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||
== CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
|
||||
)
|
||||
assert ATTR_ICON not in state.attributes
|
||||
|
||||
assert entry.device_id
|
||||
device_entry = device_registry.async_get(entry.device_id)
|
||||
assert device_entry
|
||||
assert device_entry.identifiers == {(DOMAIN, "12345")}
|
||||
assert device_entry.manufacturer == "Sensor.Community"
|
||||
assert device_entry.name == "Sensor 12345"
|
||||
assert (
|
||||
device_entry.configuration_url
|
||||
== "https://devices.sensor.community/sensors/12345/settings"
|
||||
)
|
||||
Reference in New Issue
Block a user