diff --git a/custom_components/keenetic_api/binary_sensor.py b/custom_components/keenetic_api/binary_sensor.py index de02f7d..e340575 100644 --- a/custom_components/keenetic_api/binary_sensor.py +++ b/custom_components/keenetic_api/binary_sensor.py @@ -30,6 +30,7 @@ class KeeneticBinarySensorEntityDescription(BinarySensorEntityDescription): """Describes Keenetic sensor entity.""" value_fn: Callable[[KeeneticRouterCoordinator], bool] + attributes_fn: Callable[[KeeneticRouterCoordinator], bool] | None = None BINARY_SENSOR_TYPES: dict[str, KeeneticBinarySensorEntityDescription] = { @@ -43,9 +44,19 @@ class KeeneticBinarySensorEntityDescription(BinarySensorEntityDescription): device_class=BinarySensorDeviceClass.CONNECTIVITY, value_fn= lambda coordinator, obj_id: coordinator.data.show_interface[obj_id].get('connected', "no") == "yes", ), + "connected_to_media": KeeneticBinarySensorEntityDescription( + key="connected_to_media", + device_class=BinarySensorDeviceClass.CONNECTIVITY, + value_fn= lambda coordinator, obj_id: coordinator.data.show_media.get(obj_id, False), + attributes_fn=lambda coordinator, obj_id: { + "media": coordinator.data.show_media.get(obj_id, None), + }, + ), } + + async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, @@ -69,6 +80,10 @@ async def async_setup_entry( ) ) + for usb in coordinator.data.show_rc_system_usb: + name_media = f"Media{int(usb['port'])-1}" + binary_sensors.append(KeeneticBinarySensorEntity(coordinator, BINARY_SENSOR_TYPES["connected_to_media"], name_media, name_media)) + async_add_entities(binary_sensors, False) @@ -103,3 +118,10 @@ def available(self) -> bool: return True else: return self.coordinator.last_update_success + + @property + def extra_state_attributes(self) -> dict[str, str] | None: + if self.entity_description.attributes_fn is not None: + return self.entity_description.attributes_fn(self.coordinator, self._obj_id) + else: + return None \ No newline at end of file diff --git a/custom_components/keenetic_api/const.py b/custom_components/keenetic_api/const.py index 7661015..a86bd73 100644 --- a/custom_components/keenetic_api/const.py +++ b/custom_components/keenetic_api/const.py @@ -34,5 +34,5 @@ DEFAULT_BACKUP_TYPE_FILE: Final = ["config"] -COUNT_REPEATED_REQUEST_FIREWARE: Final = 15 +COUNT_REPEATED_REQUEST_FIREWARE: Final = 30 TIMER_REPEATED_REQUEST_FIREWARE: Final = 0.3 \ No newline at end of file diff --git a/custom_components/keenetic_api/icons.json b/custom_components/keenetic_api/icons.json index ab3d573..5584771 100644 --- a/custom_components/keenetic_api/icons.json +++ b/custom_components/keenetic_api/icons.json @@ -29,16 +29,25 @@ "sensor": { "clients_wifi": { "default": "mdi:sitemap-outline" - }, + }, "memory": { "default": "mdi:memory" - }, + }, "cpuload": { "default": "mdi:cpu-32-bit" - }, + }, "wan_ip_adress": { "default": "mdi:ip-network" + } + }, + "binary_sensor": { + "connected_to_media": { + "default": "mdi:play-network", + "state": { + "off": "mdi:play-network-outline", + "on": "mdi:play-network" } + } } }, "services": { diff --git a/custom_components/keenetic_api/keenetic.py b/custom_components/keenetic_api/keenetic.py index c9acea4..66c3b45 100644 --- a/custom_components/keenetic_api/keenetic.py +++ b/custom_components/keenetic_api/keenetic.py @@ -24,6 +24,7 @@ class KeeneticFullData: priority_interface: dict[str, Any] show_rc_ip_http: dict[str, Any] show_rc_system_usb: dict[str, Any] + show_media: dict[str, Any] @dataclass class DataDevice(): @@ -302,6 +303,7 @@ async def custom_request(self): data_json_send.append({"show": {"associations": {}}}) data_json_send.append({"show": {"rc": {"system": {}}}}) data_json_send.append({"show": {"rc": {"ip": {"http": {}}}}}) + data_json_send.append({"show": {"media": {}}}) if self.hw_type == "router": data_json_send.append({"show": {"ip": {"hotspot": {}}}}) @@ -317,6 +319,7 @@ async def custom_request(self): show_associations = full_info_other[2]['show']['associations'] show_rc_system_usb = full_info_other[3]['show']['rc']['system'].get('usb', []) show_rc_ip_http = full_info_other[4]['show']['rc']['ip']['http'] + show_media = full_info_other[5]['show']['media'] show_ip_hotspot = {} show_rc_ip_static = {} @@ -325,7 +328,7 @@ async def custom_request(self): if self.hw_type == "router": - data_show_ip_hotspot = full_info_other[5]['show']['ip']['hotspot']['host'] + data_show_ip_hotspot = full_info_other[6]['show']['ip']['hotspot']['host'] for hotspot in data_show_ip_hotspot: show_ip_hotspot[hotspot["mac"]] = DataDevice( hotspot.get('mac'), @@ -340,9 +343,9 @@ async def custom_request(self): hotspot.get('txbytes'), ) - priority_interface = full_info_other[6]['show']['rc']['interface']['ip']['global'] + priority_interface = full_info_other[7]['show']['rc']['interface']['ip']['global'] - data_show_rc_ip_static = full_info_other[7]['show']['rc']['ip']['static'] + data_show_rc_ip_static = full_info_other[8]['show']['rc']['ip']['static'] for port_frw in data_show_rc_ip_static: nm_pfrw = port_frw.get('comment', port_frw.get('index')) nm_pfrw = nm_pfrw if nm_pfrw != "" else port_frw.get('index') @@ -358,7 +361,7 @@ async def custom_request(self): port_frw.get('disable', False), ) - data_show_ip_hotspot_policy = full_info_other[8]['show']['rc']['ip']['hotspot']['host'] + data_show_ip_hotspot_policy = full_info_other[9]['show']['rc']['ip']['hotspot']['host'] for hotspot_pl in data_show_ip_hotspot_policy: show_ip_hotspot_policy[hotspot_pl["mac"]] = hotspot_pl @@ -372,4 +375,5 @@ async def custom_request(self): priority_interface, show_rc_ip_http, show_rc_system_usb, + show_media, ) diff --git a/custom_components/keenetic_api/translations/en.json b/custom_components/keenetic_api/translations/en.json index 476b83b..9d19f0d 100644 --- a/custom_components/keenetic_api/translations/en.json +++ b/custom_components/keenetic_api/translations/en.json @@ -116,6 +116,9 @@ "binary_sensor": { "connected_to_interface": { "name": "{name} Connected" + }, + "connected_to_media": { + "name": "{name}" } }, "switch": {