* Original work from Quentame * Small adjustments * Add properties and method_version * fix unknown name * add consider_home functionality * fix typo * fix key * swao setup order * use formatted mac * add tracked_list option * add options flow * add config flow * add config flow * clean up registries * only remove if no other integration has that device * tracked_list formatting * convert tracked list * add import * move imports * use new tracked list on update * use update_device instead of remove * add strings * initialize already known devices * Update router.py * Update router.py * Update router.py * small fixes * styling * fix typing * fix spelling * Update router.py * get model of router * add router device info * fix api * add listeners * update router device info * remove method version option * Update __init__.py * fix styling * ignore typing * remove typing * fix mypy config * Update mypy.ini * add options flow tests * Update .coveragerc * fix styling * Update homeassistant/components/netgear/__init__.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netgear/__init__.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netgear/__init__.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netgear/config_flow.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netgear/router.py Co-authored-by: J. Nick Koston <nick@koston.org> * add ConfigEntryNotReady * Update router.py * use entry.async_on_unload * Update homeassistant/components/netgear/device_tracker.py Co-authored-by: J. Nick Koston <nick@koston.org> * use cv.ensure_list_csv * add hostname property * Update device_tracker.py * fix typo * fix isort * add myself to codeowners * clean config flow * further clean config flow * deprecate old netgear discovery * split out _async_remove_untracked_registries * Update homeassistant/components/netgear/config_flow.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netgear/config_flow.py Co-authored-by: J. Nick Koston <nick@koston.org> * cleanup * fix rename * fix typo * remove URL option * fixes * add sensor platform * fixes * fix removing multiple entities * remove extra attributes * initialize sensors correctly * extra sensors disabled by default * fix styling and unused imports * fix tests * Update .coveragerc * fix requirements * remove tracked list * remove tracked registry editing * fix styling * fix discovery test * simplify unload * Update homeassistant/components/netgear/router.py Co-authored-by: J. Nick Koston <nick@koston.org> * add typing Co-authored-by: J. Nick Koston <nick@koston.org> * add typing Co-authored-by: J. Nick Koston <nick@koston.org> * add typing Co-authored-by: J. Nick Koston <nick@koston.org> * condense NetgearSensorEntities Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netgear/router.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netgear/router.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netgear/router.py Co-authored-by: J. Nick Koston <nick@koston.org> * Update homeassistant/components/netgear/router.py Co-authored-by: J. Nick Koston <nick@koston.org> * add typing * styling * add typing * use ForwardRefrence for typing * Update homeassistant/components/netgear/device_tracker.py Co-authored-by: J. Nick Koston <nick@koston.org> * add typing * Apply suggestions from code review Thanks! Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * process review comments * fix styling * fix devicename not available on all models * ensure DeviceName is not needed * Update homeassistant/components/netgear/config_flow.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/netgear/config_flow.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update __init__.py * fix styling Co-authored-by: J. Nick Koston <nick@koston.org> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
83 lines
2.7 KiB
Python
83 lines
2.7 KiB
Python
"""Support for Netgear routers."""
|
|
import logging
|
|
|
|
from homeassistant.components.sensor import (
|
|
DEVICE_CLASS_SIGNAL_STRENGTH,
|
|
SensorEntity,
|
|
SensorEntityDescription,
|
|
)
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import PERCENTAGE
|
|
from homeassistant.core import callback
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
|
|
|
from .router import NetgearDeviceEntity, NetgearRouter, async_setup_netgear_entry
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
SENSOR_TYPES = {
|
|
"type": SensorEntityDescription(
|
|
key="type",
|
|
name="link type",
|
|
native_unit_of_measurement=None,
|
|
device_class=None,
|
|
),
|
|
"link_rate": SensorEntityDescription(
|
|
key="link_rate",
|
|
name="link rate",
|
|
native_unit_of_measurement="Mbps",
|
|
device_class=None,
|
|
),
|
|
"signal": SensorEntityDescription(
|
|
key="signal",
|
|
name="signal strength",
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
|
),
|
|
}
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
) -> None:
|
|
"""Set up device tracker for Netgear component."""
|
|
|
|
def generate_sensor_classes(router: NetgearRouter, device: dict):
|
|
return [
|
|
NetgearSensorEntity(router, device, attribute)
|
|
for attribute in ("type", "link_rate", "signal")
|
|
]
|
|
|
|
async_setup_netgear_entry(hass, entry, async_add_entities, generate_sensor_classes)
|
|
|
|
|
|
class NetgearSensorEntity(NetgearDeviceEntity, SensorEntity):
|
|
"""Representation of a device connected to a Netgear router."""
|
|
|
|
_attr_entity_registry_enabled_default = False
|
|
|
|
def __init__(self, router: NetgearRouter, device: dict, attribute: str) -> None:
|
|
"""Initialize a Netgear device."""
|
|
super().__init__(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[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[self._attribute] is not None:
|
|
self._state = self._device[self._attribute]
|
|
|
|
self.async_write_ha_state()
|