Add the ability to reload ping platforms from yaml (#39344)
This commit is contained in:
parent
85869be2d8
commit
57848cdf35
7 changed files with 71 additions and 0 deletions
|
@ -1 +1,4 @@
|
||||||
"""The ping component."""
|
"""The ping component."""
|
||||||
|
|
||||||
|
DOMAIN = "ping"
|
||||||
|
PLATFORMS = ["binary_sensor"]
|
||||||
|
|
|
@ -12,7 +12,9 @@ import voluptuous as vol
|
||||||
from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity
|
from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity
|
||||||
from homeassistant.const import CONF_HOST, CONF_NAME
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.reload import setup_reload_service
|
||||||
|
|
||||||
|
from . import DOMAIN, PLATFORMS
|
||||||
from .const import PING_TIMEOUT
|
from .const import PING_TIMEOUT
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -56,6 +58,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None) -> None:
|
def setup_platform(hass, config, add_entities, discovery_info=None) -> None:
|
||||||
"""Set up the Ping Binary sensor."""
|
"""Set up the Ping Binary sensor."""
|
||||||
|
setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||||
|
|
||||||
host = config[CONF_HOST]
|
host = config[CONF_HOST]
|
||||||
count = config[CONF_PING_COUNT]
|
count = config[CONF_PING_COUNT]
|
||||||
name = config.get(CONF_NAME, f"{DEFAULT_NAME} {host}")
|
name = config.get(CONF_NAME, f"{DEFAULT_NAME} {host}")
|
||||||
|
|
2
homeassistant/components/ping/services.yaml
Normal file
2
homeassistant/components/ping/services.yaml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
reload:
|
||||||
|
description: Reload all ping entities.
|
|
@ -391,6 +391,9 @@ huawei-lte-api==1.4.12
|
||||||
# homeassistant.components.iaqualink
|
# homeassistant.components.iaqualink
|
||||||
iaqualink==0.3.4
|
iaqualink==0.3.4
|
||||||
|
|
||||||
|
# homeassistant.components.ping
|
||||||
|
icmplib==1.1.1
|
||||||
|
|
||||||
# homeassistant.components.influxdb
|
# homeassistant.components.influxdb
|
||||||
influxdb-client==1.8.0
|
influxdb-client==1.8.0
|
||||||
|
|
||||||
|
|
1
tests/components/ping/__init__.py
Normal file
1
tests/components/ping/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
"""Tests for ping component."""
|
53
tests/components/ping/test_binary_sensor.py
Normal file
53
tests/components/ping/test_binary_sensor.py
Normal file
|
@ -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__)))
|
5
tests/fixtures/ping/configuration.yaml
vendored
Normal file
5
tests/fixtures/ping/configuration.yaml
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
binary_sensor:
|
||||||
|
- platform: ping
|
||||||
|
name: test2
|
||||||
|
host: 127.0.0.1
|
||||||
|
count: 1
|
Loading…
Add table
Add a link
Reference in a new issue