tomaae / homeassistant-mikrotik_router

Mikrotik router integration for Home Assistant
Apache License 2.0
301 stars 50 forks source link

[Feature] Kid control rules disable/enable #147

Closed sshalyminov closed 2 years ago

sshalyminov commented 2 years ago

Current kids control feature adds switch per kid control rule which is pause/restore kid access to internet via call Pause/Resume commands - this way completely disable access via firewall rule on Pause or enable access via removing this firewall rule on Resume. Is it possible to add additional switch which will enable/disable kid control rule itself?

Why do I need this: my rules automatically enable access for kids in time frame 8am-9pm. If I need to enable access out of this time frame, I need go to router console and disable kid control rule manually. Much better to have this in HA console.

Also it would be great if time values editing feature added - for me does not sence if only one for time frame begin and one for end, not for all days. In this case "begin value" can be used for all days both for enable and unlimited rate, and the same for "end value". Or may be 2 "begins" and 2 "ends" (one for enablement and another for unlimited rate) - may be this will be important for someone.

sshalyminov commented 2 years ago

image this is what I mean in console

tomaae commented 2 years ago

isnt it same as pause/restore? I know nothing of how kid control works. if it is different, it would be best if you could explain it to me. so I know what I'm doing :)

sshalyminov commented 2 years ago

It is not same. Pause feature creates FW rule totally disabling kid access. Resume feature removes this FW rule. This is what we have now already implemented in switch. This can be used for immediate disable acces "right now". Kid rule can be probrammed to disable kid access in desired time and enable it back automatically. In this case when need to disable, router creates disable FW rule (different than in previous commands). And removes it automatically at morning time set in kid rule. If I press "disable kid control rule" - router disables this rule and remove automatically created FW rule - access granted. When I press "enable kid control rule" - router will enable it and depends on what time is now - creates or not disabling FW rule. What I had requested - is to add this feature "enable/disable kid control rule" in additional switch in HA. And - if possible - ability to edit time frames. may be not all 14 but at least same value for all 7 days and for both modes.

tomaae commented 2 years ago

Ok, I get it now. So they basically work in opposite ways, makes sense.

tomaae commented 2 years ago

I will add it in next feature update. Will be a big one from the looks of it. But may take a while, since I dont have dev environment available right now.

sshalyminov commented 2 years ago

https://wiki.mikrotik.com/wiki/Manual:Kid-control If I understand right, for now you use: /ip kid-control pause artem /ip kid-control resume artem

I am requesting: /ip kid-control disable artem /ip kid-control enable artem

And for time: /ip kid-control set artem mon "08:00-21:45" tue "08:00-21:45" wed "08:00-21:45" thu "08:00-21:45" fri "08:00-21:45" sat "08:00-21:45" sun "08:00-21:45" For rate limi time: /ip kid-control set artem tur-mon "08:00-21:45" tur-tue "08:00-21:45" tur-wed "08:00-21:45" tur-thu "08:00-21:45" tur-fri "08:00-21:45" tur-sat "08:00-21:45" tur-sun "08:00-21:45" For rate limit value: /ip kid-control set artem rate-limit=1024

I've checked these commands aready :)

Of course all "set" commands can be combined to one if needed.

sshalyminov commented 2 years ago

For me exactly right now is good to have enable/disable - if you can implement it even without testing (just by copying everything from pause/resume) - would be great, I will test it but I sure it will work as it is exactly same with just changing command "pause/resume" to "enable/disable". For time editing - it is "nice to have" and can be implemented some time later.

tomaae commented 2 years ago

Can you test master?

sshalyminov commented 2 years ago

Excellent! Switches working as expected!

sshalyminov commented 2 years ago

Hm, by the way - all router sensors are died on master: CPU, Memory, HDD, Temperature, Uptime, PPoE client state and traffic, ports traffic - everything becomes unavailable. Rolling back to last release...

sshalyminov commented 2 years ago

Log:

