From 4149cc9662380ed41e82519642d55743ef5899fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 10 Apr 2021 03:08:13 +0300 Subject: [PATCH] Huawei LTE cleanups (#48959) --- .../components/huawei_lte/__init__.py | 35 +--------------- .../components/huawei_lte/config_flow.py | 40 +++++++------------ homeassistant/components/huawei_lte/const.py | 1 - .../components/huawei_lte/device_tracker.py | 1 + homeassistant/components/huawei_lte/sensor.py | 11 ++--- .../components/huawei_lte/strings.json | 3 +- 6 files changed, 22 insertions(+), 69 deletions(-) diff --git a/homeassistant/components/huawei_lte/__init__.py b/homeassistant/components/huawei_lte/__init__.py index 67170aaf866..25df0f620fa 100644 --- a/homeassistant/components/huawei_lte/__init__.py +++ b/homeassistant/components/huawei_lte/__init__.py @@ -48,11 +48,7 @@ from homeassistant.helpers import ( device_registry as dr, discovery, ) -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, - dispatcher_send, -) +from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send from homeassistant.helpers.entity import Entity from homeassistant.helpers.event import async_track_time_interval from homeassistant.helpers.typing import ConfigType, HomeAssistantType @@ -82,7 +78,6 @@ from .const import ( SERVICE_REBOOT, SERVICE_RESUME_INTEGRATION, SERVICE_SUSPEND_INTEGRATION, - UPDATE_OPTIONS_SIGNAL, UPDATE_SIGNAL, ) @@ -436,11 +431,6 @@ async def async_setup_entry(hass: HomeAssistantType, config_entry: ConfigEntry) hass.data[DOMAIN].hass_config, ) - # Add config entry options update listener - router.unload_handlers.append( - config_entry.add_update_listener(async_signal_options_update) - ) - def _update_router(*_: Any) -> None: """ Update router data. @@ -492,9 +482,8 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool: def service_handler(service: ServiceCall) -> None: """Apply a service.""" - url = service.data.get(CONF_URL) routers = hass.data[DOMAIN].routers - if url: + if url := service.data.get(CONF_URL): router = routers.get(url) elif not routers: _LOGGER.error("%s: no routers configured", service.service) @@ -559,13 +548,6 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool: return True -async def async_signal_options_update( - hass: HomeAssistantType, config_entry: ConfigEntry -) -> None: - """Handle config entry options update.""" - async_dispatcher_send(hass, UPDATE_OPTIONS_SIGNAL, config_entry) - - async def async_migrate_entry( hass: HomeAssistantType, config_entry: ConfigEntry ) -> bool: @@ -631,30 +613,17 @@ class HuaweiLteBaseEntity(Entity): """Update state.""" raise NotImplementedError - async def async_update_options(self, config_entry: ConfigEntry) -> None: - """Update config entry options.""" - async def async_added_to_hass(self) -> None: """Connect to update signals.""" self._unsub_handlers.append( async_dispatcher_connect(self.hass, UPDATE_SIGNAL, self._async_maybe_update) ) - self._unsub_handlers.append( - async_dispatcher_connect( - self.hass, UPDATE_OPTIONS_SIGNAL, self._async_maybe_update_options - ) - ) async def _async_maybe_update(self, url: str) -> None: """Update state if the update signal comes from our router.""" if url == self.router.url: self.async_schedule_update_ha_state(True) - async def _async_maybe_update_options(self, config_entry: ConfigEntry) -> None: - """Update options if the update signal comes from our router.""" - if config_entry.data[CONF_URL] == self.router.url: - await self.async_update_options(config_entry) - async def async_will_remove_from_hass(self) -> None: """Invoke unsubscription handlers.""" for unsub in self._unsub_handlers: diff --git a/homeassistant/components/huawei_lte/config_flow.py b/homeassistant/components/huawei_lte/config_flow.py index 415e2ea2bc3..fc455f865fd 100644 --- a/homeassistant/components/huawei_lte/config_flow.py +++ b/homeassistant/components/huawei_lte/config_flow.py @@ -1,7 +1,6 @@ """Config flow for the Huawei LTE platform.""" from __future__ import annotations -from collections import OrderedDict import logging from typing import Any from urllib.parse import urlparse @@ -65,32 +64,21 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): return self.async_show_form( step_id="user", data_schema=vol.Schema( - OrderedDict( - ( - ( - vol.Required( - CONF_URL, - default=user_input.get( - CONF_URL, - self.context.get(CONF_URL, ""), - ), - ), - str, + { + vol.Required( + CONF_URL, + default=user_input.get( + CONF_URL, + self.context.get(CONF_URL, ""), ), - ( - vol.Optional( - CONF_USERNAME, default=user_input.get(CONF_USERNAME, "") - ), - str, - ), - ( - vol.Optional( - CONF_PASSWORD, default=user_input.get(CONF_PASSWORD, "") - ), - str, - ), - ) - ) + ): str, + vol.Optional( + CONF_USERNAME, default=user_input.get(CONF_USERNAME, "") + ): str, + vol.Optional( + CONF_PASSWORD, default=user_input.get(CONF_PASSWORD, "") + ): str, + } ), errors=errors or {}, ) diff --git a/homeassistant/components/huawei_lte/const.py b/homeassistant/components/huawei_lte/const.py index 039bab10fb9..519da09caee 100644 --- a/homeassistant/components/huawei_lte/const.py +++ b/homeassistant/components/huawei_lte/const.py @@ -6,7 +6,6 @@ DEFAULT_DEVICE_NAME = "LTE" DEFAULT_NOTIFY_SERVICE_NAME = DOMAIN UPDATE_SIGNAL = f"{DOMAIN}_update" -UPDATE_OPTIONS_SIGNAL = f"{DOMAIN}_options_update" CONNECTION_TIMEOUT = 10 NOTIFY_SUPPRESS_TIMEOUT = 30 diff --git a/homeassistant/components/huawei_lte/device_tracker.py b/homeassistant/components/huawei_lte/device_tracker.py index b042c0c2912..595221a3d84 100644 --- a/homeassistant/components/huawei_lte/device_tracker.py +++ b/homeassistant/components/huawei_lte/device_tracker.py @@ -105,6 +105,7 @@ def async_add_new_entities( def _better_snakecase(text: str) -> str: + # Awaiting https://github.com/okunishinishi/python-stringcase/pull/18 if text == text.upper(): # All uppercase to all lowercase to get http for HTTP, not h_t_t_p text = text.lower() diff --git a/homeassistant/components/huawei_lte/sensor.py b/homeassistant/components/huawei_lte/sensor.py index c6cb93f0e67..da218947457 100644 --- a/homeassistant/components/huawei_lte/sensor.py +++ b/homeassistant/components/huawei_lte/sensor.py @@ -337,11 +337,9 @@ async def async_setup_entry( router = hass.data[DOMAIN].routers[config_entry.data[CONF_URL]] sensors: list[Entity] = [] for key in SENSOR_KEYS: - items = router.data.get(key) - if not items: + if not (items := router.data.get(key)): continue - key_meta = SENSOR_META.get(key) - if key_meta: + if key_meta := SENSOR_META.get(key): if key_meta.include: items = filter(key_meta.include.search, items) if key_meta.exclude: @@ -361,10 +359,9 @@ def format_default(value: StateType) -> tuple[StateType, str | None]: unit = None if value is not None: # Clean up value and infer unit, e.g. -71dBm, 15 dB - match = re.match( + if match := re.match( r"([>=<]*)(?P.+?)\s*(?P[a-zA-Z]+)\s*$", str(value) - ) - if match: + ): try: value = float(match.group("value")) unit = match.group("unit") diff --git a/homeassistant/components/huawei_lte/strings.json b/homeassistant/components/huawei_lte/strings.json index 00994f8b0a0..4aa0278faf4 100644 --- a/homeassistant/components/huawei_lte/strings.json +++ b/homeassistant/components/huawei_lte/strings.json @@ -33,8 +33,7 @@ "init": { "data": { "name": "Notification service name (change requires restart)", - "recipient": "SMS notification recipients", - "track_new_devices": "Track new devices" + "recipient": "SMS notification recipients" } } }