Add device info to Netgear LTE (#106568)
* Add device info to Netgear LTE * uno maspull/106575/head
parent
353f33f4ac
commit
454201d0a8
|
@ -10,7 +10,6 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import ModemData
|
||||
from .const import DOMAIN
|
||||
from .entity import LTEEntity
|
||||
|
||||
|
@ -36,22 +35,13 @@ async def async_setup_entry(
|
|||
modem_data = hass.data[DOMAIN].get_modem_data(entry.data)
|
||||
|
||||
async_add_entities(
|
||||
NetgearLTEBinarySensor(modem_data, sensor) for sensor in BINARY_SENSORS
|
||||
NetgearLTEBinarySensor(entry, modem_data, sensor) for sensor in BINARY_SENSORS
|
||||
)
|
||||
|
||||
|
||||
class NetgearLTEBinarySensor(LTEEntity, BinarySensorEntity):
|
||||
"""Netgear LTE binary sensor entity."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
modem_data: ModemData,
|
||||
entity_description: BinarySensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize a Netgear LTE binary sensor entity."""
|
||||
super().__init__(modem_data, entity_description.key)
|
||||
self.entity_description = entity_description
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if the binary sensor is on."""
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
"""Entity representing a Netgear LTE entity."""
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity import Entity, EntityDescription
|
||||
|
||||
from . import ModemData
|
||||
from .const import DISPATCHER_NETGEAR_LTE
|
||||
from .const import DISPATCHER_NETGEAR_LTE, DOMAIN, MANUFACTURER
|
||||
|
||||
|
||||
class LTEEntity(Entity):
|
||||
|
@ -14,14 +17,24 @@ class LTEEntity(Entity):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
config_entry: ConfigEntry,
|
||||
modem_data: ModemData,
|
||||
sensor_type: str,
|
||||
description: EntityDescription,
|
||||
) -> None:
|
||||
"""Initialize a Netgear LTE entity."""
|
||||
self.entity_description = description
|
||||
self.modem_data = modem_data
|
||||
self.sensor_type = sensor_type
|
||||
self._attr_name = f"Netgear LTE {sensor_type}"
|
||||
self._attr_unique_id = f"{sensor_type}_{modem_data.data.serial_number}"
|
||||
self._attr_name = f"Netgear LTE {description.key}"
|
||||
self._attr_unique_id = f"{description.key}_{modem_data.data.serial_number}"
|
||||
self._attr_device_info = DeviceInfo(
|
||||
configuration_url=f"http://{config_entry.data[CONF_HOST]}",
|
||||
identifiers={(DOMAIN, modem_data.data.serial_number)},
|
||||
manufacturer=MANUFACTURER,
|
||||
model=modem_data.data.items["general.model"],
|
||||
serial_number=modem_data.data.serial_number,
|
||||
sw_version=modem_data.data.items["general.fwversion"],
|
||||
hw_version=modem_data.data.items["general.hwversion"],
|
||||
)
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Register callback."""
|
||||
|
|
|
@ -76,7 +76,9 @@ async def async_setup_entry(
|
|||
"""Set up the Netgear LTE sensor."""
|
||||
modem_data = hass.data[DOMAIN].get_modem_data(entry.data)
|
||||
|
||||
async_add_entities(NetgearLTESensor(modem_data, sensor) for sensor in SENSORS)
|
||||
async_add_entities(
|
||||
NetgearLTESensor(entry, modem_data, sensor) for sensor in SENSORS
|
||||
)
|
||||
|
||||
|
||||
class NetgearLTESensor(LTEEntity, SensorEntity):
|
||||
|
@ -84,18 +86,9 @@ class NetgearLTESensor(LTEEntity, SensorEntity):
|
|||
|
||||
entity_description: NetgearLTESensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
modem_data: ModemData,
|
||||
entity_description: NetgearLTESensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize a Netgear LTE sensor entity."""
|
||||
super().__init__(modem_data, entity_description.key)
|
||||
self.entity_description = entity_description
|
||||
|
||||
@property
|
||||
def native_value(self) -> StateType:
|
||||
"""Return the state of the sensor."""
|
||||
if self.entity_description.value_fn is not None:
|
||||
return self.entity_description.value_fn(self.modem_data)
|
||||
return getattr(self.modem_data.data, self.sensor_type)
|
||||
return getattr(self.modem_data.data, self.entity_description.key)
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
# serializer version: 1
|
||||
# name: test_device
|
||||
DeviceRegistryEntrySnapshot({
|
||||
'area_id': None,
|
||||
'config_entries': <ANY>,
|
||||
'configuration_url': 'http://192.168.5.1',
|
||||
'connections': set({
|
||||
}),
|
||||
'disabled_by': None,
|
||||
'entry_type': None,
|
||||
'hw_version': '1.0',
|
||||
'id': <ANY>,
|
||||
'identifiers': set({
|
||||
tuple(
|
||||
'netgear_lte',
|
||||
'FFFFFFFFFFFFF',
|
||||
),
|
||||
}),
|
||||
'is_new': False,
|
||||
'manufacturer': 'Netgear',
|
||||
'model': 'LM1200',
|
||||
'name': 'Netgear LM1200',
|
||||
'name_by_user': None,
|
||||
'serial_number': 'FFFFFFFFFFFFF',
|
||||
'suggested_area': None,
|
||||
'sw_version': 'EC25AFFDR07A09M4G',
|
||||
'via_device_id': None,
|
||||
})
|
||||
# ---
|
|
@ -1,7 +1,10 @@
|
|||
"""Test Netgear LTE integration."""
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.netgear_lte.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from .conftest import CONF_DATA
|
||||
|
||||
|
@ -26,3 +29,16 @@ async def test_async_setup_entry_not_ready(
|
|||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert entry.state == ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
async def test_device(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
setup_integration: None,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test device info."""
|
||||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
||||
await hass.async_block_till_done()
|
||||
device = device_registry.async_get_device(identifiers={(DOMAIN, entry.unique_id)})
|
||||
assert device == snapshot
|
||||
|
|
Loading…
Reference in New Issue