diff --git a/homeassistant/components/ping/__init__.py b/homeassistant/components/ping/__init__.py index e55f13dc7177e0ccd39ddbd7acfc8ae237814ab0..d5ec35276cf5c361f3701306ceff53cc2cb091dc 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 535c5bcd6a217a881567a26838c3a8fba6ca9485..cb0d025f65def046f69c51c574db04255a975a85 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 0000000000000000000000000000000000000000..e2da0c286271f1bd2ffc3348052d990e91ab1bca --- /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 2ea965c28b6dd3c1ab544f28ed67492b2fd18993..511776e5f21c95ff6d28e8711593d9d1bf753cf6 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 0000000000000000000000000000000000000000..3d695fea17117c52ee85fd460f2306772bd119dd --- /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 0000000000000000000000000000000000000000..ae6362939a7988ff6fc998911dae2a315f5f6f8f --- /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 0000000000000000000000000000000000000000..201c020835e821e4a3e04a561341b09ae9e29d24 --- /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