Skip to content

Commit

Permalink
Add missed part to TemperatureSensor factory
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsakoff committed Apr 11, 2021
1 parent 9341bff commit a8220ad
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Get temperature when there is only one 1-wire device on the bus::
from digitemp.master import UART_Adapter
from digitemp.device import TemperatureSensor

sensor = TemperatureSensor(UART_Adapter('/dev/ttyS0')
sensor = TemperatureSensor(UART_Adapter('/dev/ttyS0'))
sensor.info()
print(sensor.get_temperature())

Expand Down
2 changes: 1 addition & 1 deletion digitemp/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.4.0'
__version__ = '1.4.1'
16 changes: 14 additions & 2 deletions digitemp/device/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@
from ..exceptions import OneWireException
from ..utils import *

if PY3:
from typing import Optional

__temperatureSensors = {
0x10: DS18S20,
0x22: DS1822,
0x28: DS18B20,
}


def TemperatureSensor(bus, rom):
# type: (UART_Adapter, str) -> OneWireTemperatureSensor
def TemperatureSensor(bus, rom=None):
"""
If rom is not, will try to find a connected device.
In that case there must be only one device connected.
"""
# type: (UART_Adapter, Optional[str]) -> OneWireTemperatureSensor
if rom is None:
roms = bus.get_connected_ROMs()
if len(roms) > 1:
raise OneWireException(f"More than one connected 1-wire device found: {', '.join(roms)}")
rom = roms[0]
family_code = iord(str2rom(rom), 0)
if family_code not in __temperatureSensors:
raise OneWireException("Unsupported family code: %d" % family_code)
Expand Down

0 comments on commit a8220ad

Please sign in to comment.