Use new enums in huawei-lte (#61771)

Co-authored-by: epenet <epenet@users.noreply.github.com>
pull/61791/head
epenet 2021-12-14 12:59:00 +01:00 committed by GitHub
parent 96e54ca939
commit cec43fe868
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 68 additions and 72 deletions

View File

@ -10,26 +10,22 @@ from typing import NamedTuple
import attr
from homeassistant.components.sensor import (
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_SIGNAL_STRENGTH,
DOMAIN as SENSOR_DOMAIN,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
DATA_BYTES,
DATA_RATE_BYTES_PER_SECOND,
ENTITY_CATEGORY_CONFIG,
ENTITY_CATEGORY_DIAGNOSTIC,
FREQUENCY_MEGAHERTZ,
PERCENTAGE,
STATE_UNKNOWN,
TIME_SECONDS,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity import Entity, EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
@ -73,32 +69,32 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
(KEY_DEVICE_INFORMATION, "WanIPAddress"): SensorMeta(
name="WAN IP address",
icon="mdi:ip",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
enabled_default=True,
),
(KEY_DEVICE_INFORMATION, "WanIPv6Address"): SensorMeta(
name="WAN IPv6 address",
icon="mdi:ip",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_INFORMATION, "uptime"): SensorMeta(
name="Uptime",
icon="mdi:timer-outline",
unit=TIME_SECONDS,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "band"): SensorMeta(
name="Band",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "cell_id"): SensorMeta(
name="Cell ID",
icon="mdi:transmission-tower",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "dl_mcs"): SensorMeta(
name="Downlink MCS",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "dlbandwidth"): SensorMeta(
name="Downlink bandwidth",
@ -107,47 +103,47 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
"mdi:speedometer-medium",
"mdi:speedometer",
)[bisect((8, 15), x if x is not None else -1000)],
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "earfcn"): SensorMeta(
name="EARFCN",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "lac"): SensorMeta(
name="LAC",
icon="mdi:map-marker",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "plmn"): SensorMeta(
name="PLMN",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "rac"): SensorMeta(
name="RAC",
icon="mdi:map-marker",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "rrc_status"): SensorMeta(
name="RRC status",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "tac"): SensorMeta(
name="TAC",
icon="mdi:map-marker",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "tdd"): SensorMeta(
name="TDD",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "txpower"): SensorMeta(
name="Transmit power",
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "ul_mcs"): SensorMeta(
name="Uplink MCS",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "ulbandwidth"): SensorMeta(
name="Uplink bandwidth",
@ -156,7 +152,7 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
"mdi:speedometer-medium",
"mdi:speedometer",
)[bisect((8, 15), x if x is not None else -1000)],
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "mode"): SensorMeta(
name="Mode",
@ -166,16 +162,16 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
str(x), "mdi:signal"
)
),
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "pci"): SensorMeta(
name="PCI",
icon="mdi:transmission-tower",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "rsrq"): SensorMeta(
name="RSRQ",
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
# http://www.lte-anbieter.info/technik/rsrq.php
icon=lambda x: (
"mdi:signal-cellular-outline",
@ -183,13 +179,13 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
"mdi:signal-cellular-2",
"mdi:signal-cellular-3",
)[bisect((-11, -8, -5), x if x is not None else -1000)],
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
enabled_default=True,
),
(KEY_DEVICE_SIGNAL, "rsrp"): SensorMeta(
name="RSRP",
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
# http://www.lte-anbieter.info/technik/rsrp.php
icon=lambda x: (
"mdi:signal-cellular-outline",
@ -197,13 +193,13 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
"mdi:signal-cellular-2",
"mdi:signal-cellular-3",
)[bisect((-110, -95, -80), x if x is not None else -1000)],
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
enabled_default=True,
),
(KEY_DEVICE_SIGNAL, "rssi"): SensorMeta(
name="RSSI",
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
# https://eyesaas.com/wi-fi-signal-strength/
icon=lambda x: (
"mdi:signal-cellular-outline",
@ -211,13 +207,13 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
"mdi:signal-cellular-2",
"mdi:signal-cellular-3",
)[bisect((-80, -70, -60), x if x is not None else -1000)],
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
enabled_default=True,
),
(KEY_DEVICE_SIGNAL, "sinr"): SensorMeta(
name="SINR",
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
# http://www.lte-anbieter.info/technik/sinr.php
icon=lambda x: (
"mdi:signal-cellular-outline",
@ -225,13 +221,13 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
"mdi:signal-cellular-2",
"mdi:signal-cellular-3",
)[bisect((0, 5, 10), x if x is not None else -1000)],
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
enabled_default=True,
),
(KEY_DEVICE_SIGNAL, "rscp"): SensorMeta(
name="RSCP",
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
# https://wiki.teltonika.lt/view/RSCP
icon=lambda x: (
"mdi:signal-cellular-outline",
@ -239,12 +235,12 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
"mdi:signal-cellular-2",
"mdi:signal-cellular-3",
)[bisect((-95, -85, -75), x if x is not None else -1000)],
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "ecio"): SensorMeta(
name="EC/IO",
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
# https://wiki.teltonika.lt/view/EC/IO
icon=lambda x: (
"mdi:signal-cellular-outline",
@ -252,17 +248,17 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
"mdi:signal-cellular-2",
"mdi:signal-cellular-3",
)[bisect((-20, -10, -6), x if x is not None else -1000)],
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "transmode"): SensorMeta(
name="Transmission mode",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "cqi0"): SensorMeta(
name="CQI 0",
icon="mdi:speedometer",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "cqi1"): SensorMeta(
name="CQI 1",
@ -270,7 +266,7 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
),
(KEY_DEVICE_SIGNAL, "enodeb_id"): SensorMeta(
name="eNodeB ID",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "ltedlfreq"): SensorMeta(
name="Downlink frequency",
@ -278,7 +274,7 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
round(int(x) / 10) if x is not None else None,
FREQUENCY_MEGAHERTZ,
),
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_DEVICE_SIGNAL, "lteulfreq"): SensorMeta(
name="Uplink frequency",
@ -286,7 +282,7 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
round(int(x) / 10) if x is not None else None,
FREQUENCY_MEGAHERTZ,
),
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
KEY_MONITORING_CHECK_NOTIFICATIONS: SensorMeta(
exclude=re.compile(
@ -304,13 +300,13 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
name="Current month download",
unit=DATA_BYTES,
icon="mdi:download",
state_class=STATE_CLASS_TOTAL_INCREASING,
state_class=SensorStateClass.TOTAL_INCREASING,
),
(KEY_MONITORING_MONTH_STATISTICS, "CurrentMonthUpload"): SensorMeta(
name="Current month upload",
unit=DATA_BYTES,
icon="mdi:upload",
state_class=STATE_CLASS_TOTAL_INCREASING,
state_class=SensorStateClass.TOTAL_INCREASING,
),
KEY_MONITORING_STATUS: SensorMeta(
include=re.compile(
@ -320,36 +316,36 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
),
(KEY_MONITORING_STATUS, "BatteryPercent"): SensorMeta(
name="Battery",
device_class=DEVICE_CLASS_BATTERY,
device_class=SensorDeviceClass.BATTERY,
unit=PERCENTAGE,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_MONITORING_STATUS, "CurrentWifiUser"): SensorMeta(
name="WiFi clients connected",
icon="mdi:wifi",
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_MONITORING_STATUS, "PrimaryDns"): SensorMeta(
name="Primary DNS server",
icon="mdi:ip",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_MONITORING_STATUS, "SecondaryDns"): SensorMeta(
name="Secondary DNS server",
icon="mdi:ip",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_MONITORING_STATUS, "PrimaryIPv6Dns"): SensorMeta(
name="Primary IPv6 DNS server",
icon="mdi:ip",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_MONITORING_STATUS, "SecondaryIPv6Dns"): SensorMeta(
name="Secondary IPv6 DNS server",
icon="mdi:ip",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
KEY_MONITORING_TRAFFIC_STATISTICS: SensorMeta(
exclude=re.compile(r"^showtraffic$", re.IGNORECASE)
@ -361,43 +357,43 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
name="Current connection download",
unit=DATA_BYTES,
icon="mdi:download",
state_class=STATE_CLASS_TOTAL_INCREASING,
state_class=SensorStateClass.TOTAL_INCREASING,
),
(KEY_MONITORING_TRAFFIC_STATISTICS, "CurrentDownloadRate"): SensorMeta(
name="Current download rate",
unit=DATA_RATE_BYTES_PER_SECOND,
icon="mdi:download",
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
(KEY_MONITORING_TRAFFIC_STATISTICS, "CurrentUpload"): SensorMeta(
name="Current connection upload",
unit=DATA_BYTES,
icon="mdi:upload",
state_class=STATE_CLASS_TOTAL_INCREASING,
state_class=SensorStateClass.TOTAL_INCREASING,
),
(KEY_MONITORING_TRAFFIC_STATISTICS, "CurrentUploadRate"): SensorMeta(
name="Current upload rate",
unit=DATA_RATE_BYTES_PER_SECOND,
icon="mdi:upload",
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
(KEY_MONITORING_TRAFFIC_STATISTICS, "TotalConnectTime"): SensorMeta(
name="Total connected duration",
unit=TIME_SECONDS,
icon="mdi:timer-outline",
state_class=STATE_CLASS_TOTAL_INCREASING,
state_class=SensorStateClass.TOTAL_INCREASING,
),
(KEY_MONITORING_TRAFFIC_STATISTICS, "TotalDownload"): SensorMeta(
name="Total download",
unit=DATA_BYTES,
icon="mdi:download",
state_class=STATE_CLASS_TOTAL_INCREASING,
state_class=SensorStateClass.TOTAL_INCREASING,
),
(KEY_MONITORING_TRAFFIC_STATISTICS, "TotalUpload"): SensorMeta(
name="Total upload",
unit=DATA_BYTES,
icon="mdi:upload",
state_class=STATE_CLASS_TOTAL_INCREASING,
state_class=SensorStateClass.TOTAL_INCREASING,
),
KEY_NET_CURRENT_PLMN: SensorMeta(
exclude=re.compile(r"^(Rat|ShortName|Spn)$", re.IGNORECASE)
@ -405,15 +401,15 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
(KEY_NET_CURRENT_PLMN, "State"): SensorMeta(
name="Operator search mode",
formatter=lambda x: ({"0": "Auto", "1": "Manual"}.get(x, "Unknown"), None),
entity_category=ENTITY_CATEGORY_CONFIG,
entity_category=EntityCategory.CONFIG,
),
(KEY_NET_CURRENT_PLMN, "FullName"): SensorMeta(
name="Operator name",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
(KEY_NET_CURRENT_PLMN, "Numeric"): SensorMeta(
name="Operator code",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
KEY_NET_NET_MODE: SensorMeta(include=re.compile(r"^NetworkMode$", re.IGNORECASE)),
(KEY_NET_NET_MODE, "NetworkMode"): SensorMeta(
@ -430,7 +426,7 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
}.get(x, "Unknown"),
None,
),
entity_category=ENTITY_CATEGORY_CONFIG,
entity_category=EntityCategory.CONFIG,
),
(KEY_SMS_SMS_COUNT, "LocalDeleted"): SensorMeta(
name="SMS deleted (device)",