Modernize Huawei LTE (#26675)

* Modernization rework

- config entry support, with override support from huawei_lte platform in YAML
- device tracker entity registry support
- refactor for easier addition of more features
- internal code cleanups

* Remove log level dependent subscription/data debug hack

No longer needed, because pretty much all keys from supported
categories are exposed as sensors.

Closes https://github.com/home-assistant/home-assistant/issues/23819

* Upgrade huawei-lte-api to 1.4.1

https://github.com/Salamek/huawei-lte-api/releases

* Add support for access without username and password

* Use subclass init instead of config_entries.HANDLERS

* Update huawei-lte-api to 1.4.3 (#27269)

* Convert device state attributes to snake_case

* Simplify scanner entity initialization

* Remove not needed hass reference from Router

* Return explicit None from unsupported old device tracker setup

* Mark unknown connection errors during config as such

* Drop some dead config flow code

* Run config flow sync I/O in executor

* Parametrize config flow login error tests

* Forward entry unload to platforms

* Async/sync fixups

* Improve data subscription debug logging

* Implement on the fly add of new and tracking of seen device tracker entities

* Handle device tracker entry unload cleanup in component

* Remove unnecessary _async_setup_lte, just have code in async_setup_entry

* Remove time tracker on unload

* Fix to not use same mutable default subscription set for all routers

* Pylint fixes

* Remove some redundant defensive device tracker code

* Add back explicit get_scanner None return, hush pylint

* Adjust approach to set system_options on entry create

* Enable some sensors on first add instead of disabling everything

* Fix SMS notification recipients default value

* Add option to skip new device tracker entities

* Fix SMS notification recipient option default

* Work around https://github.com/PyCQA/pylint/issues/3202

* Remove unrelated type hint additions

* Change async_add_new_entities to a regular function

* Remove option to disable polling for new device tracker entries
This commit is contained in:
Ville Skyttä authored and GitHub committed 2019-10-24 19:31:49 +03:00
1 parent 969322e14a
commit fc09702cc3
15 files changed
+1067 -276

No files matched your search

+22 -26
View File
@@ -1,58 +1,54 @@
"""Support for Huawei LTE router notifications."""
import logging
from typing import Any, List
import voluptuous as vol
import attr
from huawei_lte_api.exceptions import ResponseErrorException
from homeassistant.components.notify import (
BaseNotificationService,
ATTR_TARGET,
PLATFORM_SCHEMA,
)
from homeassistant.components.notify import BaseNotificationService, ATTR_TARGET
from homeassistant.const import CONF_RECIPIENT, CONF_URL
import homeassistant.helpers.config_validation as cv
from . import Router
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_URL): cv.url,
vol.Required(CONF_RECIPIENT): vol.All(cv.ensure_list, [cv.string]),
}
)
async def async_get_service(hass, config, discovery_info=None):
"""Get the notification service."""
return HuaweiLteSmsNotificationService(hass, config)
if discovery_info is None:
_LOGGER.warning(
"Loading as a platform is no longer supported, convert to use "
"config entries or the huawei_lte component"
)
return None
router = hass.data[DOMAIN].routers[discovery_info[CONF_URL]]
default_targets = discovery_info[CONF_RECIPIENT] or []
return HuaweiLteSmsNotificationService(router, default_targets)
@attr.s
class HuaweiLteSmsNotificationService(BaseNotificationService):
"""Huawei LTE router SMS notification service."""
hass = attr.ib()
config = attr.ib()
router: Router = attr.ib()
default_targets: List[str] = attr.ib()
def send_message(self, message="", **kwargs):
def send_message(self, message: str = "", **kwargs: Any) -> None:
"""Send message to target numbers."""
from huawei_lte_api.exceptions import ResponseErrorException
targets = kwargs.get(ATTR_TARGET, self.config.get(CONF_RECIPIENT))
targets = kwargs.get(ATTR_TARGET, self.default_targets)
if not targets or not message:
return
data = self.hass.data[DOMAIN].get_data(self.config)
if not data:
_LOGGER.error("Router not available")
return
try:
resp = data.client.sms.send_sms(phone_numbers=targets, message=message)
resp = self.router.client.sms.send_sms(
phone_numbers=targets, message=message
)
_LOGGER.debug("Sent to %s: %s", targets, resp)
except ResponseErrorException as ex:
_LOGGER.error("Could not send to %s: %s", targets, ex)