mirror of
https://github.com/home-assistant/core.git
synced 2026-07-08 15:24:58 +01:00
Simplify Netgear entity initialisation (#164837)
This commit is contained in:
@@ -41,10 +41,9 @@ async def async_setup_entry(
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up button for Netgear component."""
|
||||
router = entry.runtime_data.router
|
||||
coordinator_tracker = entry.runtime_data.coordinator_tracker
|
||||
async_add_entities(
|
||||
NetgearRouterButtonEntity(coordinator_tracker, router, entity_description)
|
||||
NetgearRouterButtonEntity(coordinator_tracker, entity_description)
|
||||
for entity_description in BUTTONS
|
||||
)
|
||||
|
||||
@@ -57,13 +56,14 @@ class NetgearRouterButtonEntity(NetgearRouterCoordinatorEntity, ButtonEntity):
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: NetgearTrackerCoordinator,
|
||||
router: NetgearRouter,
|
||||
entity_description: NetgearButtonEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize a Netgear device."""
|
||||
super().__init__(coordinator, router)
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = entity_description
|
||||
self._attr_unique_id = f"{router.serial_number}-{entity_description.key}"
|
||||
self._attr_unique_id = (
|
||||
f"{coordinator.router.serial_number}-{entity_description.key}"
|
||||
)
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Triggers the button press service."""
|
||||
|
||||
@@ -11,7 +11,6 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from .const import DEVICE_ICONS
|
||||
from .coordinator import NetgearConfigEntry, NetgearTrackerCoordinator
|
||||
from .entity import NetgearDeviceEntity
|
||||
from .router import NetgearRouter
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -38,9 +37,7 @@ async def async_setup_entry(
|
||||
if mac in tracked:
|
||||
continue
|
||||
|
||||
new_entities.append(
|
||||
NetgearScannerEntity(coordinator_tracker, router, device)
|
||||
)
|
||||
new_entities.append(NetgearScannerEntity(coordinator_tracker, device))
|
||||
tracked.add(mac)
|
||||
|
||||
async_add_entities(new_entities)
|
||||
@@ -59,11 +56,10 @@ class NetgearScannerEntity(NetgearDeviceEntity, ScannerEntity):
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: NetgearTrackerCoordinator,
|
||||
router: NetgearRouter,
|
||||
device: dict,
|
||||
) -> None:
|
||||
"""Initialize a Netgear device."""
|
||||
super().__init__(coordinator, router, device)
|
||||
super().__init__(coordinator, device)
|
||||
self._hostname = self.get_hostname()
|
||||
self._icon = DEVICE_ICONS.get(device["device_type"], "mdi:help-network")
|
||||
self._attr_name = self._device_name
|
||||
|
||||
@@ -25,12 +25,11 @@ class NetgearDeviceEntity(CoordinatorEntity[NetgearTrackerCoordinator]):
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: NetgearTrackerCoordinator,
|
||||
router: NetgearRouter,
|
||||
device: dict,
|
||||
) -> None:
|
||||
"""Initialize a Netgear device."""
|
||||
super().__init__(coordinator)
|
||||
self._router = router
|
||||
self._router = coordinator.router
|
||||
self._device = device
|
||||
self._mac = device["mac"]
|
||||
self._device_name = self.get_device_name()
|
||||
@@ -40,7 +39,7 @@ class NetgearDeviceEntity(CoordinatorEntity[NetgearTrackerCoordinator]):
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, self._mac)},
|
||||
default_name=self._device_name,
|
||||
default_model=device["device_model"],
|
||||
via_device=(DOMAIN, router.unique_id),
|
||||
via_device=(DOMAIN, coordinator.router.unique_id),
|
||||
)
|
||||
|
||||
def get_device_name(self):
|
||||
@@ -93,10 +92,10 @@ class NetgearRouterCoordinatorEntity[T: NetgearDataCoordinator[Any]](
|
||||
):
|
||||
"""Base class for a Netgear router entity."""
|
||||
|
||||
def __init__(self, coordinator: T, router: NetgearRouter) -> None:
|
||||
def __init__(self, coordinator: T) -> None:
|
||||
"""Initialize a Netgear device."""
|
||||
CoordinatorEntity.__init__(self, coordinator)
|
||||
NetgearRouterEntity.__init__(self, router)
|
||||
NetgearRouterEntity.__init__(self, coordinator.router)
|
||||
|
||||
@abstractmethod
|
||||
@callback
|
||||
|
||||
@@ -33,7 +33,6 @@ from .coordinator import (
|
||||
NetgearTrackerCoordinator,
|
||||
)
|
||||
from .entity import NetgearDeviceEntity, NetgearRouterCoordinatorEntity
|
||||
from .router import NetgearRouter
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -282,7 +281,7 @@ async def async_setup_entry(
|
||||
coordinator_link = entry.runtime_data.coordinator_link
|
||||
|
||||
async_add_entities(
|
||||
NetgearRouterSensorEntity(coordinator, router, description)
|
||||
NetgearRouterSensorEntity(coordinator, description)
|
||||
for (coordinator, descriptions) in (
|
||||
(coordinator_traffic, SENSOR_TRAFFIC_TYPES),
|
||||
(coordinator_speed, SENSOR_SPEED_TYPES),
|
||||
@@ -311,7 +310,7 @@ async def async_setup_entry(
|
||||
continue
|
||||
|
||||
new_entities.extend(
|
||||
NetgearSensorEntity(coordinator_tracker, router, device, attribute)
|
||||
NetgearSensorEntity(coordinator_tracker, device, attribute)
|
||||
for attribute in sensors
|
||||
)
|
||||
tracked.add(mac)
|
||||
@@ -330,12 +329,11 @@ class NetgearSensorEntity(NetgearDeviceEntity, SensorEntity):
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: NetgearTrackerCoordinator,
|
||||
router: NetgearRouter,
|
||||
device: dict,
|
||||
attribute: str,
|
||||
) -> None:
|
||||
"""Initialize a Netgear device."""
|
||||
super().__init__(coordinator, router, device)
|
||||
super().__init__(coordinator, device)
|
||||
self._attribute = attribute
|
||||
self.entity_description = SENSOR_TYPES[attribute]
|
||||
self._attr_unique_id = f"{self._mac}-{attribute}"
|
||||
@@ -369,13 +367,12 @@ class NetgearRouterSensorEntity(NetgearRouterCoordinatorEntity, RestoreSensor):
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: NetgearDataCoordinator[dict[str, Any] | None],
|
||||
router: NetgearRouter,
|
||||
entity_description: NetgearSensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize a Netgear device."""
|
||||
super().__init__(coordinator, router)
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = entity_description
|
||||
self._attr_unique_id = f"{router.serial_number}-{entity_description.key}-{entity_description.index}"
|
||||
self._attr_unique_id = f"{coordinator.router.serial_number}-{entity_description.key}-{entity_description.index}"
|
||||
|
||||
self._value: StateType | date | datetime | Decimal = None
|
||||
self.async_update_device()
|
||||
|
||||
@@ -126,9 +126,7 @@ async def async_setup_entry(
|
||||
|
||||
new_entities.extend(
|
||||
[
|
||||
NetgearAllowBlock(
|
||||
coordinator_tracker, router, device, entity_description
|
||||
)
|
||||
NetgearAllowBlock(coordinator_tracker, device, entity_description)
|
||||
for entity_description in SWITCH_TYPES
|
||||
]
|
||||
)
|
||||
@@ -150,12 +148,11 @@ class NetgearAllowBlock(NetgearDeviceEntity, SwitchEntity):
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: NetgearTrackerCoordinator,
|
||||
router: NetgearRouter,
|
||||
device: dict,
|
||||
entity_description: SwitchEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize a Netgear device."""
|
||||
super().__init__(coordinator, router, device)
|
||||
super().__init__(coordinator, device)
|
||||
self.entity_description = entity_description
|
||||
self._attr_unique_id = f"{self._mac}-{entity_description.key}"
|
||||
self.async_update_device()
|
||||
|
||||
@@ -15,7 +15,6 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .coordinator import NetgearConfigEntry, NetgearFirmwareCoordinator
|
||||
from .entity import NetgearRouterCoordinatorEntity
|
||||
from .router import NetgearRouter
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -26,9 +25,8 @@ async def async_setup_entry(
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up update entities for Netgear component."""
|
||||
router = entry.runtime_data.router
|
||||
coordinator = entry.runtime_data.coordinator_firmware
|
||||
entities = [NetgearUpdateEntity(coordinator, router)]
|
||||
entities = [NetgearUpdateEntity(coordinator)]
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
@@ -44,11 +42,10 @@ class NetgearUpdateEntity(
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: NetgearFirmwareCoordinator,
|
||||
router: NetgearRouter,
|
||||
) -> None:
|
||||
"""Initialize a Netgear device."""
|
||||
super().__init__(coordinator, router)
|
||||
self._attr_unique_id = f"{router.serial_number}-update"
|
||||
super().__init__(coordinator)
|
||||
self._attr_unique_id = f"{coordinator.router.serial_number}-update"
|
||||
|
||||
@property
|
||||
def installed_version(self) -> str | None:
|
||||
|
||||
Reference in New Issue
Block a user