Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for use with Textual 0.9.1 #21

Draft
wants to merge 1 commit into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions TheengsExplorer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import platform

from bleak import BleakScanner
from textual.app import App
from textual.widgets import Footer, ScrollView
from textual.app import App, ComposeResult
from textual.widgets import Header, Footer
from textual.scroll_view import ScrollView
from widgets.devicetable import DeviceTable

if platform.system() == "Linux":
Expand Down Expand Up @@ -61,14 +62,16 @@ def __init__(self, config, *args, **kwargs):

async def detection_callback(self, device, advertisement_data) -> None:
"""Process detected advertisement data from device."""
self.device_table.update_device(device, advertisement_data)
await self.scroll_view.update(self.device_table.render(), home=False)

async def on_load(self) -> None:
self.query_one(DeviceTable).update_device(device, advertisement_data)
if isinstance(self.focused, ScrollView):
await self.focused.update(self.device_table.render(), home=False)

def on_load(self) -> None:
"""Bind keys when the app loads."""
await self.bind("q", "quit", "Quit")
await self.bind("s", "toggle_scan", "Toggle scan")
await self.bind("a", "toggle_advertisement", "Show advertisement")
self.bind("q", "quit", description="Quit")
self.bind("s", "toggle_scan", description="Toggle scan")
self.bind("a", "toggle_advertisement", description="Show advertisement")

async def action_toggle_scan(self) -> None:
"""Start or stop BLE scanner."""
Expand All @@ -86,20 +89,23 @@ async def action_toggle_advertisement(self) -> None:
else:
self.config["advertisement"] = True

def compose(self) -> ComposeResult:
"""Create app child widgets."""

yield Header()
yield ScrollView(DeviceTable(self.config));
yield Footer()

async def on_mount(self) -> None:
"""Create ScrollView and start BLE scan."""
self.device_table = DeviceTable(self.config)
self.scroll_view = ScrollView(self.device_table)

await self.view.dock(Footer(), edge="bottom")
await self.view.dock(self.scroll_view, edge="top")

self.query_one(ScrollView).focus()
await self.scanner.start()


if __name__ == "__main__":

config = {"adapter": None, "advertisement": True, "scanning_mode": "active", "temp_unit": "celsius"}
config = {"adapter": None, "advertisement": True, "scanning_mode": "active", "temperature_unit": "celsius"}

parser = ArgumentParser()
parser.add_argument(
Expand Down Expand Up @@ -134,4 +140,6 @@ async def on_mount(self) -> None:
config["scanning_mode"] = args.scanning_mode
config["temperature_unit"] = args.temperature_unit

TheengsExplorerApp.run(config=config, title="Theengs Explorer")
# title="Theengs Explorer"
if __name__ == "__main__":
TheengsExplorerApp(config=config).run()
10 changes: 5 additions & 5 deletions TheengsExplorer/widgets/advertisement.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from bleak.uuids import uuidstr_to_str
from bluetooth_numbers.companies import company
from bluetooth_numbers import company
from rich.table import Table
from rich.text import Text
from rich.tree import Tree
Expand All @@ -15,7 +15,7 @@ def __init__(self, cic: int, compliant: bool):
self.cic = cic
self.compliant = compliant

def render(self) -> Text:
def __rich__(self) -> Text:
"""Render CompanyID widget."""
try:
manufacturer_name = company[self.cic]
Expand All @@ -38,7 +38,7 @@ def __init__(self, data: bytes):
super().__init__()
self.data = data

def render(self) -> Text:
def __rich__(self) -> Text:
"""Render HexData widget."""
return Text.assemble(
(f"0x{bytes(self.data).hex()}", "cyan bold"), f" ({len(self.data)} bytes)"
Expand All @@ -53,7 +53,7 @@ def __init__(self, uuid128: str):
super().__init__()
self.uuid128 = uuid128

def render(self) -> Text:
def __rich__(self) -> Text:
"""Render UUID widget."""
# Colorize the 16-bit UUID part in a standardized 128-bit UUID.
if self.uuid128.startswith("0000") and self.uuid128.endswith(
Expand All @@ -79,7 +79,7 @@ def __init__(self, advertisement_data, cid_compliant):
self.advertisement_data = advertisement_data
self.cid_compliant = cid_compliant

def render(self) -> Text:
def __rich__(self) -> Text:
"""Render Advertisement widget."""
table = Table(show_header=False, show_edge=False, padding=0)

Expand Down
4 changes: 2 additions & 2 deletions TheengsExplorer/widgets/decoded.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, entry, value, device_properties):
self.value = value
self.device_properties = device_properties

def render(self) -> Text:
def __rich__(self) -> Text:
"""Render Property widget."""
if self.entry in self.device_properties:
unit = self.device_properties[self.entry]["unit"]
Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(self, decoded, temperature_unit):
else:
self.hidden_temperature_unit = "tempc"

def render(self) -> Table:
def __rich__(self) -> Table:
"""Render Decoded widget."""
table = Table(show_header=False, show_edge=False, padding=0)

Expand Down
8 changes: 4 additions & 4 deletions TheengsExplorer/widgets/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class Device(Widget):
def __init__(self, name, address, decoded):
"""Initialize Device widget."""
super().__init__()
self.name = name
self.dev_name = name
self.address = address
self.decoded = decoded

def render(self) -> Table:
def __rich__(self) -> Table:
"""Render Device widget."""
table = Table(show_header=False, show_edge=False, padding=0)
if self.name:
table.add_row(Text(self.name, style="green bold"))
if self.dev_name:
table.add_row(Text(self.dev_name, style="green bold"))

table.add_row(self.address)

Expand Down
2 changes: 1 addition & 1 deletion TheengsExplorer/widgets/rssi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __init__(self, rssi: int):
super().__init__()
self.rssi = rssi

def render(self) -> str:
def __rich__(self) -> str:
"""Render RSSI widget."""
if self.rssi is not None:
return f"{self.rssi} dBm"
Expand Down
2 changes: 1 addition & 1 deletion TheengsExplorer/widgets/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, time, previous_time):
self.time = time
self.previous_time = previous_time

def render(self) -> Table:
def __rich__(self) -> Table:
"""Render Time widget."""
table = Table(show_header=False, show_edge=False, padding=0)
table.add_row(self.time.strftime("%H:%M:%S.%f"))
Expand Down