From 893bbd34bbbdce9fee887a840b6d626cf8085ba2 Mon Sep 17 00:00:00 2001 From: MrDadoo Date: Sat, 1 Feb 2020 23:12:28 +0100 Subject: [PATCH 1/7] 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. --- homeassistant/components/onewire/sensor.py | 44 ++++++++++++++++++---- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/onewire/sensor.py b/homeassistant/components/onewire/sensor.py index 936bf9f751ba7e..edc4f16d038d7a 100644 --- a/homeassistant/components/onewire/sensor.py +++ b/homeassistant/components/onewire/sensor.py @@ -3,6 +3,7 @@ import logging import os import time +import json from pyownet import protocol import voluptuous as vol @@ -19,6 +20,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"}, @@ -27,6 +29,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"}, @@ -54,6 +59,7 @@ } SENSOR_TYPES = { + # SensorType: [ Measured unity, Unit ] "temperature": ["temperature", TEMP_CELSIUS], "humidity": ["humidity", "%"], "humidity_raw": ["humidity", "%"], @@ -70,6 +76,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( @@ -92,15 +102,17 @@ def hb_info_from_type(dev_type="std"): def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the one wire Sensors.""" - base_dir = config[CONF_MOUNT_DIR] - owport = config[CONF_PORT] + base_dir = config.get(CONF_MOUNT_DIR) + owport = config.get(CONF_PORT) owhost = config.get(CONF_HOST) + _LOGGER.info("onewire initializing using : %s %s", owhost, owport) + 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] + _LOGGER.info("device_names dictionary contains %s", json.dumps(device_names)) # We have an owserver on a remote(or local) host/port if owhost: try: @@ -113,6 +125,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): devices = [] for device in devices: _LOGGER.debug("found device: %s", device) + _LOGGER.info("found device: %s", device) family = owproxy.read(f"{device}family").decode() dev_type = "std" if "EF" in family: @@ -136,6 +149,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None): sensor_key = f"wetness_{id}" sensor_id = os.path.split(os.path.split(device)[0])[1] device_file = os.path.join(os.path.split(device)[0], sensor_value) + + _LOGGER.info("appending: id:%s dev:%s as:%s",sensor_id, device_file, device_names.get(sensor_id, sensor_id)) + devs.append( OneWireProxy( device_names.get(sensor_id, sensor_id), @@ -198,6 +214,7 @@ def __init__(self, name, device_file, sensor_type): """Initialize the sensor.""" self._name = name + " " + sensor_type.capitalize() self._device_file = device_file + self._sensor_type = sensor_type self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] self._state = None @@ -224,6 +241,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, + "sensor_type": self._sensor_type, + "raw_value": self._value_raw + } + class OneWireProxy(OneWire): """Implementation of a One wire Sensor through owserver.""" @@ -249,7 +276,8 @@ 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 @@ -267,6 +295,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 @@ -280,6 +309,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: From 9f65f87bf9f80e30f55ad8dce77cd39770bd7136 Mon Sep 17 00:00:00 2001 From: MrDadoo Date: Sun, 2 Feb 2020 18:52:32 +0100 Subject: [PATCH 2/7] Changed to _LOGGER.debug in some places Resorted includes --- homeassistant/components/onewire/sensor.py | 29 +++++++++++----------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/onewire/sensor.py b/homeassistant/components/onewire/sensor.py index edc4f16d038d7a..1697d939e61414 100644 --- a/homeassistant/components/onewire/sensor.py +++ b/homeassistant/components/onewire/sensor.py @@ -1,5 +1,6 @@ """Support for 1-Wire environment sensors.""" from glob import glob +import json import logging import os import time @@ -59,7 +60,7 @@ } SENSOR_TYPES = { - # SensorType: [ Measured unity, Unit ] + # SensorType: [ Measured unit, Unit ] "temperature": ["temperature", TEMP_CELSIUS], "humidity": ["humidity", "%"], "humidity_raw": ["humidity", "%"], @@ -102,17 +103,20 @@ def hb_info_from_type(dev_type="std"): def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the one wire Sensors.""" - base_dir = config.get(CONF_MOUNT_DIR) - owport = config.get(CONF_PORT) - owhost = config.get(CONF_HOST) - _LOGGER.info("onewire initializing using : %s %s", owhost, owport) - + base_dir = config[CONF_MOUNT_DIR] + owport = config[CONF_PORT] + owhost = config[CONF_HOST] + if owhost: + _LOGGER.debug("Initializing using %s:%s", owhost, owport) + else: + _LOGGER.debug("Initializing using %s", base_dir) + devs = [] device_names = {} if CONF_NAMES in config: if isinstance(config[CONF_NAMES], dict): device_names = config[CONF_NAMES] - _LOGGER.info("device_names dictionary contains %s", json.dumps(device_names)) + # We have an owserver on a remote(or local) host/port if owhost: try: @@ -124,8 +128,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): ) devices = [] for device in devices: - _LOGGER.debug("found device: %s", device) - _LOGGER.info("found device: %s", device) + _LOGGER.debug("Found device: %s", device) family = owproxy.read(f"{device}family").decode() dev_type = "std" if "EF" in family: @@ -150,8 +153,6 @@ def setup_platform(hass, config, add_entities, discovery_info=None): sensor_id = os.path.split(os.path.split(device)[0])[1] device_file = os.path.join(os.path.split(device)[0], sensor_value) - _LOGGER.info("appending: id:%s dev:%s as:%s",sensor_id, device_file, device_names.get(sensor_id, sensor_id)) - devs.append( OneWireProxy( device_names.get(sensor_id, sensor_id), @@ -214,7 +215,6 @@ def __init__(self, name, device_file, sensor_type): """Initialize the sensor.""" self._name = name + " " + sensor_type.capitalize() self._device_file = device_file - self._sensor_type = sensor_type self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] self._state = None @@ -247,8 +247,7 @@ def device_state_attributes(self): return { "device_file" : self._device_file, - "sensor_type": self._sensor_type, - "raw_value": self._value_raw + "raw_value" : self._value_raw } @@ -277,7 +276,7 @@ def update(self): if value_read: value = round(float(value_read), 1) self._value_raw = float(value_read) - + self._state = value From d458514b277e3aac9f5670a69f5615a376c16038 Mon Sep 17 00:00:00 2001 From: MrDadoo Date: Tue, 4 Feb 2020 10:03:46 +0100 Subject: [PATCH 3/7] Fixup of code to reflect review comment, comply to Black and pass tests --- homeassistant/components/onewire/sensor.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/onewire/sensor.py b/homeassistant/components/onewire/sensor.py index 1697d939e61414..ea0c5bf66de787 100644 --- a/homeassistant/components/onewire/sensor.py +++ b/homeassistant/components/onewire/sensor.py @@ -1,10 +1,8 @@ """Support for 1-Wire environment sensors.""" from glob import glob -import json import logging import os import time -import json from pyownet import protocol import voluptuous as vol @@ -105,12 +103,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the one wire Sensors.""" base_dir = config[CONF_MOUNT_DIR] owport = config[CONF_PORT] - owhost = config[CONF_HOST] + owhost = config.get(CONF_HOST) if owhost: - _LOGGER.debug("Initializing using %s:%s", owhost, owport) + _LOGGER.info("Initializing using %s:%s", owhost, owport) else: - _LOGGER.debug("Initializing using %s", base_dir) - + _LOGGER.info("Initializing using %s", base_dir) + devs = [] device_names = {} if CONF_NAMES in config: @@ -152,7 +150,6 @@ def setup_platform(hass, config, add_entities, discovery_info=None): sensor_key = f"wetness_{id}" sensor_id = os.path.split(os.path.split(device)[0])[1] device_file = os.path.join(os.path.split(device)[0], sensor_value) - devs.append( OneWireProxy( device_names.get(sensor_id, sensor_id), @@ -217,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.""" @@ -244,11 +242,7 @@ def unit_of_measurement(self): @property def device_state_attributes(self): """Return the state attributes of the sensor.""" - - return { - "device_file" : self._device_file, - "raw_value" : self._value_raw - } + return {"device_file": self._device_file, "raw_value": self._value_raw} class OneWireProxy(OneWire): From 16404a5d6d4ce5d93910f22630c76319f94738d7 Mon Sep 17 00:00:00 2001 From: MrDadoo Date: Fri, 7 Feb 2020 18:11:32 +0100 Subject: [PATCH 4/7] Added unique_id Entity property with device file id and added device_file as attribute that takes its data from Entities unique_id-. --- homeassistant/components/onewire/sensor.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/onewire/sensor.py b/homeassistant/components/onewire/sensor.py index ea0c5bf66de787..24a64ef71f2e62 100644 --- a/homeassistant/components/onewire/sensor.py +++ b/homeassistant/components/onewire/sensor.py @@ -242,8 +242,12 @@ def unit_of_measurement(self): @property def device_state_attributes(self): """Return the state attributes of the sensor.""" - return {"device_file": self._device_file, "raw_value": self._value_raw} + return {"device_file": self.unique_id, "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.""" From 23fb3cf30316d50113e57f1404dad4936c1a8f21 Mon Sep 17 00:00:00 2001 From: MrDadoo Date: Fri, 7 Feb 2020 20:05:29 +0100 Subject: [PATCH 5/7] Missing blank line identified by fake8 and black --- homeassistant/components/onewire/sensor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/homeassistant/components/onewire/sensor.py b/homeassistant/components/onewire/sensor.py index 24a64ef71f2e62..01ef0b7d21b46d 100644 --- a/homeassistant/components/onewire/sensor.py +++ b/homeassistant/components/onewire/sensor.py @@ -249,6 +249,7 @@ def unique_id(self) -> str: """Return a unique ID.""" return self._device_file + class OneWireProxy(OneWire): """Implementation of a One wire Sensor through owserver.""" From 0f3d551b3e011c27fa6ed0e7fdeef3ad6bec0989 Mon Sep 17 00:00:00 2001 From: MrDadoo Date: Sun, 9 Feb 2020 18:39:18 +0100 Subject: [PATCH 6/7] Changed to let device_state_attributes return attribute device_file from the self._device_file member variable of the entity. --- homeassistant/components/onewire/sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/onewire/sensor.py b/homeassistant/components/onewire/sensor.py index 01ef0b7d21b46d..68ca76f697b3a0 100644 --- a/homeassistant/components/onewire/sensor.py +++ b/homeassistant/components/onewire/sensor.py @@ -242,7 +242,7 @@ def unit_of_measurement(self): @property def device_state_attributes(self): """Return the state attributes of the sensor.""" - return {"device_file": self.unique_id, "raw_value": self._value_raw} + return {"device_file": self._device_file, "raw_value": self._value_raw} @property def unique_id(self) -> str: From e763aae9da7f8dd37703cb6c5eb317ca4c77aaeb Mon Sep 17 00:00:00 2001 From: MrDadoo Date: Mon, 10 Feb 2020 12:37:49 +0100 Subject: [PATCH 7/7] Changed from info to debug logging --- homeassistant/components/onewire/sensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/onewire/sensor.py b/homeassistant/components/onewire/sensor.py index 68ca76f697b3a0..6a7f282ac87dac 100644 --- a/homeassistant/components/onewire/sensor.py +++ b/homeassistant/components/onewire/sensor.py @@ -105,9 +105,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None): owport = config[CONF_PORT] owhost = config.get(CONF_HOST) if owhost: - _LOGGER.info("Initializing using %s:%s", owhost, owport) + _LOGGER.debug("Initializing using %s:%s", owhost, owport) else: - _LOGGER.info("Initializing using %s", base_dir) + _LOGGER.debug("Initializing using %s", base_dir) devs = [] device_names = {}