|
93 | 93 | DBUS_NAME = "org.jfhbrook.crystalfontz" |
94 | 94 |
|
95 | 95 |
|
96 | | -async def load_client(config_file: Optional[str]) -> Client: |
97 | | - config: Config = Config.from_file(config_file) |
98 | | - |
99 | | - client = await create_connection(config.port) |
100 | | - |
101 | | - return client |
102 | | - |
103 | | - |
104 | 96 | class DbusReportHandler(ReportHandler): |
105 | | - def __init__(self: Self, iface: "DbusInterface") -> None: |
106 | | - self.iface = iface |
| 97 | + def __init__(self: Self) -> None: |
| 98 | + self.iface: "Optional[DbusInterface]" = None |
107 | 99 |
|
108 | 100 | async def on_key_activity(self: Self, report: KeyActivityReport) -> None: |
109 | | - """ |
110 | | - This method is called on any new key activity report. |
111 | | - """ |
| 101 | + if not self.iface: |
| 102 | + return |
112 | 103 |
|
113 | 104 | self.iface.key_activity_reports.emit(KeyActivityReportM.pack(report)) |
114 | 105 |
|
115 | 106 | async def on_temperature(self: Self, report: TemperatureReport) -> None: |
116 | | - """ |
117 | | - This method is called on any new temperature report. |
118 | | - """ |
| 107 | + if not self.iface: |
| 108 | + return |
119 | 109 |
|
120 | 110 | self.iface.temperature_reports.emit(TemperatureReportM.pack(report)) |
121 | 111 |
|
122 | 112 |
|
| 113 | +async def load_client( |
| 114 | + report_handler: Optional[DbusReportHandler], config_file: Optional[str] |
| 115 | +) -> Client: |
| 116 | + config: Config = Config.from_file(config_file) |
| 117 | + |
| 118 | + client = await create_connection(config.port, report_handler=report_handler) |
| 119 | + |
| 120 | + return client |
| 121 | + |
| 122 | + |
123 | 123 | class DbusInterface( # type: ignore |
124 | 124 | DbusInterfaceCommonAsync, interface_name=DBUS_NAME # type: ignore |
125 | 125 | ): |
126 | 126 | """ |
127 | 127 | A DBus interface for controlling the Crystalfontz device. |
128 | 128 | """ |
129 | 129 |
|
130 | | - def __init__(self: Self, client: Client, config_file: Optional[str] = None) -> None: |
| 130 | + def __init__( |
| 131 | + self: Self, |
| 132 | + client: Client, |
| 133 | + report_handler: Optional[DbusReportHandler] = None, |
| 134 | + config_file: Optional[str] = None, |
| 135 | + ) -> None: |
131 | 136 | super().__init__() |
132 | 137 | self._config: Config = Config.from_file(config_file) |
133 | 138 | self.client: Client = client |
134 | 139 | self._client_lock: asyncio.Lock = asyncio.Lock() |
| 140 | + self._report_handler = report_handler |
| 141 | + |
| 142 | + if self._report_handler: |
| 143 | + self._report_handler.iface = self |
135 | 144 |
|
136 | 145 | @dbus_property_async(ConfigM.t) |
137 | 146 | def config(self: Self) -> ConfigT: |
|
0 commit comments