From 57848cdf35256ce055d94987a548d16b37f8272e Mon Sep 17 00:00:00 2001
From: "J. Nick Koston" <nick@koston.org>
Date: Fri, 28 Aug 2020 12:40:30 -0500
Subject: [PATCH] Add the ability to reload ping platforms from yaml (#39344)

---
 homeassistant/components/ping/__init__.py     |  3 ++
 .../components/ping/binary_sensor.py          |  4 ++
 homeassistant/components/ping/services.yaml   |  2 +
 requirements_test_all.txt                     |  3 ++
 tests/components/ping/__init__.py             |  1 +
 tests/components/ping/test_binary_sensor.py   | 53 +++++++++++++++++++
 tests/fixtures/ping/configuration.yaml        |  5 ++
 7 files changed, 71 insertions(+)
 create mode 100644 homeassistant/components/ping/services.yaml
 create mode 100644 tests/components/ping/__init__.py
 create mode 100644 tests/components/ping/test_binary_sensor.py
 create mode 100644 tests/fixtures/ping/configuration.yaml

diff --git a/homeassistant/components/ping/__init__.py b/homeassistant/components/ping/__init__.py
index e55f13dc717..d5ec35276cf 100644
--- a/homeassistant/components/ping/__init__.py
+++ b/homeassistant/components/ping/__init__.py
@@ -1 +1,4 @@
 """The ping component."""
+
+DOMAIN = "ping"
+PLATFORMS = ["binary_sensor"]
diff --git a/homeassistant/components/ping/binary_sensor.py b/homeassistant/components/ping/binary_sensor.py
index 535c5bcd6a2..cb0d025f65d 100644
--- a/homeassistant/components/ping/binary_sensor.py
+++ b/homeassistant/components/ping/binary_sensor.py
@@ -12,7 +12,9 @@ import voluptuous as vol
 from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity
 from homeassistant.const import CONF_HOST, CONF_NAME
 import homeassistant.helpers.config_validation as cv
+from homeassistant.helpers.reload import setup_reload_service
 
+from . import DOMAIN, PLATFORMS
 from .const import PING_TIMEOUT
 
 _LOGGER = logging.getLogger(__name__)
@@ -56,6 +58,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
 
 def setup_platform(hass, config, add_entities, discovery_info=None) -> None:
     """Set up the Ping Binary sensor."""
+    setup_reload_service(hass, DOMAIN, PLATFORMS)
+
     host = config[CONF_HOST]
     count = config[CONF_PING_COUNT]
     name = config.get(CONF_NAME, f"{DEFAULT_NAME} {host}")
diff --git a/homeassistant/components/ping/services.yaml b/homeassistant/components/ping/services.yaml
new file mode 100644
index 00000000000..e2da0c28627
--- /dev/null
+++ b/homeassistant/components/ping/services.yaml
@@ -0,0 +1,2 @@
+reload:
+  description: Reload all ping entities.
diff --git a/requirements_test_all.txt b/requirements_test_all.txt
index 2ea965c28b6..511776e5f21 100644
--- a/requirements_test_all.txt
+++ b/requirements_test_all.txt
@@ -391,6 +391,9 @@ huawei-lte-api==1.4.12
 # homeassistant.components.iaqualink
 iaqualink==0.3.4
 
+# homeassistant.components.ping
+icmplib==1.1.1
+
 # homeassistant.components.influxdb
 influxdb-client==1.8.0
 
diff --git a/tests/components/ping/__init__.py b/tests/components/ping/__init__.py
new file mode 100644
index 00000000000..3d695fea171
--- /dev/null
+++ b/tests/components/ping/__init__.py
@@ -0,0 +1 @@
+"""Tests for ping component."""
diff --git a/tests/components/ping/test_binary_sensor.py b/tests/components/ping/test_binary_sensor.py
new file mode 100644
index 00000000000..ae6362939a7
--- /dev/null
+++ b/tests/components/ping/test_binary_sensor.py
@@ -0,0 +1,53 @@
+"""The test for the ping binary_sensor platform."""
+from os import path
+
+from homeassistant import config as hass_config, setup
+from homeassistant.components.ping import DOMAIN
+from homeassistant.const import SERVICE_RELOAD
+
+from tests.async_mock import patch
+
+
+async def test_reload(hass):
+    """Verify we can reload trend sensors."""
+
+    await setup.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()
+
+    assert len(hass.states.async_all()) == 1
+
+    assert hass.states.get("binary_sensor.test")
+
+    yaml_path = path.join(
+        _get_fixtures_base_path(),
+        "fixtures",
+        "ping/configuration.yaml",
+    )
+    with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
+        await hass.services.async_call(
+            DOMAIN,
+            SERVICE_RELOAD,
+            {},
+            blocking=True,
+        )
+        await hass.async_block_till_done()
+
+    assert len(hass.states.async_all()) == 1
+
+    assert hass.states.get("binary_sensor.test") is None
+    assert hass.states.get("binary_sensor.test2")
+
+
+def _get_fixtures_base_path():
+    return path.dirname(path.dirname(path.dirname(__file__)))
diff --git a/tests/fixtures/ping/configuration.yaml b/tests/fixtures/ping/configuration.yaml
new file mode 100644
index 00000000000..201c020835e
--- /dev/null
+++ b/tests/fixtures/ping/configuration.yaml
@@ -0,0 +1,5 @@
+binary_sensor:
+  - platform: ping
+    name: test2
+    host: 127.0.0.1
+    count: 1
-- 
GitLab