Skip to content

Commit

Permalink
non-admin user will have binary sensor for status of interface instea…
Browse files Browse the repository at this point in the history
…d of switch
  • Loading branch information
elad-bar committed Oct 12, 2022
1 parent 1bbca68 commit 4ed3609
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.0.5

- Since user with operator role cannot update interface status, non-admin user will have binary sensor for status of interface instead of switch, in addition, an INFO log message will explain it

## 2.0.4

- Add IP address to status switch of its interface [#71](https://github.com/elad-bar/ha-edgeos/issues/71)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Provides an integration between EdgeOS (Ubiquiti) routers to Home Assistant.
- EdgeRouter with at least firmware version 2.0
- EdgeRouter User with 'Operator' level access or higher
- Traffic Analysis set to 'Enabled' (both `dpi` and `export` enabled under `system/traffic-analysis`)
- To enable / disable interfaces an `admin` role is a required

#### Installations via HACS
- In HACS, look for "Ubiquiti EdgeOS Routers" and install and restart
Expand Down
5 changes: 5 additions & 0 deletions custom_components/edgeos/component/helpers/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@
SYSTEM_DATA_TRAFFIC_ANALYSIS_DPI = "dpi"
SYSTEM_DATA_TRAFFIC_ANALYSIS_EXPORT = "export"
SYSTEM_DATA_TIME_ZONE = "time-zone"
SYSTEM_DATA_LOGIN = "login"
SYSTEM_DATA_LOGIN_USER = "user"
SYSTEM_DATA_LOGIN_USER_LEVEL = "level"

USER_LEVEL_ADMIN = "admin"

SYSTEM_INFO_DATA_FW_LATEST = "fw-latest"
SYSTEM_INFO_DATA_FW_LATEST_STATE = "state"
Expand Down
51 changes: 50 additions & 1 deletion custom_components/edgeos/component/managers/home_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,12 @@ def load_entities(self):
for unique_id in self._interfaces:
interface_item = self._interfaces.get(unique_id)
self._load_interface_monitor_switch(interface_item)
self._load_interface_status_switch(interface_item)

if self._system.user_level == USER_LEVEL_ADMIN:
self._load_interface_status_switch(interface_item)

else:
self._load_interface_status_binary_sensor(interface_item)

self._load_interface_received_rate_sensor(interface_item)
self._load_interface_received_traffic_sensor(interface_item)
Expand Down Expand Up @@ -388,7 +393,19 @@ def _extract_system(self, data: dict, system_info: dict):

system_data.sw_version = sw_latest

login_details = system_details.get(SYSTEM_DATA_LOGIN, {})
users = login_details.get(SYSTEM_DATA_LOGIN_USER, {})
current_user = users.get(self.config_data.username, {})
system_data.user_level = current_user.get(SYSTEM_DATA_LOGIN_USER_LEVEL)

self._system = system_data

if system_data.user_level != USER_LEVEL_ADMIN:
_LOGGER.info(
f"User {self.config_data.username} level is {self._system.user_level}, "
f"Interface status switch will not be created as it requires admin role"
)

except Exception as ex:
exc_type, exc_obj, tb = sys.exc_info()
line_number = tb.tb_lineno
Expand Down Expand Up @@ -1256,6 +1273,38 @@ def _load_interface_status_switch(self, interface: EdgeOSInterfaceData):
ex, f"Failed to load switch for {entity_name}"
)

def _load_interface_status_binary_sensor(self, interface: EdgeOSInterfaceData):
interface_name = self._get_interface_name(interface)
entity_name = f"{interface_name} Status"

try:
state = interface.up

attributes = {
ATTR_FRIENDLY_NAME: entity_name,
ADDRESS_LIST: interface.address
}

unique_id = EntityData.generate_unique_id(DOMAIN_BINARY_SENSOR, entity_name)

entity_description = BinarySensorEntityDescription(
key=unique_id,
name=entity_name,
device_class=BinarySensorDeviceClass.CONNECTIVITY
)

self.entity_manager.set_entity(DOMAIN_BINARY_SENSOR,
self.entry_id,
state,
attributes,
interface_name,
entity_description)

except Exception as ex:
self.log_exception(
ex, f"Failed to load binary sensor for {entity_name}"
)

def _load_interface_monitor_switch(self, interface: EdgeOSInterfaceData):
interface_name = self._get_interface_name(interface)
entity_name = f"{interface_name} Monitored"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class EdgeOSSystemData:
cpu: int | None
mem: int | None
last_reset: datetime | None
user_level: str | None

def __init__(self):
self.hostname = None
Expand All @@ -45,6 +46,7 @@ def __init__(self):
self.upgrade_available = False
self.upgrade_url = None
self.upgrade_version = None
self.user_level = None

@staticmethod
def is_enabled(data: dict, key: str) -> bool:
Expand All @@ -61,7 +63,8 @@ def to_dict(self):
SYSTEM_DATA_OFFLOAD_HW_NAT: self.hardware_offload,
SYSTEM_DATA_TRAFFIC_ANALYSIS_DPI: self.deep_packet_inspection,
SYSTEM_DATA_TRAFFIC_ANALYSIS_EXPORT: self.traffic_analysis_export,
DHCP_SERVER_LEASES: self.leased_devices
DHCP_SERVER_LEASES: self.leased_devices,
SYSTEM_DATA_LOGIN_USER_LEVEL: self.user_level
}

return obj
Expand Down
2 changes: 1 addition & 1 deletion custom_components/edgeos/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"codeowners": ["@elad-bar"],
"requirements": ["aiohttp"],
"config_flow": true,
"version": "2.0.4",
"version": "2.0.5",
"iot_class": "local_polling"
}
5 changes: 5 additions & 0 deletions info.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Provides an integration between EdgeOS (Ubiquiti) routers to Home Assistant.
- EdgeRouter with at least firmware version 2.0
- EdgeRouter User with 'Operator' level access or higher
- Traffic Analysis set to 'Enabled' (both `dpi` and `export` enabled under `system/traffic-analysis`)
- To enable / disable interfaces an `admin` role is a required

#### Installations via HACS
- In HACS, look for "Ubiquiti EdgeOS Routers" and install and restart
Expand Down Expand Up @@ -129,6 +130,8 @@ Allows to set:
- Log incoming messages - Enable / Disable logging of incoming WebSocket messages for debug
- Store debug data - Enable / Disable store debug data to './storage' directory of HA for API (edgeos.debug.api.json) and WS (edgeos.debug.ws.json) data for faster debugging or just to get more ideas for additional features
- Unit of measurement
- Update API interval - Interval in seconds to update API data
- Update Entities interval - Interval in seconds to update entities

More details available in `Developer tools` -> `Services` -> `edgeos.update_configuration`

Expand All @@ -140,6 +143,8 @@ data:
store_debug_data: true
log_incoming_messages: true
consider_away_interval: 180
update_api_interval: 30
update_entities_interval: 1
```

*Changing the unit will reload the integration*

0 comments on commit 4ed3609

Please sign in to comment.