mirror of
https://github.com/home-assistant/core
synced 2024-07-21 02:25:24 +00:00
Set entity category and device class for Netgear LTE entities (#106661)
* Set entity category and device class for Netgear * add suggested unit of measure and precision
This commit is contained in:
parent
a4f0c84457
commit
7396bc61d7
|
@ -7,6 +7,7 @@ from homeassistant.components.binary_sensor import (
|
|||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
|
@ -17,15 +18,19 @@ BINARY_SENSORS: tuple[BinarySensorEntityDescription, ...] = (
|
|||
BinarySensorEntityDescription(
|
||||
key="roaming",
|
||||
translation_key="roaming",
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
BinarySensorEntityDescription(
|
||||
key="wire_connected",
|
||||
translation_key="wire_connected",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||
),
|
||||
BinarySensorEntityDescription(
|
||||
key="mobile_connected",
|
||||
translation_key="mobile_connected",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||
),
|
||||
)
|
||||
|
|
|
@ -13,6 +13,7 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.const import (
|
||||
PERCENTAGE,
|
||||
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||
EntityCategory,
|
||||
UnitOfInformation,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
@ -35,12 +36,14 @@ SENSORS: tuple[NetgearLTESensorEntityDescription, ...] = (
|
|||
NetgearLTESensorEntityDescription(
|
||||
key="sms",
|
||||
translation_key="sms",
|
||||
icon="mdi:message-processing",
|
||||
native_unit_of_measurement="unread",
|
||||
value_fn=lambda modem_data: sum(1 for x in modem_data.data.sms if x.unread),
|
||||
),
|
||||
NetgearLTESensorEntityDescription(
|
||||
key="sms_total",
|
||||
translation_key="sms_total",
|
||||
icon="mdi:message-processing",
|
||||
native_unit_of_measurement="messages",
|
||||
value_fn=lambda modem_data: len(modem_data.data.sms),
|
||||
),
|
||||
|
@ -48,39 +51,84 @@ SENSORS: tuple[NetgearLTESensorEntityDescription, ...] = (
|
|||
key="usage",
|
||||
translation_key="usage",
|
||||
device_class=SensorDeviceClass.DATA_SIZE,
|
||||
native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
|
||||
value_fn=lambda modem_data: round(modem_data.data.usage / 1024**2, 1),
|
||||
entity_registry_enabled_default=False,
|
||||
native_unit_of_measurement=UnitOfInformation.BYTES,
|
||||
suggested_unit_of_measurement=UnitOfInformation.MEBIBYTES,
|
||||
suggested_display_precision=1,
|
||||
value_fn=lambda modem_data: modem_data.data.usage,
|
||||
),
|
||||
NetgearLTESensorEntityDescription(
|
||||
key="radio_quality",
|
||||
translation_key="radio_quality",
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
),
|
||||
NetgearLTESensorEntityDescription(
|
||||
key="rx_level",
|
||||
translation_key="rx_level",
|
||||
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||
),
|
||||
NetgearLTESensorEntityDescription(
|
||||
key="tx_level",
|
||||
translation_key="tx_level",
|
||||
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
||||
entity_registry_enabled_default=False,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||
),
|
||||
NetgearLTESensorEntityDescription(key="upstream", translation_key="upstream"),
|
||||
NetgearLTESensorEntityDescription(
|
||||
key="connection_text", translation_key="connection_text"
|
||||
key="upstream",
|
||||
translation_key="upstream",
|
||||
entity_registry_enabled_default=False,
|
||||
icon="mdi:ip-network",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
NetgearLTESensorEntityDescription(
|
||||
key="connection_type", translation_key="connection_type"
|
||||
key="connection_text",
|
||||
translation_key="connection_text",
|
||||
entity_registry_enabled_default=False,
|
||||
icon="mdi:radio-tower",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
NetgearLTESensorEntityDescription(
|
||||
key="current_ps_service_type", translation_key="service_type"
|
||||
key="connection_type",
|
||||
translation_key="connection_type",
|
||||
entity_registry_enabled_default=False,
|
||||
icon="mdi:ip",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
NetgearLTESensorEntityDescription(
|
||||
key="register_network_display", translation_key="register_network_display"
|
||||
key="current_ps_service_type",
|
||||
translation_key="service_type",
|
||||
entity_registry_enabled_default=False,
|
||||
icon="mdi:radio-tower",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
NetgearLTESensorEntityDescription(
|
||||
key="register_network_display",
|
||||
translation_key="register_network_display",
|
||||
entity_registry_enabled_default=False,
|
||||
icon="mdi:web",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
NetgearLTESensorEntityDescription(
|
||||
key="current_band",
|
||||
translation_key="band",
|
||||
entity_registry_enabled_default=False,
|
||||
icon="mdi:radio-tower",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
NetgearLTESensorEntityDescription(
|
||||
key="cell_id",
|
||||
translation_key="cell_id",
|
||||
entity_registry_enabled_default=False,
|
||||
icon="mdi:radio-tower",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
NetgearLTESensorEntityDescription(key="current_band", translation_key="band"),
|
||||
NetgearLTESensorEntityDescription(key="cell_id", translation_key="cell_id"),
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
# serializer version: 1
|
||||
# name: test_binary_sensors[binary_sensor.netgear_lm1200_mobile_connected]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'connectivity',
|
||||
'friendly_name': 'Netgear LM1200 Mobile connected',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.netgear_lm1200_mobile_connected',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[binary_sensor.netgear_lm1200_roaming]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Netgear LM1200 Roaming',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.netgear_lm1200_roaming',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
||||
# name: test_binary_sensors[binary_sensor.netgear_lm1200_wire_connected]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'connectivity',
|
||||
'friendly_name': 'Netgear LM1200 Wire connected',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.netgear_lm1200_wire_connected',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
175
tests/components/netgear_lte/snapshots/test_sensor.ambr
Normal file
175
tests/components/netgear_lte/snapshots/test_sensor.ambr
Normal file
|
@ -0,0 +1,175 @@
|
|||
# serializer version: 1
|
||||
# name: test_sensors[sensor.netgear_lm1200_cell_id]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Netgear LM1200 Cell ID',
|
||||
'icon': 'mdi:radio-tower',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.netgear_lm1200_cell_id',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '12345678',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.netgear_lm1200_connection_text]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Netgear LM1200 Connection text',
|
||||
'icon': 'mdi:radio-tower',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.netgear_lm1200_connection_text',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '4G',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.netgear_lm1200_connection_type]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Netgear LM1200 Connection type',
|
||||
'icon': 'mdi:ip',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.netgear_lm1200_connection_type',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'IPv4AndIPv6',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.netgear_lm1200_current_band]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Netgear LM1200 Current band',
|
||||
'icon': 'mdi:radio-tower',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.netgear_lm1200_current_band',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'LTE B4',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.netgear_lm1200_radio_quality]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Netgear LM1200 Radio quality',
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.netgear_lm1200_radio_quality',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '52',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.netgear_lm1200_register_network_display]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Netgear LM1200 Register network display',
|
||||
'icon': 'mdi:web',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.netgear_lm1200_register_network_display',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'T-Mobile',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.netgear_lm1200_rx_level]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'signal_strength',
|
||||
'friendly_name': 'Netgear LM1200 Rx level',
|
||||
'unit_of_measurement': 'dBm',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.netgear_lm1200_rx_level',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '-113',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.netgear_lm1200_service_type]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Netgear LM1200 Service type',
|
||||
'icon': 'mdi:radio-tower',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.netgear_lm1200_service_type',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'LTE',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.netgear_lm1200_sms]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Netgear LM1200 SMS',
|
||||
'icon': 'mdi:message-processing',
|
||||
'unit_of_measurement': 'unread',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.netgear_lm1200_sms',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '1',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.netgear_lm1200_sms_total]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Netgear LM1200 SMS total',
|
||||
'icon': 'mdi:message-processing',
|
||||
'unit_of_measurement': 'messages',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.netgear_lm1200_sms_total',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '1',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.netgear_lm1200_tx_level]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'signal_strength',
|
||||
'friendly_name': 'Netgear LM1200 Tx level',
|
||||
'unit_of_measurement': 'dBm',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.netgear_lm1200_tx_level',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '4',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.netgear_lm1200_upstream]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Netgear LM1200 Upstream',
|
||||
'icon': 'mdi:ip-network',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.netgear_lm1200_upstream',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'LTE',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.netgear_lm1200_usage]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'data_size',
|
||||
'friendly_name': 'Netgear LM1200 Usage',
|
||||
'unit_of_measurement': <UnitOfInformation.MEBIBYTES: 'MiB'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.netgear_lm1200_usage',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '40.5162000656128',
|
||||
})
|
||||
# ---
|
|
@ -1,19 +1,27 @@
|
|||
"""The tests for Netgear LTE binary sensor platform."""
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
|
||||
from homeassistant.const import ATTR_DEVICE_CLASS, STATE_OFF, STATE_ON
|
||||
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
||||
from homeassistant.components.netgear_lte.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("setup_integration", "entity_registry_enabled_by_default")
|
||||
async def test_binary_sensors(hass: HomeAssistant) -> None:
|
||||
async def test_binary_sensors(
|
||||
hass: HomeAssistant,
|
||||
entity_registry_enabled_by_default: None,
|
||||
setup_integration: None,
|
||||
entity_registry: er.EntityRegistry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test for successfully setting up the Netgear LTE binary sensor platform."""
|
||||
state = hass.states.get("binary_sensor.netgear_lm1200_mobile_connected")
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes[ATTR_DEVICE_CLASS] == BinarySensorDeviceClass.CONNECTIVITY
|
||||
state = hass.states.get("binary_sensor.netgear_lm1200_wire_connected")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes[ATTR_DEVICE_CLASS] == BinarySensorDeviceClass.CONNECTIVITY
|
||||
state = hass.states.get("binary_sensor.netgear_lm1200_roaming")
|
||||
assert state.state == STATE_OFF
|
||||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
||||
entity_entries = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
|
||||
|
||||
assert entity_entries
|
||||
for entity_entry in entity_entries:
|
||||
if entity_entry.domain != BINARY_SENSOR_DOMAIN:
|
||||
continue
|
||||
assert hass.states.get(entity_entry.entity_id) == snapshot(
|
||||
name=entity_entry.entity_id
|
||||
)
|
||||
|
|
|
@ -1,56 +1,27 @@
|
|||
"""The tests for Netgear LTE sensor platform."""
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.sensor import SensorDeviceClass
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
PERCENTAGE,
|
||||
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||
UnitOfInformation,
|
||||
)
|
||||
from homeassistant.components.netgear_lte.const import DOMAIN
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("setup_integration", "entity_registry_enabled_by_default")
|
||||
async def test_sensors(hass: HomeAssistant) -> None:
|
||||
async def test_sensors(
|
||||
hass: HomeAssistant,
|
||||
entity_registry_enabled_by_default: None,
|
||||
setup_integration: None,
|
||||
entity_registry: er.EntityRegistry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test for successfully setting up the Netgear LTE sensor platform."""
|
||||
state = hass.states.get("sensor.netgear_lm1200_cell_id")
|
||||
assert state.state == "12345678"
|
||||
state = hass.states.get("sensor.netgear_lm1200_connection_text")
|
||||
assert state.state == "4G"
|
||||
state = hass.states.get("sensor.netgear_lm1200_connection_type")
|
||||
assert state.state == "IPv4AndIPv6"
|
||||
state = hass.states.get("sensor.netgear_lm1200_current_band")
|
||||
assert state.state == "LTE B4"
|
||||
state = hass.states.get("sensor.netgear_lm1200_service_type")
|
||||
assert state.state == "LTE"
|
||||
state = hass.states.get("sensor.netgear_lm1200_radio_quality")
|
||||
assert state.state == "52"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
|
||||
state = hass.states.get("sensor.netgear_lm1200_register_network_display")
|
||||
assert state.state == "T-Mobile"
|
||||
state = hass.states.get("sensor.netgear_lm1200_rx_level")
|
||||
assert state.state == "-113"
|
||||
assert (
|
||||
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||
== SIGNAL_STRENGTH_DECIBELS_MILLIWATT
|
||||
)
|
||||
state = hass.states.get("sensor.netgear_lm1200_sms")
|
||||
assert state.state == "1"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "unread"
|
||||
state = hass.states.get("sensor.netgear_lm1200_sms_total")
|
||||
assert state.state == "1"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "messages"
|
||||
state = hass.states.get("sensor.netgear_lm1200_tx_level")
|
||||
assert state.state == "4"
|
||||
assert (
|
||||
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||
== SIGNAL_STRENGTH_DECIBELS_MILLIWATT
|
||||
)
|
||||
state = hass.states.get("sensor.netgear_lm1200_upstream")
|
||||
assert state.state == "LTE"
|
||||
state = hass.states.get("sensor.netgear_lm1200_usage")
|
||||
assert state.state == "40.5"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfInformation.MEBIBYTES
|
||||
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.DATA_SIZE
|
||||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
||||
entity_entries = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
|
||||
|
||||
assert entity_entries
|
||||
for entity_entry in entity_entries:
|
||||
if entity_entry.domain != SENSOR_DOMAIN:
|
||||
continue
|
||||
assert hass.states.get(entity_entry.entity_id) == snapshot(
|
||||
name=entity_entry.entity_id
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue