Add entity tests for ping (#104168)

* Add entity tests for ping

* Remove unused param

* Use async_setup of config_entries
pull/104193/head
Jan-Philipp Benecke 2023-11-18 23:45:24 +01:00 committed by GitHub
parent 09df04fafe
commit c2e81bbafb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 312 additions and 8 deletions

View File

@ -947,9 +947,6 @@ omit =
homeassistant/components/pilight/light.py homeassistant/components/pilight/light.py
homeassistant/components/pilight/switch.py homeassistant/components/pilight/switch.py
homeassistant/components/ping/__init__.py homeassistant/components/ping/__init__.py
homeassistant/components/ping/binary_sensor.py
homeassistant/components/ping/coordinator.py
homeassistant/components/ping/device_tracker.py
homeassistant/components/ping/helpers.py homeassistant/components/ping/helpers.py
homeassistant/components/pioneer/media_player.py homeassistant/components/pioneer/media_player.py
homeassistant/components/plaato/__init__.py homeassistant/components/plaato/__init__.py

View File

@ -1,8 +1,16 @@
"""Test configuration for ping.""" """Test configuration for ping."""
from unittest.mock import patch from unittest.mock import patch
from icmplib import Host
import pytest import pytest
from homeassistant.components.ping import DOMAIN
from homeassistant.components.ping.const import CONF_PING_COUNT
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
@pytest.fixture @pytest.fixture
def patch_setup(*args, **kwargs): def patch_setup(*args, **kwargs):
@ -12,3 +20,35 @@ def patch_setup(*args, **kwargs):
return_value=True, return_value=True,
), patch("homeassistant.components.ping.async_setup", return_value=True): ), patch("homeassistant.components.ping.async_setup", return_value=True):
yield yield
@pytest.fixture(autouse=True)
async def patch_ping():
"""Patch icmplib async_ping."""
mock = Host("10.10.10.10", 5, [10, 1, 2])
with patch(
"homeassistant.components.ping.helpers.async_ping", return_value=mock
), patch("homeassistant.components.ping.async_ping", return_value=mock):
yield mock
@pytest.fixture(name="config_entry")
async def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
"""Return a MockConfigEntry for testing."""
return MockConfigEntry(
domain=DOMAIN,
title="10.10.10.10",
options={CONF_HOST: "10.10.10.10", CONF_PING_COUNT: 10.0},
)
@pytest.fixture(name="setup_integration")
async def mock_setup_integration(
hass: HomeAssistant, config_entry: MockConfigEntry, patch_ping
) -> None:
"""Fixture for setting up the component."""
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

View File

@ -1,5 +0,0 @@
binary_sensor:
- platform: ping
name: test2
host: 127.0.0.1
count: 1

View File

@ -0,0 +1,121 @@
# serializer version: 1
# name: test_sensor
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.10_10_10_10',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.CONNECTIVITY: 'connectivity'>,
'original_icon': None,
'original_name': '10.10.10.10',
'platform': 'ping',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unit_of_measurement': None,
})
# ---
# name: test_sensor.1
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'connectivity',
'friendly_name': '10.10.10.10',
'round_trip_time_avg': 4.333,
'round_trip_time_max': 10,
'round_trip_time_mdev': '',
'round_trip_time_min': 1,
}),
'context': <ANY>,
'entity_id': 'binary_sensor.10_10_10_10',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_sensor.2
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'connectivity',
'friendly_name': '10.10.10.10',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.10_10_10_10',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_setup_and_update
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.10_10_10_10',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.CONNECTIVITY: 'connectivity'>,
'original_icon': None,
'original_name': '10.10.10.10',
'platform': 'ping',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unit_of_measurement': None,
})
# ---
# name: test_setup_and_update.1
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'connectivity',
'friendly_name': '10.10.10.10',
'round_trip_time_avg': 4.333,
'round_trip_time_max': 10,
'round_trip_time_mdev': '',
'round_trip_time_min': 1,
}),
'context': <ANY>,
'entity_id': 'binary_sensor.10_10_10_10',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_setup_and_update.2
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'connectivity',
'friendly_name': '10.10.10.10',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.10_10_10_10',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---

