from dataclasses import dataclass from typing import Any, Iterable @dataclass class SensorData: mac: str value: Any time: int | None = None name: str | None = None def create_nm_req( dev_mac: str, dev_name: str | None = None, sensors: Iterable[SensorData] = ()) -> str: '''Generate a NarodMon format (#mac#value) request''' res = '#' + dev_mac if dev_name is not None: res += '#' + dev_name res += '\n' for s in sensors: res += f'#{s.mac}#{s.value}' if s.time is not None: res += f'#{s.time}' if s.name is not None: res += '#' + s.name res += '\n' return res