Skip to content

Commit

Permalink
Update onewire component (#31419)
Browse files Browse the repository at this point in the history
* Added some measurement points for Family 26.
Added three attriubtes for each sensor, DEVICE_FILE, RAW_VALUE and SENSOR_TYPE
Added some comments.
Updated to get config values for owserver to get owport and owhost.

* Changed to _LOGGER.debug in some places

Resorted includes

* Fixup of code to reflect review comment, comply to Black and pass tests

* Added unique_id Entity property with device file id and added device_file as attribute that takes its data from Entities unique_id-.

* Missing blank line identified by fake8 and black

* Changed to let device_state_attributes return attribute device_file from the self._device_file member variable of the entity.

* Changed from info to debug logging
  • Loading branch information
MrDadoo committed Feb 13, 2020
1 parent f091e04 commit fdfe885
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions homeassistant/components/onewire/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

DEFAULT_MOUNT_DIR = "/sys/bus/w1/devices/"
DEVICE_SENSORS = {
# Family : { SensorType: owfs path }
"10": {"temperature": "temperature"},
"12": {"temperature": "TAI8570/temperature", "pressure": "TAI8570/pressure"},
"22": {"temperature": "temperature"},
Expand All @@ -27,6 +28,9 @@
"humidity": "humidity",
"pressure": "B1-R1-A/pressure",
"illuminance": "S3-R1-A/illuminance",
"voltage_VAD": "VAD",
"voltage_VDD": "VDD",
"current": "IAD",
},
"28": {"temperature": "temperature"},
"3B": {"temperature": "temperature"},
Expand Down Expand Up @@ -54,6 +58,7 @@
}

SENSOR_TYPES = {
# SensorType: [ Measured unit, Unit ]
"temperature": ["temperature", TEMP_CELSIUS],
"humidity": ["humidity", "%"],
"humidity_raw": ["humidity", "%"],
Expand All @@ -70,6 +75,10 @@
"counter_a": ["counter", "count"],
"counter_b": ["counter", "count"],
"HobbyBoard": ["none", "none"],
"voltage": ["voltage", "V"],
"voltage_VAD": ["voltage", "V"],
"voltage_VDD": ["voltage", "V"],
"current": ["current", "A"],
}

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
Expand All @@ -95,11 +104,16 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
base_dir = config[CONF_MOUNT_DIR]
owport = config[CONF_PORT]
owhost = config.get(CONF_HOST)
if owhost:
_LOGGER.debug("Initializing using %s:%s", owhost, owport)
else:
_LOGGER.debug("Initializing using %s", base_dir)

devs = []
device_names = {}
if "names" in config:
if isinstance(config["names"], dict):
device_names = config["names"]
if CONF_NAMES in config:
if isinstance(config[CONF_NAMES], dict):
device_names = config[CONF_NAMES]

# We have an owserver on a remote(or local) host/port
if owhost:
Expand All @@ -112,7 +126,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
)
devices = []
for device in devices:
_LOGGER.debug("found device: %s", device)
_LOGGER.debug("Found device: %s", device)
family = owproxy.read(f"{device}family").decode()
dev_type = "std"
if "EF" in family:
Expand Down Expand Up @@ -200,6 +214,7 @@ def __init__(self, name, device_file, sensor_type):
self._device_file = device_file
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
self._state = None
self._value_raw = None

def _read_value_raw(self):
"""Read the value as it is returned by the sensor."""
Expand All @@ -224,6 +239,16 @@ def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return self._unit_of_measurement

@property
def device_state_attributes(self):
"""Return the state attributes of the sensor."""
return {"device_file": self._device_file, "raw_value": self._value_raw}

@property
def unique_id(self) -> str:
"""Return a unique ID."""
return self._device_file


class OneWireProxy(OneWire):
"""Implementation of a One wire Sensor through owserver."""
Expand All @@ -249,6 +274,7 @@ def update(self):
_LOGGER.error("Owserver failure in read(), got: %s", exc)
if value_read:
value = round(float(value_read), 1)
self._value_raw = float(value_read)

self._state = value

Expand All @@ -267,6 +293,7 @@ def update(self):
if equals_pos != -1:
value_string = lines[1][equals_pos + 2 :]
value = round(float(value_string) / 1000.0, 1)
self._value_raw = float(value_string)
self._state = value


Expand All @@ -280,6 +307,7 @@ def update(self):
value_read = self._read_value_raw()
if len(value_read) == 1:
value = round(float(value_read[0]), 1)
self._value_raw = float(value_read[0])
except ValueError:
_LOGGER.warning("Invalid value read from %s", self._device_file)
except FileNotFoundError:
Expand Down

0 comments on commit fdfe885

Please sign in to comment.