Use shorthand attributes for Freebox (#99327)

This commit is contained in:
Joost Lekkerkerker 2023-08-30 15:33:05 +02:00 committed by GitHub
parent 587928223a
commit e56db78b27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 7 additions and 23 deletions

View File

@ -12,7 +12,6 @@ from homeassistant.components.button import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN from .const import DOMAIN
@ -72,13 +71,9 @@ class FreeboxButton(ButtonEntity):
"""Initialize a Freebox button.""" """Initialize a Freebox button."""
self.entity_description = description self.entity_description = description
self._router = router self._router = router
self._attr_device_info = router.device_info
self._attr_unique_id = f"{router.mac} {description.name}" self._attr_unique_id = f"{router.mac} {description.name}"
@property
def device_info(self) -> DeviceInfo:
"""Return the device information."""
return self._router.device_info
async def async_press(self) -> None: async def async_press(self) -> None:
"""Press the button.""" """Press the button."""
await self.entity_description.async_press(self._router) await self.entity_description.async_press(self._router)

View File

@ -61,9 +61,9 @@ class FreeboxDevice(ScannerEntity):
self._name = device["primary_name"].strip() or DEFAULT_DEVICE_NAME self._name = device["primary_name"].strip() or DEFAULT_DEVICE_NAME
self._mac = device["l2ident"]["id"] self._mac = device["l2ident"]["id"]
self._manufacturer = device["vendor_name"] self._manufacturer = device["vendor_name"]
self._icon = icon_for_freebox_device(device) self._attr_icon = icon_for_freebox_device(device)
self._active = False self._active = False
self._attrs: dict[str, Any] = {} self._attr_extra_state_attributes: dict[str, Any] = {}
@callback @callback
def async_update_state(self) -> None: def async_update_state(self) -> None:
@ -72,7 +72,7 @@ class FreeboxDevice(ScannerEntity):
self._active = device["active"] self._active = device["active"]
if device.get("attrs") is None: if device.get("attrs") is None:
# device # device
self._attrs = { self._attr_extra_state_attributes = {
"last_time_reachable": datetime.fromtimestamp( "last_time_reachable": datetime.fromtimestamp(
device["last_time_reachable"] device["last_time_reachable"]
), ),
@ -80,7 +80,7 @@ class FreeboxDevice(ScannerEntity):
} }
else: else:
# router # router
self._attrs = device["attrs"] self._attr_extra_state_attributes = device["attrs"]
@property @property
def mac_address(self) -> str: def mac_address(self) -> str:
@ -102,16 +102,6 @@ class FreeboxDevice(ScannerEntity):
"""Return the source type.""" """Return the source type."""
return SourceType.ROUTER return SourceType.ROUTER
@property
def icon(self) -> str:
"""Return the icon."""
return self._icon
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the attributes."""
return self._attrs
@callback @callback
def async_on_demand_update(self): def async_on_demand_update(self):
"""Update state.""" """Update state."""

View File

@ -197,7 +197,6 @@ class FreeboxDiskSensor(FreeboxSensor):
) -> None: ) -> None:
"""Initialize a Freebox disk sensor.""" """Initialize a Freebox disk sensor."""
super().__init__(router, description) super().__init__(router, description)
self._disk = disk
self._partition = partition self._partition = partition
self._attr_name = f"{partition['label']} {description.name}" self._attr_name = f"{partition['label']} {description.name}"
self._attr_unique_id = ( self._attr_unique_id = (

View File

@ -48,8 +48,8 @@ class FreeboxSwitch(SwitchEntity):
"""Initialize the switch.""" """Initialize the switch."""
self.entity_description = entity_description self.entity_description = entity_description
self._router = router self._router = router
self._attr_device_info = self._router.device_info self._attr_device_info = router.device_info
self._attr_unique_id = f"{self._router.mac} {self.entity_description.name}" self._attr_unique_id = f"{router.mac} {entity_description.name}"
async def _async_set_state(self, enabled: bool): async def _async_set_state(self, enabled: bool):
"""Turn the switch on or off.""" """Turn the switch on or off."""