Skip to content

Commit a8220ad

Browse files
committed
Add missed part to TemperatureSensor factory
1 parent 9341bff commit a8220ad

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Get temperature when there is only one 1-wire device on the bus::
4646
from digitemp.master import UART_Adapter
4747
from digitemp.device import TemperatureSensor
4848

49-
sensor = TemperatureSensor(UART_Adapter('/dev/ttyS0')
49+
sensor = TemperatureSensor(UART_Adapter('/dev/ttyS0'))
5050
sensor.info()
5151
print(sensor.get_temperature())
5252

digitemp/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.4.0'
1+
__version__ = '1.4.1'

digitemp/device/factories.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@
33
from ..exceptions import OneWireException
44
from ..utils import *
55

6+
if PY3:
7+
from typing import Optional
8+
69
__temperatureSensors = {
710
0x10: DS18S20,
811
0x22: DS1822,
912
0x28: DS18B20,
1013
}
1114

1215

13-
def TemperatureSensor(bus, rom):
14-
# type: (UART_Adapter, str) -> OneWireTemperatureSensor
16+
def TemperatureSensor(bus, rom=None):
17+
"""
18+
If rom is not, will try to find a connected device.
19+
In that case there must be only one device connected.
20+
"""
21+
# type: (UART_Adapter, Optional[str]) -> OneWireTemperatureSensor
22+
if rom is None:
23+
roms = bus.get_connected_ROMs()
24+
if len(roms) > 1:
25+
raise OneWireException(f"More than one connected 1-wire device found: {', '.join(roms)}")
26+
rom = roms[0]
1527
family_code = iord(str2rom(rom), 0)
1628
if family_code not in __temperatureSensors:
1729
raise OneWireException("Unsupported family code: %d" % family_code)

0 commit comments

Comments
 (0)