device: move status string function to Device and Receiver
This commit is contained in:
parent
87285faf7f
commit
0805ecb511
|
@ -463,6 +463,9 @@ class Device:
|
||||||
|
|
||||||
__nonzero__ = __bool__
|
__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):
|
def __str__(self):
|
||||||
try:
|
try:
|
||||||
name = self.name or self.codename or "?"
|
name = self.name or self.codename or "?"
|
||||||
|
|
|
@ -280,7 +280,7 @@ def _process_hidpp10_notification(device, status, n):
|
||||||
if n.address == 0x01:
|
if n.address == 0x01:
|
||||||
if logger.isEnabledFor(logging.DEBUG):
|
if logger.isEnabledFor(logging.DEBUG):
|
||||||
logger.debug("%s: device powered on", device)
|
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)
|
status.changed(active=True, alert=_ALERT.NOTIFICATION, reason=reason)
|
||||||
else:
|
else:
|
||||||
logger.warning("%s: unknown %s", device, n)
|
logger.warning("%s: unknown %s", device, n)
|
||||||
|
|
|
@ -26,6 +26,7 @@ import hidapi as _hid
|
||||||
from . import base as _base
|
from . import base as _base
|
||||||
from . import exceptions, hidpp10, hidpp10_constants
|
from . import exceptions, hidpp10, hidpp10_constants
|
||||||
from .device import Device
|
from .device import Device
|
||||||
|
from .i18n import _, ngettext
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -331,6 +332,14 @@ class Receiver:
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return self.path.__hash__()
|
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):
|
def __str__(self):
|
||||||
return "<%s(%s,%s%s)>" % (
|
return "<%s(%s,%s%s)>" % (
|
||||||
self.name.replace(" ", ""),
|
self.name.replace(" ", ""),
|
||||||
|
|
|
@ -22,7 +22,6 @@ from . import hidpp10_constants as _hidpp10_constants
|
||||||
from . import hidpp20_constants as _hidpp20_constants
|
from . import hidpp20_constants as _hidpp20_constants
|
||||||
from . import settings as _settings
|
from . import settings as _settings
|
||||||
from .common import Battery, NamedInts
|
from .common import Battery, NamedInts
|
||||||
from .i18n import _, ngettext
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -66,17 +65,6 @@ class ReceiverStatus:
|
||||||
# self.device_passkey = None
|
# self.device_passkey = None
|
||||||
# self.new_device = 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):
|
def changed(self, alert=ALERT.NOTIFICATION, reason=None):
|
||||||
self._changed_callback(self._receiver, alert=alert, reason=reason)
|
self._changed_callback(self._receiver, alert=alert, reason=reason)
|
||||||
|
|
||||||
|
@ -98,9 +86,6 @@ class DeviceStatus:
|
||||||
# self.notification_flags = None
|
# self.notification_flags = None
|
||||||
self.battery_error = None
|
self.battery_error = None
|
||||||
|
|
||||||
def to_string(self):
|
|
||||||
return self.battery.to_str() if self.battery is not None else ""
|
|
||||||
|
|
||||||
def __bool__(self):
|
def __bool__(self):
|
||||||
return bool(self._active)
|
return bool(self._active)
|
||||||
|
|
||||||
|
|
|
@ -116,7 +116,7 @@ if available:
|
||||||
elif dev.status is None:
|
elif dev.status is None:
|
||||||
message = _("unpaired")
|
message = _("unpaired")
|
||||||
elif bool(dev.status):
|
elif bool(dev.status):
|
||||||
message = dev.status.to_string() or _("connected")
|
message = dev.status_string() or _("connected")
|
||||||
else:
|
else:
|
||||||
message = _("offline")
|
message = _("offline")
|
||||||
|
|
||||||
|
|
|
@ -196,12 +196,13 @@ try:
|
||||||
|
|
||||||
def _update_tray_icon():
|
def _update_tray_icon():
|
||||||
if _picked_device and gtk.battery_icons_style != "solaar":
|
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_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
|
battery_charging = device_status.battery.charging() if device_status.battery is not None else None
|
||||||
tray_icon_name = _icons.battery(battery_level, battery_charging)
|
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:
|
else:
|
||||||
# there may be a receiver, but no peripherals
|
# there may be a receiver, but no peripherals
|
||||||
tray_icon_name = _icons.TRAY_OKAY if _devices_info else _icons.TRAY_INIT
|
tray_icon_name = _icons.TRAY_OKAY if _devices_info else _icons.TRAY_INIT
|
||||||
|
@ -302,19 +303,19 @@ def _generate_description_lines():
|
||||||
yield _("no receiver")
|
yield _("no receiver")
|
||||||
return
|
return
|
||||||
|
|
||||||
for _ignore, number, name, status in _devices_info:
|
for _ignore, number, name, device in _devices_info:
|
||||||
if number is None: # receiver
|
if number is None: # receiver
|
||||||
continue
|
continue
|
||||||
|
|
||||||
p = status.to_string()
|
p = device.status_string()
|
||||||
if p: # does it have any properties to print?
|
if p: # does it have any properties to print?
|
||||||
yield "<b>%s</b>" % name
|
yield "<b>%s</b>" % name
|
||||||
if status:
|
if device.online:
|
||||||
yield "\t%s" % p
|
yield "\t%s" % p
|
||||||
else:
|
else:
|
||||||
yield "\t%s <small>(" % p + _("offline") + ")</small>"
|
yield "\t%s <small>(" % p + _("offline") + ")</small>"
|
||||||
else:
|
else:
|
||||||
if status:
|
if device.online:
|
||||||
yield "<b>%s</b> <small>(" % name + _("no status") + ")</small>"
|
yield "<b>%s</b> <small>(" % name + _("no status") + ")</small>"
|
||||||
else:
|
else:
|
||||||
yield "<b>%s</b> <small>(" % name + _("offline") + ")</small>"
|
yield "<b>%s</b> <small>(" % name + _("offline") + ")</small>"
|
||||||
|
@ -330,7 +331,7 @@ def _pick_device_with_lowest_battery():
|
||||||
for info in _devices_info:
|
for info in _devices_info:
|
||||||
if info[1] is None: # is receiver
|
if info[1] is None: # is receiver
|
||||||
continue
|
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)
|
# print ("checking %s -> %s", info, level)
|
||||||
if level is not None and picked_level > level:
|
if level is not None and picked_level > level:
|
||||||
picked = info
|
picked = info
|
||||||
|
@ -366,7 +367,7 @@ def _add_device(device):
|
||||||
break
|
break
|
||||||
index = index + 1
|
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)
|
_devices_info.insert(index, new_device_info)
|
||||||
|
|
||||||
label_prefix = " "
|
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
|
charging = device.status.battery.charging() if device.status.battery is not None else None
|
||||||
icon_name = _icons.battery(level, charging)
|
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 = menu_item.get_image()
|
||||||
image_widget.set_sensitive(bool(device.online))
|
image_widget.set_sensitive(bool(device.online))
|
||||||
_update_menu_icon(image_widget, icon_name)
|
_update_menu_icon(image_widget, icon_name)
|
||||||
|
|
Loading…
Reference in New Issue