diff --git a/brother/__init__.py b/brother/__init__.py index 7c4de49..f6ebf46 100644 --- a/brother/__init__.py +++ b/brother/__init__.py @@ -4,6 +4,7 @@ """ import logging +import chardet from pysnmp.error import PySnmpError import pysnmp.hlapi.asyncio as hlapi from pysnmp.hlapi.asyncore.cmdgen import lcd @@ -60,13 +61,27 @@ async def async_update(self): ) try: self.firmware = raw_data[OIDS[ATTR_FIRMWARE]] - data[ATTR_STATUS] = ( - raw_data[OIDS[ATTR_STATUS]] - .strip() - .encode("latin1") - .decode("iso_8859_2") - .lower() - ) + code_page = chardet.detect(raw_data[OIDS[ATTR_STATUS]].encode("latin1"))[ + "encoding" + ] + # chardet detects Polish as ISO-8859-1 but Polish should use ISO-8859-2 + print(code_page) + if code_page == "ISO-8859-1": + data[ATTR_STATUS] = ( + raw_data[OIDS[ATTR_STATUS]] + .strip() + .encode("latin1") + .decode("iso_8859_2") + .lower() + ) + else: + data[ATTR_STATUS] = ( + raw_data[OIDS[ATTR_STATUS]] + .strip() + .encode("latin1") + .decode(code_page) + .lower() + ) data[ATTR_PAGE_COUNT] = raw_data[OIDS[ATTR_PAGE_COUNT]] data[ATTR_UPTIME] = round(int(raw_data[OIDS[ATTR_UPTIME]]) / 8640000) except (AttributeError, KeyError, TypeError): diff --git a/setup.py b/setup.py index bfd79e5..b95402b 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name="brother", - version="0.1.4", + version="0.1.5", author="Maciej Bieniek", author_email="maciej.bieniek@gmail.com", description="Python wrapper for getting data from Brother laser and inkjet \ @@ -14,7 +14,7 @@ license="Apache 2", packages=["brother"], python_requires=">=3.6", - install_requires=["pysnmp"], + install_requires=["pysnmp", "chardet"], classifiers=[ "License :: OSI Approved :: Apache Software License", "Programming Language :: Python", diff --git a/tests/data/dcp-l2540dn.json b/tests/data/dcp-l2540dn.json new file mode 100644 index 0000000..4e86c2d --- /dev/null +++ b/tests/data/dcp-l2540dn.json @@ -0,0 +1,19 @@ +{ + "1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.10.0": ["0001040000014d"], + "1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.17.0": "R1906110243", + "1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.8.0": [ + "63010400000001", + "1101040000014d", + "41010400002648", + "31010400000001", + "6f01040000157c", + "8101040000003c", + "86010400000010" + ], + "1.3.6.1.4.1.2435.2.4.3.2435.5.13.3.0": "Brother DCP-L2540DN series", + "1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.11.0": ["82010400002d93"], + "1.3.6.1.2.1.43.10.2.1.4.1.1": "333", + "1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.1.0": "serial_number", + "1.3.6.1.4.1.2435.2.3.9.4.2.1.5.4.5.2.0": "ÁßïéØÙ àÕÖØÜ ", + "1.3.6.1.2.1.1.3.0": "1171125" +} diff --git a/tests/test_base.py b/tests/test_base.py index c9f8301..81e1f75 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -73,6 +73,26 @@ async def test_dcp_j132w_model(): assert brother.data["page_counter"] == 879 +@pytest.mark.asyncio +async def test_dcp_l2540dw_model(): + """Test with valid data from DCP-L2540DN printer with status in Russian.""" + with open("tests/data/dcp-l2540dn.json") as file: + data = json.load(file) + + with patch("brother.Brother._get_data", return_value=data): + + brother = Brother(HOST, kind="laser") + await brother.async_update() + + assert brother.available == True + assert brother.model == "DCP-L2540DN" + assert brother.firmware == "R1906110243" + assert brother.serial == "serial_number" + assert brother.data["status"] == "спящий режим" + assert brother.data["black_toner_remaining"] == 55 + assert brother.data["page_counter"] == 333 + + @pytest.mark.asyncio async def test_mfc_j680dw_model(): """Test with valid data from MFC-J680DW printer."""