async def main(): host = argv[1] if len(argv) > 1 else HOST kind = argv[2] if len(argv) > 2 else "laser" # argument kind: laser - for laser printer # ink - for inkjet printer external_snmp = False if len(argv) > 3 and argv[3] == "use_external_snmp": external_snmp = True if external_snmp: print("Using external SNMP engine") snmp_engine = hlapi.SnmpEngine() brother = Brother(host, kind=kind, snmp_engine=snmp_engine) else: brother = Brother(host, kind=kind) try: data = await brother.async_update() except (ConnectionError, SnmpError, UnsupportedModel) as error: print(f"{error}") return brother.shutdown() print(f"Model: {brother.model}") print(f"Firmware: {brother.firmware}") if data: print(f"Status: {data.status}") print(f"Serial no: {data.serial}") print(f"Sensors data: {data}")
def get_snmp_engine(opp: OpenPeerPower) -> hlapi.SnmpEngine: """Get SNMP engine.""" _LOGGER.debug("Creating SNMP engine") snmp_engine = hlapi.SnmpEngine() @callback def shutdown_listener(ev: Event) -> None: if opp.data.get(DOMAIN): _LOGGER.debug("Unconfiguring SNMP engine") lcd.unconfigure(opp.data[DOMAIN][SNMP], None) opp.bus.async_listen_once(EVENT_OPENPEERPOWER_STOP, shutdown_listener) return snmp_engine
def get_snmp_engine(hass): """Get SNMP engine.""" _LOGGER.debug("Creating SNMP engine") snmp_engine = hlapi.SnmpEngine() @callback def shutdown_listener(ev): if hass.data.get(DOMAIN): _LOGGER.debug("Unconfiguring SNMP engine") lcd.unconfigure(hass.data[DOMAIN][SNMP], None) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown_listener) return snmp_engine
async def _get_data(self): """Retreive data from printer.""" raw_data = {} snmp_engine = hlapi.SnmpEngine() try: request_args = [ snmp_engine, hlapi.CommunityData("public", mpModel=0), hlapi.UdpTransportTarget((self._host, self._port), timeout=2, retries=10), hlapi.ContextData(), ] errindication, errstatus, errindex, restable = await hlapi.getCmd( *request_args, *self._oids) # unconfigure SNMP engine lcd.unconfigure(snmp_engine, None) except PySnmpError as error: self.data = {} raise ConnectionError(error) if errindication: self.data = {} raise SnmpError(errindication) if errstatus: self.data = {} raise SnmpError(f"{errstatus}, {errindex}") for resrow in restable: if str(resrow[0]) in OIDS_HEX: # asOctet gives bytes data b'\x00\x01\x04\x00\x00\x03\xf6\xff' temp = resrow[-1].asOctets() # convert to string without checksum FF at the end, gives 000104000003f6 temp = "".join(["%.2x" % x for x in temp])[0:-2] # split to 14 digits words in list, gives ['000104000003f6'] temp = [ temp[ind:ind + 2 * self._split] for ind in range(0, len(temp), 2 * self._split) ] # map sensors names to OIDs raw_data[str(resrow[0])] = temp else: raw_data[str(resrow[0])] = str(resrow[-1]) return raw_data
async def _get_data(self): """Retreive data from printer.""" raw_data = {} if not self._snmp_engine: self._snmp_engine = hlapi.SnmpEngine() try: request_args = [ self._snmp_engine, hlapi.CommunityData("public", mpModel=0), hlapi.UdpTransportTarget( (self._host, self._port), timeout=2, retries=10 ), hlapi.ContextData(), ] errindication, errstatus, errindex, restable = await hlapi.getCmd( *request_args, *self._oids ) except PySnmpError as err: self.data = {} raise ConnectionError(err) from err if errindication: self.data = {} raise SnmpError(errindication) if errstatus: self.data = {} raise SnmpError(f"{errstatus}, {errindex}") for resrow in restable: if str(resrow[0]) in OIDS_HEX: # asOctet gives bytes data b'c\x01\x04\x00\x00\x00\x01\x11\x01\x04\x00\ # x00\x05,A\x01\x04\x00\x00"\xc41\x01\x04\x00\x00\x00\x01o\x01\x04\x00\ # x00\x19\x00\x81\x01\x04\x00\x00\x00F\x86\x01\x04\x00\x00\x00\n\xff' temp = resrow[-1].asOctets() # convert to string without checksum FF at the end, gives # '630104000000011101040000052c410104000022c4310104000000016f01040000190 # 0810104000000468601040000000a' temp = "".join(["%.2x" % x for x in temp])[0:-2] # split to 14 digits words in list, gives ['63010400000001', # '1101040000052c', '410104000022c4', '31010400000001', # '6f010400001900', '81010400000046', '8601040000000a'] temp = [temp[ind : ind + 14] for ind in range(0, len(temp), 14)] # map sensors names to OIDs raw_data[str(resrow[0])] = temp else: raw_data[str(resrow[0])] = str(resrow[-1]) # for legacy printers for resrow in restable: if str(resrow[0]) == OIDS[ATTR_MAINTENANCE]: # asOctet gives bytes data temp = resrow[-1].asOctets() # convert to string without checksum FF at the end, gives # 'a101020414a201020c14a301020614a401020b14' temp = "".join(["%.2x" % x for x in temp])[0:-2] if self._legacy_printer(temp): self._legacy = True # split to 10 digits words in list, gives ['a101020414', # 'a201020c14', 'a301020614', 'a401020b14'] temp = [temp[ind : ind + 10] for ind in range(0, len(temp), 10)] # map sensors names to OIDs raw_data[str(resrow[0])] = temp break return raw_data