Move Netgear LTE entity to its own file (#92944)

This commit is contained in:
Robert Hillis 2023-05-12 07:29:56 -04:00 committed by GitHub
parent e9705364a8
commit b4e85b7692
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 54 deletions

View File

@ -19,11 +19,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers import config_validation as cv, discovery
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.typing import ConfigType
@ -334,50 +330,3 @@ async def _retry_login(hass, modem_data, password):
await _login(hass, modem_data, password)
except eternalegypt.Error:
delay = min(2 * delay, 300)
@attr.s
class LTEEntity(Entity):
"""Base LTE entity."""
modem_data = attr.ib()
sensor_type = attr.ib()
_unique_id = attr.ib(init=False)
@_unique_id.default
def _init_unique_id(self):
"""Register unique_id while we know data is valid."""
return f"{self.sensor_type}_{self.modem_data.data.serial_number}"
async def async_added_to_hass(self):
"""Register callback."""
self.async_on_remove(
async_dispatcher_connect(
self.hass, DISPATCHER_NETGEAR_LTE, self.async_write_ha_state
)
)
async def async_update(self):
"""Force update of state."""
await self.modem_data.async_update()
@property
def should_poll(self):
"""Return that the sensor should not be polled."""
return False
@property
def available(self):
"""Return the availability of the sensor."""
return self.modem_data.data is not None
@property
def unique_id(self):
"""Return a unique ID like 'usage_5TG365AB0078V'."""
return self._unique_id
@property
def name(self):
"""Return the name of the sensor."""
return f"Netgear LTE {self.sensor_type}"

View File

@ -8,8 +8,8 @@ from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import LTEEntity
from .const import CONF_BINARY_SENSOR, DOMAIN
from .entity import LTEEntity
from .sensor_types import BINARY_SENSOR_CLASSES

View File

@ -0,0 +1,41 @@
"""Entity representing a Netgear LTE entity."""
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from . import ModemData
from .const import DISPATCHER_NETGEAR_LTE
class LTEEntity(Entity):
"""Base LTE entity."""
_attr_should_poll = False
def __init__(
self,
modem_data: ModemData,
sensor_type: str,
) -> None:
"""Initialize a Netgear LTE entity."""
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}"
async def async_added_to_hass(self) -> None:
"""Register callback."""
self.async_on_remove(
async_dispatcher_connect(
self.hass, DISPATCHER_NETGEAR_LTE, self.async_write_ha_state
)
)
async def async_update(self) -> None:
"""Force update of state."""
await self.modem_data.async_update()
@property
def available(self) -> bool:
"""Return the availability of the sensor."""
return self.modem_data.data is not None

View File

@ -8,8 +8,8 @@ from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import LTEEntity
from .const import CONF_SENSOR, DOMAIN
from .entity import LTEEntity
from .sensor_types import SENSOR_SMS, SENSOR_SMS_TOTAL, SENSOR_UNITS, SENSOR_USAGE