1 """Support for Netgear switches."""
3 from collections.abc
import Callable
4 from dataclasses
import dataclass
5 from datetime
import timedelta
9 from pynetgear
import ALLOW, BLOCK
18 from .const
import DOMAIN, KEY_COORDINATOR, KEY_ROUTER
19 from .entity
import NetgearDeviceEntity, NetgearRouterEntity
20 from .router
import NetgearRouter
22 _LOGGER = logging.getLogger(__name__)
29 translation_key=
"allowed_on_network",
30 entity_category=EntityCategory.CONFIG,
35 @dataclass(frozen=True)
37 """Required attributes of NetgearSwitchEntityDescription."""
40 @dataclass(frozen=
True, kw_only=
True)
42 """Class describing Netgear Switch entities."""
44 update: Callable[[NetgearRouter], bool]
45 action: Callable[[NetgearRouter], bool]
48 ROUTER_SWITCH_TYPES = [
51 translation_key=
"access_control",
52 entity_category=EntityCategory.CONFIG,
53 update=
lambda router: router.api.get_block_device_enable_status,
54 action=
lambda router: router.api.set_block_device_enable,
58 translation_key=
"traffic_meter",
59 entity_category=EntityCategory.CONFIG,
60 update=
lambda router: router.api.get_traffic_meter_enabled,
61 action=
lambda router: router.api.enable_traffic_meter,
64 key=
"parental_control",
65 translation_key=
"parental_control",
66 entity_category=EntityCategory.CONFIG,
67 update=
lambda router: router.api.get_parental_control_enable_status,
68 action=
lambda router: router.api.enable_parental_control,
72 translation_key=
"quality_of_service",
73 entity_category=EntityCategory.CONFIG,
74 update=
lambda router: router.api.get_qos_enable_status,
75 action=
lambda router: router.api.set_qos_enable_status,
79 translation_key=
"2g_guest_wifi",
80 entity_category=EntityCategory.CONFIG,
81 update=
lambda router: router.api.get_2g_guest_access_enabled,
82 action=
lambda router: router.api.set_2g_guest_access_enabled,
86 translation_key=
"5g_guest_wifi",
87 entity_category=EntityCategory.CONFIG,
88 update=
lambda router: router.api.get_5g_guest_access_enabled,
89 action=
lambda router: router.api.set_5g_guest_access_enabled,
93 translation_key=
"smart_connect",
94 entity_category=EntityCategory.CONFIG,
95 update=
lambda router: router.api.get_smart_connect_enabled,
96 action=
lambda router: router.api.set_smart_connect_enabled,
102 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
104 """Set up switches for Netgear component."""
105 router = hass.data[DOMAIN][entry.entry_id][KEY_ROUTER]
109 for description
in ROUTER_SWITCH_TYPES
113 coordinator = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR]
117 def new_device_callback() -> None:
118 """Add new devices if needed."""
120 if not coordinator.data:
123 for mac, device
in router.devices.items():
130 for entity_description
in SWITCH_TYPES
137 entry.async_on_unload(coordinator.async_add_listener(new_device_callback))
139 coordinator.data =
True
140 new_device_callback()
144 """Allow or Block a device from the network."""
146 _attr_entity_registry_enabled_default =
False
150 coordinator: DataUpdateCoordinator,
151 router: NetgearRouter,
153 entity_description: SwitchEntityDescription,
155 """Initialize a Netgear device."""
156 super().
__init__(coordinator, router, device)
162 """Turn the switch on."""
163 await self.
_router_router.async_allow_block_device(self.
_mac_mac, ALLOW)
164 await self.coordinator.async_request_refresh()
167 """Turn the switch off."""
168 await self.
_router_router.async_allow_block_device(self.
_mac_mac, BLOCK)
169 await self.coordinator.async_request_refresh()
173 """Update the Netgear device."""
183 """Representation of a Netgear router switch."""
185 _attr_entity_registry_enabled_default =
False
186 entity_description: NetgearSwitchEntityDescription
190 router: NetgearRouter,
191 entity_description: NetgearSwitchEntityDescription,
193 """Initialize a Netgear device."""
202 """Fetch state when entity is added."""
207 """Poll the state of the switch."""
208 async
with self.
_router_router.api_lock:
209 response = await self.
hasshass.async_add_executor_job(
219 """Turn the switch on."""
220 async
with self.
_router_router.api_lock:
221 await self.
hasshass.async_add_executor_job(
226 """Turn the switch off."""
227 async
with self.
_router_router.api_lock:
228 await self.
hasshass.async_add_executor_job(
None async_update_device(self)
None async_update_device(self)
None async_turn_on(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
None __init__(self, DataUpdateCoordinator coordinator, NetgearRouter router, dict device, SwitchEntityDescription entity_description)
def async_added_to_hass(self)
def async_turn_on(self, **kwargs)
None __init__(self, NetgearRouter router, NetgearSwitchEntityDescription entity_description)
def async_turn_off(self, **kwargs)
IssData update(pyiss.ISS iss)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)