device: move status string function to Device and Receiver

This commit is contained in:
Peter F. Patel-Schneider 2024-03-09 16:14:17 -05:00
parent 87285faf7f
commit 0805ecb511
6 changed files with 24 additions and 26 deletions

View File

@ -463,6 +463,9 @@ class Device:
__nonzero__ = __bool__
def status_string(self):
return self.status.battery.to_str() if hasattr(self, "status") and self.status.battery is not None else ""
def __str__(self):
try:
name = self.name or self.codename or "?"

View File

@ -280,7 +280,7 @@ def _process_hidpp10_notification(device, status, n):
if n.address == 0x01:
if logger.isEnabledFor(logging.DEBUG):
logger.debug("%s: device powered on", device)
reason = status.to_string() or _("powered on")
reason = device.status_string() or _("powered on")
status.changed(active=True, alert=_ALERT.NOTIFICATION, reason=reason)
else:
logger.warning("%s: unknown %s", device, n)

View File

@ -26,6 +26,7 @@ import hidapi as _hid
from . import base as _base
from . import exceptions, hidpp10, hidpp10_constants
from .device import Device
from .i18n import _, ngettext
logger = logging.getLogger(__name__)
@ -331,6 +332,14 @@ class Receiver:
def __hash__(self):
return self.path.__hash__()
def status_string(self):
count = len(self)
return (
_("No paired devices.")
if count == 0
else ngettext("%(count)s paired device.", "%(count)s paired devices.", count) % {"count": count}
)
def __str__(self):
return "<%s(%s,%s%s)>" % (
self.name.replace(" ", ""),

View File

@ -22,7 +22,6 @@ from . import hidpp10_constants as _hidpp10_constants
from . import hidpp20_constants as _hidpp20_constants
from . import settings as _settings
from .common import Battery, NamedInts
from .i18n import _, ngettext
logger = logging.getLogger(__name__)
@ -66,17 +65,6 @@ class ReceiverStatus:
# self.device_passkey = None
# self.new_device = None
def to_string(self):
count = len(self._receiver)
return (
_("No paired devices.")
if count == 0
else ngettext("%(count)s paired device.", "%(count)s paired devices.", count) % {"count": count}
)
def __str__(self):
self.to_string()
def changed(self, alert=ALERT.NOTIFICATION, reason=None):
self._changed_callback(self._receiver, alert=alert, reason=reason)
@ -98,9 +86,6 @@ class DeviceStatus:
# self.notification_flags = None
self.battery_error = None
def to_string(self):
return self.battery.to_str() if self.battery is not None else ""
def __bool__(self):
return bool(self._active)

View File

@ -116,7 +116,7 @@ if available:
elif dev.status is None:
message = _("unpaired")
elif bool(dev.status):
message = dev.status.to_string() or _("connected")
message = dev.status_string() or _("connected")
else:
message = _("offline")

View File

@ -196,12 +196,13 @@ try:
def _update_tray_icon():
if _picked_device and gtk.battery_icons_style != "solaar":
_ignore, _ignore, name, device_status = _picked_device
_ignore, _ignore, name, device = _picked_device
device_status = device.status
battery_level = device_status.battery.level if device_status.battery is not None else None
battery_charging = device_status.battery.charging() if device_status.battery is not None else None
tray_icon_name = _icons.battery(battery_level, battery_charging)
description = "%s: %s" % (name, device_status.to_string())
description = "%s: %s" % (name, device.status_string())
else:
# there may be a receiver, but no peripherals
tray_icon_name = _icons.TRAY_OKAY if _devices_info else _icons.TRAY_INIT
@ -302,19 +303,19 @@ def _generate_description_lines():
yield _("no receiver")
return
for _ignore, number, name, status in _devices_info:
for _ignore, number, name, device in _devices_info:
if number is None: # receiver
continue
p = status.to_string()
p = device.status_string()
if p: # does it have any properties to print?
yield "<b>%s</b>" % name
if status:
if device.online:
yield "\t%s" % p
else:
yield "\t%s <small>(" % p + _("offline") + ")</small>"
else:
if status:
if device.online:
yield "<b>%s</b> <small>(" % name + _("no status") + ")</small>"
else:
yield "<b>%s</b> <small>(" % name + _("offline") + ")</small>"
@ -330,7 +331,7 @@ def _pick_device_with_lowest_battery():
for info in _devices_info:
if info[1] is None: # is receiver
continue
level = info[-1].battery.level if info[-1].battery is not None else None
level = info[-1].status.battery.level if hasattr(info[-1], "status") and info[-1].status.battery is not None else None
# print ("checking %s -> %s", info, level)
if level is not None and picked_level > level:
picked = info
@ -366,7 +367,7 @@ def _add_device(device):
break
index = index + 1
new_device_info = (receiver_path, device.number, device.name, device.status)
new_device_info = (receiver_path, device.number, device.name, device)
_devices_info.insert(index, new_device_info)
label_prefix = " "
@ -431,7 +432,7 @@ def _update_menu_item(index, device):
charging = device.status.battery.charging() if device.status.battery is not None else None
icon_name = _icons.battery(level, charging)
menu_item.set_label((" " if 0 < device.number <= 6 else "") + device.name + ": " + device.status.to_string())
menu_item.set_label((" " if 0 < device.number <= 6 else "") + device.name + ": " + device.status_string())
image_widget = menu_item.get_image()
image_widget.set_sensitive(bool(device.online))
_update_menu_icon(image_widget, icon_name)