2021-12-16 01:16:12 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.mikrotik_router
2021-12-16 01:16:12 INFO (MainThread) [homeassistant.components.binary_sensor] Setting up binary_sensor.mikrotik_router
2021-12-16 01:16:12 INFO (MainThread) [homeassistant.components.device_tracker] Setting up device_tracker.mikrotik_router
2021-12-16 01:16:12 INFO (MainThread) [homeassistant.components.switch] Setting up switch.mikrotik_router
2021-12-16 01:16:12 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up mikrotik_router platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 249, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/mikrotik_router/sensor.py", line 245, in async_setup_entry
    update_controller()
  File "/config/custom_components/mikrotik_router/sensor.py", line 235, in update_controller
    update_items(
  File "/config/custom_components/mikrotik_router/sensor.py", line 332, in update_items
    sensors[item_id] = MikrotikControllerTrafficSensor(
  File "/config/custom_components/mikrotik_router/sensor.py", line 490, in __init__
    super().__init__(mikrotik_controller, inst, sensor)
  File "/config/custom_components/mikrotik_router/sensor.py", line 383, in __init__
    self._unit = self._data[SENSOR_TYPES[self._sensor][ATTR_UNIT_ATTR]]
KeyError: 'tx-bits-per-second-attr'
2021-12-16 01:16:13 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new switch.mikrotik_router entity: switch.mikrotik_router_kid_control_pause_artem
2021-12-16 01:16:13 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new switch.mikrotik_router entity: switch.mikrotik_router_kid_control_pause_danya
2021-12-16 01:16:13 ERROR (MainThread) [homeassistant.components.binary_sensor] Error adding entities for domain binary_sensor with platform mikrotik_router
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 382, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 613, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 748, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 505, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 541, in _async_write_ha_state
    extra_state_attributes = self.extra_state_attributes
  File "/config/custom_components/mikrotik_router/binary_sensor.py", line 510, in extra_state_attributes
    for variable in self._sid_data["sid_attr"]:
KeyError: 'sid_attr'
2021-12-16 01:16:13 ERROR (MainThread) [homeassistant.components.binary_sensor] Error while setting up mikrotik_router platform for binary_sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 257, in _async_setup_platform
    await asyncio.gather(*pending)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 382, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 613, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 748, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 505, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 541, in _async_write_ha_state
    extra_state_attributes = self.extra_state_attributes
  File "/config/custom_components/mikrotik_router/binary_sensor.py", line 510, in extra_state_attributes
    for variable in self._sid_data["sid_attr"]:
KeyError: 'sid_attr'

2021-12-16 01:16:43 ERROR (MainThread) [homeassistant.util.logging] Exception in update_controller when dispatching 'mikrotik_router-update-Mikrotik Router': ()
Traceback (most recent call last):
  File "/config/custom_components/mikrotik_router/sensor.py", line 235, in update_controller
    update_items(
  File "/config/custom_components/mikrotik_router/sensor.py", line 281, in update_items
    sensors[item_id].async_schedule_update_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 660, in async_schedule_update_ha_state
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 498, in async_write_ha_state
    raise RuntimeError(f"Attribute hass is None for {self}")
RuntimeError: Attribute hass is None for <Entity Mikrotik Router defconfMode>

2021-12-16 01:16:43 ERROR (MainThread) [homeassistant.util.logging] Exception in update_controller when dispatching 'mikrotik_router-update-Mikrotik Router': ()
Traceback (most recent call last):
  File "/config/custom_components/mikrotik_router/binary_sensor.py", line 159, in update_controller
    update_items(
  File "/config/custom_components/mikrotik_router/binary_sensor.py", line 214, in update_items
    sensors[item_id].async_schedule_update_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 660, in async_schedule_update_ha_state
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 505, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 541, in _async_write_ha_state
    extra_state_attributes = self.extra_state_attributes
  File "/config/custom_components/mikrotik_router/binary_sensor.py", line 510, in extra_state_attributes
    for variable in self._sid_data["sid_attr"]:
KeyError: 'sid_attr'
sshalyminov commented 2 years ago

Version 1.7.3 - all sensors are working. Looks like in master something broken

tomaae commented 2 years ago

hmm, works for me. which integration features are you using? it could be some combination.

sshalyminov commented 2 years ago

Not sure in correct translation, I'm using it in Russian:

Port tracking sensors: on Port traffic sensors: on Track network devices: on Clients traffic sensors: on Simple queues switches: off NAT rules switches: off Mangle rules switches: off Filter switches: off Child control: on PPP users: on Scripts switches: on Environment sensors: on image

tomaae commented 2 years ago

Found it, it was related to different feature I added yesterday. Should work fine now

sshalyminov commented 2 years ago

Looks like it's working now. Thank you very much!