Skip to content

Commit

Permalink
добавлены объекты binary_sensor статуса media
Browse files Browse the repository at this point in the history
  • Loading branch information
malinovsku committed Aug 11, 2024
1 parent 42700bc commit e7ec281
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
22 changes: 22 additions & 0 deletions custom_components/keenetic_api/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand All @@ -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,
Expand All @@ -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)


Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion custom_components/keenetic_api/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 12 additions & 3 deletions custom_components/keenetic_api/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
12 changes: 8 additions & 4 deletions custom_components/keenetic_api/keenetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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": {}}}})
Expand All @@ -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 = {}
Expand All @@ -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'),
Expand All @@ -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')
Expand All @@ -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

Expand All @@ -372,4 +375,5 @@ async def custom_request(self):
priority_interface,
show_rc_ip_http,
show_rc_system_usb,
show_media,
)
3 changes: 3 additions & 0 deletions custom_components/keenetic_api/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@
"binary_sensor": {
"connected_to_interface": {
"name": "{name} Connected"
},
"connected_to_media": {
"name": "{name}"
}
},
"switch": {
Expand Down

0 comments on commit e7ec281

Please sign in to comment.