From 454201d0a8cd4ff9a6058a8141745953a74bd671 Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Thu, 28 Dec 2023 11:47:04 -0500 Subject: [PATCH] Add device info to Netgear LTE (#106568) * Add device info to Netgear LTE * uno mas --- .../components/netgear_lte/binary_sensor.py | 12 +------- .../components/netgear_lte/entity.py | 25 ++++++++++++---- .../components/netgear_lte/sensor.py | 15 +++------- .../netgear_lte/snapshots/test_init.ambr | 29 +++++++++++++++++++ tests/components/netgear_lte/test_init.py | 16 ++++++++++ 5 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 tests/components/netgear_lte/snapshots/test_init.ambr diff --git a/homeassistant/components/netgear_lte/binary_sensor.py b/homeassistant/components/netgear_lte/binary_sensor.py index 810e3733fbe..ce179c9e980 100644 --- a/homeassistant/components/netgear_lte/binary_sensor.py +++ b/homeassistant/components/netgear_lte/binary_sensor.py @@ -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.""" diff --git a/homeassistant/components/netgear_lte/entity.py b/homeassistant/components/netgear_lte/entity.py index 33e0aaab749..02827e7df3f 100644 --- a/homeassistant/components/netgear_lte/entity.py +++ b/homeassistant/components/netgear_lte/entity.py @@ -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.""" diff --git a/homeassistant/components/netgear_lte/sensor.py b/homeassistant/components/netgear_lte/sensor.py index b91bb9b561a..d281c93d795 100644 --- a/homeassistant/components/netgear_lte/sensor.py +++ b/homeassistant/components/netgear_lte/sensor.py @@ -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) diff --git a/tests/components/netgear_lte/snapshots/test_init.ambr b/tests/components/netgear_lte/snapshots/test_init.ambr new file mode 100644 index 00000000000..2eb2fff89ef --- /dev/null +++ b/tests/components/netgear_lte/snapshots/test_init.ambr @@ -0,0 +1,29 @@ +# serializer version: 1 +# name: test_device + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'configuration_url': 'http://192.168.5.1', + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '1.0', + 'id': , + '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, + }) +# --- diff --git a/tests/components/netgear_lte/test_init.py b/tests/components/netgear_lte/test_init.py index 7c48d9d87d2..9d9b43f5a16 100644 --- a/tests/components/netgear_lte/test_init.py +++ b/tests/components/netgear_lte/test_init.py @@ -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