Add Huawei LTE reboot and clear traffic statistics services (#29594)

* Add clear traffic statistics service

* Add reboot service

* Register services as admin ones

* Make URL optional when there's only one router configured

* Eliminate one if/else indent level

* Remove unnecessary .keys() with sorted()
This commit is contained in:
Ville Skyttä 2019-12-08 13:21:48 +02:00 committed by Paulus Schoutsen
parent d752fe3033
commit ef4515ed70
3 changed files with 53 additions and 0 deletions

View file

@ -58,6 +58,8 @@ from .const import (
KEY_MONITORING_STATUS,
KEY_MONITORING_TRAFFIC_STATISTICS,
KEY_WLAN_HOST_LIST,
SERVICE_CLEAR_TRAFFIC_STATISTICS,
SERVICE_REBOOT,
UPDATE_OPTIONS_SIGNAL,
UPDATE_SIGNAL,
)
@ -103,6 +105,8 @@ CONFIG_SCHEMA = vol.Schema(
extra=vol.ALLOW_EXTRA,
)
SERVICE_SCHEMA = vol.Schema({vol.Optional(CONF_URL): cv.url})
CONFIG_ENTRY_PLATFORMS = (
BINARY_SENSOR_DOMAIN,
DEVICE_TRACKER_DOMAIN,
@ -387,6 +391,39 @@ async def async_setup(hass: HomeAssistantType, config) -> bool:
for router_config in config.get(DOMAIN, []):
domain_config[url_normalize(router_config.pop(CONF_URL))] = router_config
def service_handler(service) -> None:
"""Apply a service."""
url = service.data.get(CONF_URL)
routers = hass.data[DOMAIN].routers
if url:
router = routers.get(url)
elif len(routers) == 1:
router = next(iter(routers.values()))
else:
_LOGGER.error(
"%s: more than one router configured, must specify one of URLs %s",
service.service,
sorted(routers),
)
return
if not router:
_LOGGER.error("%s: router %s unavailable", service.service, url)
return
if service.service == SERVICE_CLEAR_TRAFFIC_STATISTICS:
result = router.client.monitoring.set_clear_traffic()
_LOGGER.debug("%s: %s", service.service, result)
elif service.service == SERVICE_REBOOT:
result = router.client.device.reboot()
_LOGGER.debug("%s: %s", service.service, result)
else:
_LOGGER.error("%s: unsupported service", service.service)
for service in (SERVICE_CLEAR_TRAFFIC_STATISTICS, SERVICE_REBOOT):
hass.helpers.service.async_register_admin_service(
DOMAIN, service, service_handler, schema=SERVICE_SCHEMA,
)
for url, router_config in domain_config.items():
hass.async_create_task(
hass.config_entries.flow.async_init(

View file

@ -12,6 +12,9 @@ UNIT_SECONDS = "s"
CONNECTION_TIMEOUT = 10
SERVICE_CLEAR_TRAFFIC_STATISTICS = "clear_traffic_statistics"
SERVICE_REBOOT = "reboot"
KEY_DEVICE_BASIC_INFORMATION = "device_basic_information"
KEY_DEVICE_INFORMATION = "device_information"
KEY_DEVICE_SIGNAL = "device_signal"

View file

@ -0,0 +1,13 @@
clear_traffic_statistics:
description: Clear traffic statistics.
fields:
url:
description: URL of router to clear; optional when only one is configured.
example: http://192.168.100.1/
reboot:
description: Reboot router.
fields:
url:
description: URL of router to reboot; optional when only one is configured.
example: http://192.168.100.1/