hass-core/homeassistant/components/netgear/sensor.py
starkillerOG 69a004a2f6
Netgear coordinator (#65255)
* implement coordinator

* fix styling

* fix async_uload_entry

* use async_config_entry_first_refresh

* use const

* fix black

* use KEY_ROUTER

* review comments

* fix black

* ensure the coordinator keeps updating

* fix flake8

* rework setup of entities using coordinator

* styling

* check for failed get_info call

* fix

* fix setup of entities

* simplify

* do not set unique_id and device_info on scanner entity

* Update homeassistant/components/netgear/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/netgear/device_tracker.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/netgear/router.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* use entry_id instead of unique_id

* unused import

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2022-02-03 12:28:04 +01:00

121 lines
3.8 KiB
Python

"""Support for Netgear routers."""
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import DOMAIN, KEY_COORDINATOR, KEY_ROUTER
from .router import NetgearDeviceEntity, NetgearRouter
SENSOR_TYPES = {
"type": SensorEntityDescription(
key="type",
name="link type",
entity_category=EntityCategory.DIAGNOSTIC,
),
"link_rate": SensorEntityDescription(
key="link_rate",
name="link rate",
native_unit_of_measurement="Mbps",
entity_category=EntityCategory.DIAGNOSTIC,
),
"signal": SensorEntityDescription(
key="signal",
name="signal strength",
native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
entity_category=EntityCategory.DIAGNOSTIC,
),
"ssid": SensorEntityDescription(
key="ssid",
name="ssid",
entity_category=EntityCategory.DIAGNOSTIC,
),
"conn_ap_mac": SensorEntityDescription(
key="conn_ap_mac",
name="access point mac",
entity_category=EntityCategory.DIAGNOSTIC,
),
}
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up device tracker for Netgear component."""
router = hass.data[DOMAIN][entry.entry_id][KEY_ROUTER]
coordinator = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR]
tracked = set()
sensors = ["type", "link_rate", "signal"]
if router.method_version == 2:
sensors.extend(["ssid", "conn_ap_mac"])
@callback
def new_device_callback() -> None:
"""Add new devices if needed."""
if not coordinator.data:
return
new_entities = []
for mac, device in router.devices.items():
if mac in tracked:
continue
new_entities.extend(
[
NetgearSensorEntity(coordinator, router, device, attribute)
for attribute in sensors
]
)
tracked.add(mac)
if new_entities:
async_add_entities(new_entities)
entry.async_on_unload(coordinator.async_add_listener(new_device_callback))
coordinator.data = True
new_device_callback()
class NetgearSensorEntity(NetgearDeviceEntity, SensorEntity):
"""Representation of a device connected to a Netgear router."""
_attr_entity_registry_enabled_default = False
def __init__(
self,
coordinator: DataUpdateCoordinator,
router: NetgearRouter,
device: dict,
attribute: str,
) -> None:
"""Initialize a Netgear device."""
super().__init__(coordinator, router, device)
self._attribute = attribute
self.entity_description = SENSOR_TYPES[self._attribute]
self._name = f"{self.get_device_name()} {self.entity_description.name}"
self._unique_id = f"{self._mac}-{self._attribute}"
self._state = self._device.get(self._attribute)
@property
def native_value(self):
"""Return the state of the sensor."""
return self._state
@callback
def async_update_device(self) -> None:
"""Update the Netgear device."""
self._device = self._router.devices[self._mac]
self._active = self._device["active"]
if self._device.get(self._attribute) is not None:
self._state = self._device[self._attribute]