Skip to content

Commit

Permalink
Add
Browse files Browse the repository at this point in the history
- size parsing (for world size and memory), now displaying as float instead of string (example: before "1.1GB", now 1.1 -> value GB -> unit)
  • Loading branch information
Makhuta committed Sep 15, 2024
1 parent 7f61ffc commit 61e4eb3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 12 additions & 1 deletion custom_components/crafty_controller/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@ def find_dict(list, key, value, default = None):
for item in list:
if item.get(key) == value:
return item
return default
return default

def parse_size(input:str) -> tuple[float, str]:
import re
try:
input_match = re.search(r"([\d.]+)([a-zA-Z]+)", input)
input_value = float(input_match.group(1))
input_unit = str(input_match.group(2))
return (input_value, input_unit)
except:
return (0.0, "B")

8 changes: 5 additions & 3 deletions custom_components/crafty_controller/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
CraftySensorEntity,
CraftyServiceEntity,
)
from .helpers import find_dict
from .helpers import find_dict, parse_size

def is_online(data: dict[str, Any]) -> bool:
for key, value in data.items():
Expand Down Expand Up @@ -144,7 +144,8 @@ def __init__(self, coordinator: CraftyDataCoordinator, config_entry: ConfigEntry
self._name = lambda x: f'{find_dict(x.get("servers"), "server_id", server_id).get("server_name", server_id) if find_dict(x.get("servers"), "server_id", server_id) else server_id} Memory'
self._device_name = find_dict(self._coordinator.data.get("servers"), "server_id", server_id).get("server_name", server_id) if find_dict(self._coordinator.data.get("servers"), "server_id", server_id) else server_id
self._model = f'{config_entry.data[CONF_NAME].capitalize() + " " if len(config_entry.data[CONF_NAME]) > 0 else ""}Server'
self._state = lambda x: (find_dict(x.get("servers"), "server_id", server_id).get("mem", 0)) if find_dict(x.get("servers"), "server_id", server_id) else 0
self._state = lambda x: parse_size(find_dict(x.get("servers"), "server_id", server_id).get("mem", "0"))[0] if find_dict(x.get("servers"), "server_id", server_id) else 0
self._unit = lambda x: parse_size(find_dict(x.get("servers"), "server_id", server_id).get("mem", "0"))[1] if find_dict(x.get("servers"), "server_id", server_id) else "B"
self._unique_id = f'{self._host}_{self._port}_Crafty_Controller_server_mem_{server_id}'
self._icon = "mdi:memory"
self._attr_entity_category = EntityCategory.DIAGNOSTIC
Expand Down Expand Up @@ -183,7 +184,8 @@ def __init__(self, coordinator: CraftyDataCoordinator, config_entry: ConfigEntry
self._name = lambda x: f'{find_dict(x.get("servers"), "server_id", server_id).get("server_name", server_id) if find_dict(x.get("servers"), "server_id", server_id) else server_id} World size'
self._device_name = find_dict(self._coordinator.data.get("servers"), "server_id", server_id).get("server_name", server_id) if find_dict(self._coordinator.data.get("servers"), "server_id", server_id) else server_id
self._model = f'{config_entry.data[CONF_NAME].capitalize() + " " if len(config_entry.data[CONF_NAME]) > 0 else ""}Server'
self._state = lambda x: (find_dict(x.get("servers"), "server_id", server_id).get("world_size", 0)) if find_dict(x.get("servers"), "server_id", server_id) else 0
self._state = lambda x: parse_size(find_dict(x.get("servers"), "server_id", server_id).get("world_size", "0"))[0] if find_dict(x.get("servers"), "server_id", server_id) else 0
self._unit = lambda x: parse_size(find_dict(x.get("servers"), "server_id", server_id).get("world_size", "0"))[1] if find_dict(x.get("servers"), "server_id", server_id) else "B"
self._unique_id = f'{self._host}_{self._port}_Crafty_Controller_server_world_size_{server_id}'
self._icon = "mdi:earth"
self._attr_entity_category = EntityCategory.DIAGNOSTIC
Expand Down

0 comments on commit 61e4eb3

Please sign in to comment.