diff --git a/.coveragerc b/.coveragerc index 40fcd50f57f..97ee86f1d44 100644 --- a/.coveragerc +++ b/.coveragerc @@ -947,9 +947,6 @@ omit = homeassistant/components/pilight/light.py homeassistant/components/pilight/switch.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/pioneer/media_player.py homeassistant/components/plaato/__init__.py diff --git a/tests/components/ping/conftest.py b/tests/components/ping/conftest.py index ded562b81d6..4ad06a09c1c 100644 --- a/tests/components/ping/conftest.py +++ b/tests/components/ping/conftest.py @@ -1,8 +1,16 @@ """Test configuration for ping.""" from unittest.mock import patch +from icmplib import Host 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 def patch_setup(*args, **kwargs): @@ -12,3 +20,35 @@ def patch_setup(*args, **kwargs): return_value=True, ), patch("homeassistant.components.ping.async_setup", return_value=True): 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() diff --git a/tests/components/ping/fixtures/configuration.yaml b/tests/components/ping/fixtures/configuration.yaml deleted file mode 100644 index 201c020835e..00000000000 --- a/tests/components/ping/fixtures/configuration.yaml +++ /dev/null @@ -1,5 +0,0 @@ -binary_sensor: - - platform: ping - name: test2 - host: 127.0.0.1 - count: 1 diff --git a/tests/components/ping/snapshots/test_binary_sensor.ambr b/tests/components/ping/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000..2ce320d561b --- /dev/null +++ b/tests/components/ping/snapshots/test_binary_sensor.ambr @@ -0,0 +1,121 @@ +# serializer version: 1 +# name: test_sensor + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + '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': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + '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': , + 'entity_id': 'binary_sensor.10_10_10_10', + 'last_changed': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_sensor.2 + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'connectivity', + 'friendly_name': '10.10.10.10', + }), + 'context': , + 'entity_id': 'binary_sensor.10_10_10_10', + 'last_changed': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_setup_and_update + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + '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': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + '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': , + 'entity_id': 'binary_sensor.10_10_10_10', + 'last_changed': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_setup_and_update.2 + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'connectivity', + 'friendly_name': '10.10.10.10', + }), + 'context': , + 'entity_id': 'binary_sensor.10_10_10_10', + 'last_changed': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/ping/test_binary_sensor.py b/tests/components/ping/test_binary_sensor.py new file mode 100644 index 00000000000..b1066895e2b --- /dev/null +++ b/tests/components/ping/test_binary_sensor.py @@ -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 diff --git a/tests/components/ping/test_device_tracker.py b/tests/components/ping/test_device_tracker.py new file mode 100644 index 00000000000..b6cc6b42912 --- /dev/null +++ b/tests/components/ping/test_device_tracker.py @@ -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