Use EntityDescription in Freebox switch (#80858)
* Freebox switch: use EntityDescription * unique_id base on key later with migration step * Keep the same name Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/freebox/switch.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Removing specific FreeboxSwitchEntityDescription as there is one sensor, for now Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>pull/80936/head
parent
644b00ca2d
commit
4f5c9be84f
|
@ -6,10 +6,10 @@ from typing import Any
|
|||
|
||||
from freebox_api.exceptions import InsufficientPermissionsError
|
||||
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity import EntityCategory
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
|
@ -18,49 +18,43 @@ from .router import FreeboxRouter
|
|||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
SWITCH_DESCRIPTIONS = [
|
||||
SwitchEntityDescription(
|
||||
key="wifi",
|
||||
name="Freebox WiFi",
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the switch."""
|
||||
router: FreeboxRouter = hass.data[DOMAIN][entry.unique_id]
|
||||
async_add_entities([FreeboxWifiSwitch(router)], True)
|
||||
entities = [
|
||||
FreeboxSwitch(router, entity_description)
|
||||
for entity_description in SWITCH_DESCRIPTIONS
|
||||
]
|
||||
async_add_entities(entities, True)
|
||||
|
||||
|
||||
class FreeboxWifiSwitch(SwitchEntity):
|
||||
"""Representation of a freebox wifi switch."""
|
||||
class FreeboxSwitch(SwitchEntity):
|
||||
"""Representation of a freebox switch."""
|
||||
|
||||
def __init__(self, router: FreeboxRouter) -> None:
|
||||
"""Initialize the Wifi switch."""
|
||||
self._name = "Freebox WiFi"
|
||||
self._state: bool | None = None
|
||||
def __init__(
|
||||
self, router: FreeboxRouter, entity_description: SwitchEntityDescription
|
||||
) -> None:
|
||||
"""Initialize the switch."""
|
||||
self.entity_description = entity_description
|
||||
self._router = router
|
||||
self._unique_id = f"{self._router.mac} {self._name}"
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique ID."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the switch."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if device is on."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return the device information."""
|
||||
return self._router.device_info
|
||||
self._attr_device_info = self._router.device_info
|
||||
self._attr_unique_id = f"{self._router.mac} {self.entity_description.name}"
|
||||
|
||||
async def _async_set_state(self, enabled: bool):
|
||||
"""Turn the switch on or off."""
|
||||
wifi_config = {"enabled": enabled}
|
||||
try:
|
||||
await self._router.wifi.set_global_config(wifi_config)
|
||||
await self._router.wifi.set_global_config({"enabled": enabled})
|
||||
except InsufficientPermissionsError:
|
||||
_LOGGER.warning(
|
||||
"Home Assistant does not have permissions to modify the Freebox settings. Please refer to documentation"
|
||||
|
@ -77,5 +71,4 @@ class FreeboxWifiSwitch(SwitchEntity):
|
|||
async def async_update(self) -> None:
|
||||
"""Get the state and update it."""
|
||||
datas = await self._router.wifi.get_global_config()
|
||||
active = datas["enabled"]
|
||||
self._state = bool(active)
|
||||
self._attr_is_on = bool(datas["enabled"])
|
||||
|
|
Loading…
Reference in New Issue