View File

@ -0,0 +1,89 @@
"""Test the binary sensor platform of ping."""
from datetime import timedelta
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
from icmplib import Host
import pytest
from syrupy import SnapshotAssertion
from syrupy.filters import props
from homeassistant.components.ping.const import CONF_IMPORTED_BY, DOMAIN
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
from homeassistant.helpers import entity_registry as er, issue_registry as ir
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
@pytest.mark.usefixtures("setup_integration")
async def test_setup_and_update(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
freezer: FrozenDateTimeFactory,
snapshot: SnapshotAssertion,
) -> None:
"""Test sensor setup and update."""
# check if binary sensor is there
entry = entity_registry.async_get("binary_sensor.10_10_10_10")
assert entry == snapshot(exclude=props("unique_id"))
state = hass.states.get("binary_sensor.10_10_10_10")
assert state == snapshot
# check if the sensor turns off.
with patch(
"homeassistant.components.ping.helpers.async_ping",
return_value=Host(address="10.10.10.10", packets_sent=10, rtts=[]),
):
freezer.tick(timedelta(minutes=6))
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.10_10_10_10")
assert state == snapshot
async def test_disabled_after_import(
hass: HomeAssistant,
config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
):
"""Test if binary sensor is disabled after import."""
config_entry.data = {CONF_IMPORTED_BY: "device_tracker"}
config_entry.add_to_hass(hass)
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
# check if entity is disabled after import by device tracker
entry = entity_registry.async_get("binary_sensor.10_10_10_10")
assert entry
assert entry.disabled
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
async def test_import_issue_creation(
hass: HomeAssistant,
issue_registry: ir.IssueRegistry,
):
"""Test if import issue is raised."""
await async_setup_component(
hass,
"binary_sensor",
{
"binary_sensor": {
"platform": "ping",
"name": "test",
"host": "127.0.0.1",
"count": 1,
}
},
)
await hass.async_block_till_done()
issue = issue_registry.async_get_issue(
HOMEASSISTANT_DOMAIN, f"deprecated_yaml_{DOMAIN}"
)
assert issue

View File

@ -0,0 +1,62 @@
"""Test the binary sensor platform of ping."""
import pytest
from homeassistant.components.ping.const import DOMAIN
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
from homeassistant.helpers import entity_registry as er, issue_registry as ir
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
@pytest.mark.usefixtures("setup_integration")
async def test_setup_and_update(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
config_entry: MockConfigEntry,
) -> None:
"""Test sensor setup and update."""
entry = entity_registry.async_get("device_tracker.10_10_10_10")
assert entry
assert entry.disabled
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
# check device tracker state is not there
state = hass.states.get("device_tracker.10_10_10_10")
assert state is None
# enable the entity
updated_entry = entity_registry.async_update_entity(
entity_id="device_tracker.10_10_10_10", disabled_by=None
)
assert updated_entry != entry
assert updated_entry.disabled is False
# reload config entry to enable entity
await hass.config_entries.async_reload(config_entry.entry_id)
await hass.async_block_till_done()
# check device tracker is now "home"
state = hass.states.get("device_tracker.10_10_10_10")
assert state.state == "home"
async def test_import_issue_creation(
hass: HomeAssistant,
issue_registry: ir.IssueRegistry,
):
"""Test if import issue is raised."""
await async_setup_component(
hass,
"device_tracker",
{"device_tracker": {"platform": "ping", "hosts": {"test": "10.10.10.10"}}},
)
await hass.async_block_till_done()
issue = issue_registry.async_get_issue(
HOMEASSISTANT_DOMAIN, f"deprecated_yaml_{DOMAIN}"
)
assert issue