Skip to content

Commit

Permalink
The device name has been corrected, attributes have been added, and t…
Browse files Browse the repository at this point in the history
…he documentation has been updated
  • Loading branch information
vmakeev committed Sep 30, 2024
1 parent e371945 commit 23503a8
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Home Assistant custom component for control Huawei mesh routers over LAN.
- control of the Fast Roaming function (802.11r)
- control of the Target Wake Time (reduce power consumption of Wi-Fi 6 devices in sleep mode)
- port mapping switches
- Internet access time control switches
- reboot buttons
- events for connecting, disconnecting, or moving devices over a mesh network
- automatic detection of available functions
Expand Down Expand Up @@ -153,6 +154,7 @@ You can attach one or more tags to each client device in order to be able to use
* Website filtering ([read more](docs/controls.md#website-filtering))
* Guest network ([read more](docs/controls.md#guest-network))
* Port mapping ([read more](docs/controls.md#port-mapping))
* Internet access time ([read more](docs/controls.md#internet-access-time))

### Selects
* Wi-Fi access control mode ([read more](docs/controls.md#wi-fi-access-control-mode))
Expand Down
32 changes: 29 additions & 3 deletions custom_components/huawei_mesh_router/client/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,16 +472,42 @@ def id(self) -> str:
@property
def name(self) -> str:
"""Return the name of the item."""
parts = [part for part in self.id.split(".") if part]
last_non_empty = parts[-1] if parts else "?"
return f"Time limit rule {last_non_empty}"
device_names = [
item.get("HostName", "device") for item in self._data.get("DeviceNames", [])
]
days = [
active_day.day_of_week.value
for active_day in filter(lambda x: x.is_enabled, self._days.values())
]

device_names_str = (
"for " + ", ".join(device_names)
if len(device_names) > 0
else "for some devices"
)

if len(days) == 7:
days_str = "for every day"
elif days == ["Saturday", "Sunday"]:
days_str = "on weekends"
elif days == ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]:
days_str = "on working days"
else:
days_str = "on " + ", ".join(days)

return f"Time limit rule {device_names_str} {days_str}"

@property
def enabled(self) -> bool:
"""Return the state of the item."""
value = self._data.get("Enable")
return isinstance(value, bool) and value

@property
def devices_mac(self) -> Iterable[MAC_ADDR]:
"""Return the state of the item."""
return [device.get("MACAddress", "no_mac_addr") for device in self._data.get("Devices", [])]

@property
def days(self) -> dict[DayOfWeek, HuaweiTimeControlItemDay]:
"""Return the schedule for each day."""
Expand Down
24 changes: 24 additions & 0 deletions custom_components/huawei_mesh_router/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,30 @@ def _handle_coordinator_update(self) -> None:
f"{_FUNCTION_DISPLAYED_NAME_TIME_CONTROL}: {self._time_control.name}"
)

for day in self._time_control.days.values():
self._attr_extra_state_attributes[day.day_of_week.value.lower()] = (
{
"is_enabled": day.is_enabled,
"start_time": day.start,
"end_time": day.end,
}
if day.is_enabled
else {"is_enabled": day.is_enabled}
)

if self.coordinator.connected_devices:
device_index = 1
for device_mac in self._time_control.devices_mac:
device: ConnectedDevice = self.coordinator.connected_devices.get(
device_mac
)
if device:
self._attr_extra_state_attributes[f"device_{device_index}"] = {
"mac": device.mac,
"name": device.name,
}
device_index += 1

super()._handle_coordinator_update()

@property
Expand Down
29 changes: 29 additions & 0 deletions docs/controls.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,35 @@ _Note: these switches are not attached to a specific router device._

_Note: if the port forwarding is removed in the Primary router, the corresponding switch will be removed from the Home Assistant._

### Internet access time

Allows you to enable or disable Internet access time control.

![Access time control switch](images/switch_time_access_control.png)

One switch is created for each forwarded port in the router:
* `switch.<integration_name>_time_control_time_limit_rule_<rule_id>`

__Attention__: The rule identifier is generated by the router. When deleting a rule and creating a new one, the identifier of the new rule will most likely match the identifier of the deleted rule. This should be taken into account when using these switches.

Each switch exposes the following attributes:

| Attribute | Description |
|-------------------------------------|-----------------------------------------------------------------------------------------------------------------|
| `<the name of the day of the week>` | `is_enabled`: Is the filter enabled on that day; `start_time`: Access start time; `end_time`: Access end time. |
| `device_<number>` | `mac`: The MAC address of the device to which the restrictions apply; `name`: The name of the device |

You can configure Internet Access Time Limit in the web interface of your router.
Example address: `http://192.168.3.1/html/index.html#/more/parentcontrol`

![Internet Access Time Limit setup](images/parental_control_access_time.png)

These switches will not be added to Home Assistant if the Primary router does not support access time limits, or this feature is not enabled in [advanced options](../README.md#advanced-options).

_Note: these switches are not attached to a specific router device._

_Note: if the access time limit rule is removed in the Primary router, the corresponding switch will be removed from the Home Assistant._

### Guest network

Allows you to enable or disable the guest Wi-Fi network.
Expand Down
Binary file modified docs/images/options_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/parental_control_access_time.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/switch_time_access_control.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 23503a8

Please sign in to comment.