Add the ability to reload ping platforms from yaml (#39344)

pull/39323/head
J. Nick Koston 2020-08-28 12:40:30 -05:00 committed by GitHub
parent 85869be2d8
commit 57848cdf35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 71 additions and 0 deletions

View File

@ -1 +1,4 @@
"""The ping component."""
DOMAIN = "ping"
PLATFORMS = ["binary_sensor"]

View File

@ -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}")

View File

@ -0,0 +1,2 @@
reload:
description: Reload all ping entities.

View File

@ -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

View File

@ -0,0 +1 @@
"""Tests for ping component."""

View 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__)))

View File

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