diff --git a/TheengsExplorer/__init__.py b/TheengsExplorer/__init__.py index 7c0ee75..65e96ef 100644 --- a/TheengsExplorer/__init__.py +++ b/TheengsExplorer/__init__.py @@ -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": @@ -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.""" @@ -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( @@ -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() diff --git a/TheengsExplorer/widgets/advertisement.py b/TheengsExplorer/widgets/advertisement.py index e13bb22..e525414 100644 --- a/TheengsExplorer/widgets/advertisement.py +++ b/TheengsExplorer/widgets/advertisement.py @@ -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 @@ -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] @@ -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)" @@ -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( @@ -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) diff --git a/TheengsExplorer/widgets/decoded.py b/TheengsExplorer/widgets/decoded.py index 3ec1614..05f70a5 100644 --- a/TheengsExplorer/widgets/decoded.py +++ b/TheengsExplorer/widgets/decoded.py @@ -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"] @@ -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) diff --git a/TheengsExplorer/widgets/device.py b/TheengsExplorer/widgets/device.py index 2416dab..ab06866 100644 --- a/TheengsExplorer/widgets/device.py +++ b/TheengsExplorer/widgets/device.py @@ -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) diff --git a/TheengsExplorer/widgets/rssi.py b/TheengsExplorer/widgets/rssi.py index c612951..5fdb4ec 100644 --- a/TheengsExplorer/widgets/rssi.py +++ b/TheengsExplorer/widgets/rssi.py @@ -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" diff --git a/TheengsExplorer/widgets/time.py b/TheengsExplorer/widgets/time.py index aa7dfce..31a6857 100644 --- a/TheengsExplorer/widgets/time.py +++ b/TheengsExplorer/widgets/time.py @@ -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"))