Add Huawei LTE binary sensor support, mobile connection sensor (#28226)

* Add Huawei LTE binary sensor support, mobile connection sensor

* Improve mobile connection sensor icon docstring

* Remove device class for permission to use a more descriptive icon
pull/29281/head
Ville Skyttä 2019-12-01 14:09:08 +08:00 committed by Paulus Schoutsen
parent 6a02c6540e
commit 5c8a8a631c
3 changed files with 138 additions and 3 deletions

View File

@ -21,6 +21,7 @@ from huawei_lte_api.exceptions import (
from requests.exceptions import Timeout from requests.exceptions import Timeout
from url_normalize import url_normalize from url_normalize import url_normalize
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER_DOMAIN from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER_DOMAIN
from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
@ -54,6 +55,7 @@ from .const import (
KEY_DEVICE_INFORMATION, KEY_DEVICE_INFORMATION,
KEY_DEVICE_SIGNAL, KEY_DEVICE_SIGNAL,
KEY_DIALUP_MOBILE_DATASWITCH, KEY_DIALUP_MOBILE_DATASWITCH,
KEY_MONITORING_STATUS,
KEY_MONITORING_TRAFFIC_STATISTICS, KEY_MONITORING_TRAFFIC_STATISTICS,
KEY_WLAN_HOST_LIST, KEY_WLAN_HOST_LIST,
UPDATE_OPTIONS_SIGNAL, UPDATE_OPTIONS_SIGNAL,
@ -101,6 +103,13 @@ CONFIG_SCHEMA = vol.Schema(
extra=vol.ALLOW_EXTRA, extra=vol.ALLOW_EXTRA,
) )
CONFIG_ENTRY_PLATFORMS = (
BINARY_SENSOR_DOMAIN,
DEVICE_TRACKER_DOMAIN,
SENSOR_DOMAIN,
SWITCH_DOMAIN,
)
@attr.s @attr.s
class Router: class Router:
@ -170,6 +179,7 @@ class Router:
get_data(KEY_DEVICE_BASIC_INFORMATION, self.client.device.basic_information) get_data(KEY_DEVICE_BASIC_INFORMATION, self.client.device.basic_information)
get_data(KEY_DEVICE_SIGNAL, self.client.device.signal) get_data(KEY_DEVICE_SIGNAL, self.client.device.signal)
get_data(KEY_DIALUP_MOBILE_DATASWITCH, self.client.dial_up.mobile_dataswitch) get_data(KEY_DIALUP_MOBILE_DATASWITCH, self.client.dial_up.mobile_dataswitch)
get_data(KEY_MONITORING_STATUS, self.client.monitoring.status)
get_data( get_data(
KEY_MONITORING_TRAFFIC_STATISTICS, self.client.monitoring.traffic_statistics KEY_MONITORING_TRAFFIC_STATISTICS, self.client.monitoring.traffic_statistics
) )
@ -314,7 +324,7 @@ async def async_setup_entry(hass: HomeAssistantType, config_entry: ConfigEntry)
) )
# Forward config entry setup to platforms # Forward config entry setup to platforms
for domain in (DEVICE_TRACKER_DOMAIN, SENSOR_DOMAIN, SWITCH_DOMAIN): for domain in CONFIG_ENTRY_PLATFORMS:
hass.async_create_task( hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, domain) hass.config_entries.async_forward_entry_setup(config_entry, domain)
) )
@ -357,7 +367,7 @@ async def async_unload_entry(
"""Unload config entry.""" """Unload config entry."""
# Forward config entry unload to platforms # Forward config entry unload to platforms
for domain in (DEVICE_TRACKER_DOMAIN, SENSOR_DOMAIN, SWITCH_DOMAIN): for domain in CONFIG_ENTRY_PLATFORMS:
await hass.config_entries.async_forward_entry_unload(config_entry, domain) await hass.config_entries.async_forward_entry_unload(config_entry, domain)
# Forget about the router and invoke its cleanup # Forget about the router and invoke its cleanup

View File

@ -0,0 +1,122 @@
"""Support for Huawei LTE binary sensors."""
import logging
from typing import Optional
import attr
from huawei_lte_api.enums.cradle import ConnectionStatusEnum
from homeassistant.components.binary_sensor import (
DOMAIN as BINARY_SENSOR_DOMAIN,
BinarySensorDevice,
)
from homeassistant.const import CONF_URL
from . import HuaweiLteBaseEntity
from .const import DOMAIN, KEY_MONITORING_STATUS
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up from config entry."""
router = hass.data[DOMAIN].routers[config_entry.data[CONF_URL]]
entities = []
if router.data.get(KEY_MONITORING_STATUS):
entities.append(HuaweiLteMobileConnectionBinarySensor(router))
async_add_entities(entities, True)
@attr.s
class HuaweiLteBaseBinarySensor(HuaweiLteBaseEntity, BinarySensorDevice):
"""Huawei LTE binary sensor device base class."""
key: str
item: str
_raw_state: Optional[str] = attr.ib(init=False, default=None)
async def async_added_to_hass(self):
"""Subscribe to needed data on add."""
await super().async_added_to_hass()
self.router.subscriptions[self.key].add(f"{BINARY_SENSOR_DOMAIN}/{self.item}")
async def async_will_remove_from_hass(self):
"""Unsubscribe from needed data on remove."""
await super().async_will_remove_from_hass()
self.router.subscriptions[self.key].remove(
f"{BINARY_SENSOR_DOMAIN}/{self.item}"
)
async def async_update(self):
"""Update state."""
try:
value = self.router.data[self.key][self.item]
except KeyError:
_LOGGER.debug("%s[%s] not in data", self.key, self.item)
self._available = False
return
self._available = True
self._raw_state = str(value)
CONNECTION_STATE_ATTRIBUTES = {
str(ConnectionStatusEnum.CONNECTING): "Connecting",
str(ConnectionStatusEnum.DISCONNECTING): "Disconnecting",
str(ConnectionStatusEnum.CONNECT_FAILED): "Connect failed",
str(ConnectionStatusEnum.CONNECT_STATUS_NULL): "Status not available",
str(ConnectionStatusEnum.CONNECT_STATUS_ERROR): "Status error",
}
@attr.s
class HuaweiLteMobileConnectionBinarySensor(HuaweiLteBaseBinarySensor):
"""Huawei LTE mobile connection binary sensor."""
def __attrs_post_init__(self):
"""Initialize identifiers."""
self.key = KEY_MONITORING_STATUS
self.item = "ConnectionStatus"
@property
def _entity_name(self) -> str:
return "Mobile connection"
@property
def _device_unique_id(self) -> str:
return f"{self.key}.{self.item}"
@property
def is_on(self) -> bool:
"""Return whether the binary sensor is on."""
return self._raw_state and int(self._raw_state) in (
ConnectionStatusEnum.CONNECTED,
ConnectionStatusEnum.DISCONNECTING,
)
@property
def assumed_state(self) -> bool:
"""Return True if real state is assumed, not known."""
return not self._raw_state or int(self._raw_state) not in (
ConnectionStatusEnum.CONNECT_FAILED,
ConnectionStatusEnum.CONNECTED,
ConnectionStatusEnum.DISCONNECTED,
)
@property
def icon(self):
"""Return mobile connectivity sensor icon."""
return "mdi:signal" if self.is_on else "mdi:signal-off"
@property
def device_state_attributes(self):
"""Get additional attributes related to connection status."""
attributes = super().device_state_attributes
if self._raw_state in CONNECTION_STATE_ATTRIBUTES:
if attributes is None:
attributes = {}
attributes["additional_state"] = CONNECTION_STATE_ATTRIBUTES[
self._raw_state
]
return attributes

View File

@ -16,9 +16,12 @@ KEY_DEVICE_BASIC_INFORMATION = "device_basic_information"
KEY_DEVICE_INFORMATION = "device_information" KEY_DEVICE_INFORMATION = "device_information"
KEY_DEVICE_SIGNAL = "device_signal" KEY_DEVICE_SIGNAL = "device_signal"
KEY_DIALUP_MOBILE_DATASWITCH = "dialup_mobile_dataswitch" KEY_DIALUP_MOBILE_DATASWITCH = "dialup_mobile_dataswitch"
KEY_MONITORING_STATUS = "monitoring_status"
KEY_MONITORING_TRAFFIC_STATISTICS = "monitoring_traffic_statistics" KEY_MONITORING_TRAFFIC_STATISTICS = "monitoring_traffic_statistics"
KEY_WLAN_HOST_LIST = "wlan_host_list" KEY_WLAN_HOST_LIST = "wlan_host_list"
BINARY_SENSOR_KEYS = {KEY_MONITORING_STATUS}
DEVICE_TRACKER_KEYS = {KEY_WLAN_HOST_LIST} DEVICE_TRACKER_KEYS = {KEY_WLAN_HOST_LIST}
SENSOR_KEYS = { SENSOR_KEYS = {
@ -29,4 +32,4 @@ SENSOR_KEYS = {
SWITCH_KEYS = {KEY_DIALUP_MOBILE_DATASWITCH} SWITCH_KEYS = {KEY_DIALUP_MOBILE_DATASWITCH}
ALL_KEYS = DEVICE_TRACKER_KEYS | SENSOR_KEYS | SWITCH_KEYS ALL_KEYS = BINARY_SENSOR_KEYS | DEVICE_TRACKER_KEYS | SENSOR_KEYS | SWITCH_KEYS