Use entity class attributes for asuswrt (#52685)

* Use entity class attributes for asuswrt

* fix

* tweak
This commit is contained in:
Robert Hillis 2021-07-24 05:55:54 -04:00 committed by GitHub
parent 0db160e372
commit 4b393f215d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 72 deletions

View File

@ -1,15 +1,12 @@
"""Support for ASUSWRT routers."""
from __future__ import annotations
from typing import Any
from homeassistant.components.device_tracker import SOURCE_TYPE_ROUTER
from homeassistant.components.device_tracker.config_entry import ScannerEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo
from .const import DATA_ASUSWRT, DOMAIN
from .router import AsusWrtRouter
@ -55,20 +52,14 @@ def add_entities(router, async_add_entities, tracked):
class AsusWrtDevice(ScannerEntity):
"""Representation of a AsusWrt device."""
_attr_should_poll = False
def __init__(self, router: AsusWrtRouter, device) -> None:
"""Initialize a AsusWrt device."""
self._router = router
self._device = device
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return self._device.mac
@property
def name(self) -> str:
"""Return the name."""
return self._device.name or DEFAULT_DEVICE_NAME
self._attr_unique_id = device.mac
self._attr_name = device.name or DEFAULT_DEVICE_NAME
@property
def is_connected(self):
@ -80,16 +71,6 @@ class AsusWrtDevice(ScannerEntity):
"""Return the source type."""
return SOURCE_TYPE_ROUTER
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the attributes."""
attrs = {}
if self._device.last_activity:
attrs["last_time_reachable"] = self._device.last_activity.isoformat(
timespec="seconds"
)
return attrs
@property
def hostname(self) -> str:
"""Return the hostname of device."""
@ -105,26 +86,20 @@ class AsusWrtDevice(ScannerEntity):
"""Return the mac address of the device."""
return self._device.mac
@property
def device_info(self) -> DeviceInfo:
"""Return the device information."""
data = {
"connections": {(CONNECTION_NETWORK_MAC, self._device.mac)},
}
if self._device.name:
data["default_name"] = self._device.name
return data
@property
def should_poll(self) -> bool:
"""No polling needed."""
return False
@callback
def async_on_demand_update(self):
"""Update state."""
self._device = self._router.devices[self._device.mac]
self._attr_device_info = {
"connections": {(CONNECTION_NETWORK_MAC, self._device.mac)},
}
if self._device.name:
self._attr_device_info["default_name"] = self._device.name
self._attr_extra_state_attributes = {}
if self._device.last_activity:
self._attr_extra_state_attributes[
"last_time_reachable"
] = self._device.last_activity.isoformat(timespec="seconds")
self.async_write_ha_state()
async def async_added_to_hass(self):

View File

@ -120,15 +120,15 @@ class AsusWrtSensor(CoordinatorEntity, SensorEntity):
super().__init__(coordinator)
self._router = router
self._sensor_type = sensor_type
self._sensor_def = sensor_def
self._name = f"{DEFAULT_PREFIX} {sensor_def[SENSOR_NAME]}"
self._unique_id = f"{DOMAIN} {self._name}"
self._attr_name = f"{DEFAULT_PREFIX} {sensor_def[SENSOR_NAME]}"
self._factor = sensor_def.get(SENSOR_FACTOR)
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return self._sensor_def.get(SENSOR_DEFAULT_ENABLED, False)
self._attr_unique_id = f"{DOMAIN} {self.name}"
self._attr_entity_registry_enabled_default = sensor_def.get(
SENSOR_DEFAULT_ENABLED, False
)
self._attr_unit_of_measurement = sensor_def.get(SENSOR_UNIT)
self._attr_icon = sensor_def.get(SENSOR_ICON)
self._attr_device_class = sensor_def.get(SENSOR_DEVICE_CLASS)
@property
def state(self) -> str:
@ -140,31 +140,6 @@ class AsusWrtSensor(CoordinatorEntity, SensorEntity):
return round(state / self._factor, 2)
return state
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return self._unique_id
@property
def name(self) -> str:
"""Return the name."""
return self._name
@property
def unit_of_measurement(self) -> str:
"""Return the unit."""
return self._sensor_def.get(SENSOR_UNIT)
@property
def icon(self) -> str:
"""Return the icon."""
return self._sensor_def.get(SENSOR_ICON)
@property
def device_class(self) -> str:
"""Return the device_class."""
return self._sensor_def.get(SENSOR_DEVICE_CLASS)
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the attributes."""