mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Update Onewire SysBus integration to use a 3rd party library (#40943)
This commit is contained in:
parent
3bb0e00abe
commit
843c4e27f3
@ -305,7 +305,7 @@ homeassistant/components/ohmconnect/* @robbiet480
|
|||||||
homeassistant/components/ombi/* @larssont
|
homeassistant/components/ombi/* @larssont
|
||||||
homeassistant/components/omnilogic/* @oliver84 @djtimca @gentoosu
|
homeassistant/components/omnilogic/* @oliver84 @djtimca @gentoosu
|
||||||
homeassistant/components/onboarding/* @home-assistant/core
|
homeassistant/components/onboarding/* @home-assistant/core
|
||||||
homeassistant/components/onewire/* @garbled1
|
homeassistant/components/onewire/* @garbled1 @epenet
|
||||||
homeassistant/components/onvif/* @hunterjm
|
homeassistant/components/onvif/* @hunterjm
|
||||||
homeassistant/components/openerz/* @misialq
|
homeassistant/components/openerz/* @misialq
|
||||||
homeassistant/components/opengarage/* @danielhiversen
|
homeassistant/components/opengarage/* @danielhiversen
|
||||||
|
@ -1,12 +1,18 @@
|
|||||||
"""Constants for 1-Wire component."""
|
"""Constants for 1-Wire component."""
|
||||||
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||||
|
|
||||||
CONF_MOUNT_DIR = "mount_dir"
|
CONF_MOUNT_DIR = "mount_dir"
|
||||||
CONF_NAMES = "names"
|
CONF_NAMES = "names"
|
||||||
|
|
||||||
|
CONF_TYPE_OWFS = "OWFS"
|
||||||
|
CONF_TYPE_OWSERVER = "OWServer"
|
||||||
|
CONF_TYPE_SYSBUS = "SysBus"
|
||||||
|
|
||||||
DEFAULT_OWSERVER_PORT = 4304
|
DEFAULT_OWSERVER_PORT = 4304
|
||||||
DEFAULT_SYSBUS_MOUNT_DIR = "/sys/bus/w1/devices/"
|
DEFAULT_SYSBUS_MOUNT_DIR = "/sys/bus/w1/devices/"
|
||||||
|
|
||||||
DOMAIN = "onewire"
|
DOMAIN = "onewire"
|
||||||
|
|
||||||
SUPPORTED_PLATFORMS = [
|
SUPPORTED_PLATFORMS = [
|
||||||
"sensor",
|
SENSOR_DOMAIN,
|
||||||
]
|
]
|
||||||
|
@ -2,6 +2,6 @@
|
|||||||
"domain": "onewire",
|
"domain": "onewire",
|
||||||
"name": "1-Wire",
|
"name": "1-Wire",
|
||||||
"documentation": "https://www.home-assistant.io/integrations/onewire",
|
"documentation": "https://www.home-assistant.io/integrations/onewire",
|
||||||
"requirements": ["pyownet==0.10.0.post1"],
|
"requirements": ["pyownet==0.10.0.post1", "pi1wire==0.1.0"],
|
||||||
"codeowners": ["@garbled1"]
|
"codeowners": ["@garbled1", "@epenet"]
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,13 @@
|
|||||||
from glob import glob
|
from glob import glob
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import time
|
|
||||||
|
|
||||||
|
from pi1wire import (
|
||||||
|
InvalidCRCException,
|
||||||
|
NotFoundSensorException,
|
||||||
|
Pi1Wire,
|
||||||
|
UnsupportResponseException,
|
||||||
|
)
|
||||||
from pyownet import protocol
|
from pyownet import protocol
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -23,6 +28,9 @@ from homeassistant.helpers.entity import Entity
|
|||||||
from .const import (
|
from .const import (
|
||||||
CONF_MOUNT_DIR,
|
CONF_MOUNT_DIR,
|
||||||
CONF_NAMES,
|
CONF_NAMES,
|
||||||
|
CONF_TYPE_OWFS,
|
||||||
|
CONF_TYPE_OWSERVER,
|
||||||
|
CONF_TYPE_SYSBUS,
|
||||||
DEFAULT_OWSERVER_PORT,
|
DEFAULT_OWSERVER_PORT,
|
||||||
DEFAULT_SYSBUS_MOUNT_DIR,
|
DEFAULT_SYSBUS_MOUNT_DIR,
|
||||||
)
|
)
|
||||||
@ -54,6 +62,8 @@ DEVICE_SENSORS = {
|
|||||||
"EF": {"HobbyBoard": "special"},
|
"EF": {"HobbyBoard": "special"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DEVICE_SUPPORT_SYSBUS = ["10", "22", "28", "3B", "42"]
|
||||||
|
|
||||||
# EF sensors are usually hobbyboards specialized sensors.
|
# EF sensors are usually hobbyboards specialized sensors.
|
||||||
# These can only be read by OWFS. Currently this driver only supports them
|
# These can only be read by OWFS. Currently this driver only supports them
|
||||||
# via owserver (network protocol)
|
# via owserver (network protocol)
|
||||||
@ -120,18 +130,32 @@ def hb_info_from_type(dev_type="std"):
|
|||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up 1-Wire platform."""
|
"""Set up 1-Wire platform."""
|
||||||
base_dir = config[CONF_MOUNT_DIR]
|
entities = get_entities(config)
|
||||||
owport = config[CONF_PORT]
|
add_entities(entities, True)
|
||||||
owhost = config.get(CONF_HOST)
|
|
||||||
|
|
||||||
devs = []
|
|
||||||
|
def get_entities(config):
|
||||||
|
"""Get a list of entities."""
|
||||||
|
base_dir = config[CONF_MOUNT_DIR]
|
||||||
|
owhost = config.get(CONF_HOST)
|
||||||
|
owport = config[CONF_PORT]
|
||||||
|
|
||||||
|
# Ensure type is configured
|
||||||
|
if owhost:
|
||||||
|
conf_type = CONF_TYPE_OWSERVER
|
||||||
|
elif base_dir == DEFAULT_SYSBUS_MOUNT_DIR:
|
||||||
|
conf_type = CONF_TYPE_SYSBUS
|
||||||
|
else:
|
||||||
|
conf_type = CONF_TYPE_OWFS
|
||||||
|
|
||||||
|
entities = []
|
||||||
device_names = {}
|
device_names = {}
|
||||||
if CONF_NAMES in config:
|
if CONF_NAMES in config:
|
||||||
if isinstance(config[CONF_NAMES], dict):
|
if isinstance(config[CONF_NAMES], dict):
|
||||||
device_names = config[CONF_NAMES]
|
device_names = config[CONF_NAMES]
|
||||||
|
|
||||||
# We have an owserver on a remote(or local) host/port
|
# We have an owserver on a remote(or local) host/port
|
||||||
if owhost:
|
if conf_type == CONF_TYPE_OWSERVER:
|
||||||
_LOGGER.debug("Initializing using %s:%s", owhost, owport)
|
_LOGGER.debug("Initializing using %s:%s", owhost, owport)
|
||||||
try:
|
try:
|
||||||
owproxy = protocol.proxy(host=owhost, port=owport)
|
owproxy = protocol.proxy(host=owhost, port=owport)
|
||||||
@ -166,7 +190,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
sensor_key = f"wetness_{s_id}"
|
sensor_key = f"wetness_{s_id}"
|
||||||
sensor_id = os.path.split(os.path.split(device)[0])[1]
|
sensor_id = os.path.split(os.path.split(device)[0])[1]
|
||||||
device_file = os.path.join(os.path.split(device)[0], sensor_value)
|
device_file = os.path.join(os.path.split(device)[0], sensor_value)
|
||||||
devs.append(
|
entities.append(
|
||||||
OneWireProxy(
|
OneWireProxy(
|
||||||
device_names.get(sensor_id, sensor_id),
|
device_names.get(sensor_id, sensor_id),
|
||||||
device_file,
|
device_file,
|
||||||
@ -176,19 +200,34 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# We have a raw GPIO ow sensor on a Pi
|
# We have a raw GPIO ow sensor on a Pi
|
||||||
elif base_dir == DEFAULT_SYSBUS_MOUNT_DIR:
|
elif conf_type == CONF_TYPE_SYSBUS:
|
||||||
_LOGGER.debug("Initializing using SysBus %s", base_dir)
|
_LOGGER.debug("Initializing using SysBus")
|
||||||
for device_family in DEVICE_SENSORS:
|
for p1sensor in Pi1Wire().find_all_sensors():
|
||||||
for device_folder in glob(os.path.join(base_dir, f"{device_family}[.-]*")):
|
family = p1sensor.mac_address[:2]
|
||||||
sensor_id = os.path.split(device_folder)[1]
|
sensor_id = f"{family}-{p1sensor.mac_address[2:]}"
|
||||||
device_file = os.path.join(device_folder, "w1_slave")
|
if family not in DEVICE_SUPPORT_SYSBUS:
|
||||||
devs.append(
|
_LOGGER.warning(
|
||||||
OneWireDirect(
|
"Ignoring unknown family (%s) of sensor found for device: %s",
|
||||||
device_names.get(sensor_id, sensor_id),
|
family,
|
||||||
device_file,
|
sensor_id,
|
||||||
"temperature",
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
device_file = f"/sys/bus/w1/devices/{sensor_id}/w1_slave"
|
||||||
|
entities.append(
|
||||||
|
OneWireDirect(
|
||||||
|
device_names.get(sensor_id, sensor_id),
|
||||||
|
device_file,
|
||||||
|
"temperature",
|
||||||
|
p1sensor,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if not entities:
|
||||||
|
_LOGGER.error(
|
||||||
|
"No onewire sensor found. Check if dtoverlay=w1-gpio "
|
||||||
|
"is in your /boot/config.txt. "
|
||||||
|
"Check the mount_dir parameter if it's defined"
|
||||||
|
)
|
||||||
|
|
||||||
# We have an owfs mounted
|
# We have an owfs mounted
|
||||||
else:
|
else:
|
||||||
@ -204,7 +243,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
device_file = os.path.join(
|
device_file = os.path.join(
|
||||||
os.path.split(family_file_path)[0], sensor_value
|
os.path.split(family_file_path)[0], sensor_value
|
||||||
)
|
)
|
||||||
devs.append(
|
entities.append(
|
||||||
OneWireOWFS(
|
OneWireOWFS(
|
||||||
device_names.get(sensor_id, sensor_id),
|
device_names.get(sensor_id, sensor_id),
|
||||||
device_file,
|
device_file,
|
||||||
@ -212,15 +251,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if devs == []:
|
return entities
|
||||||
_LOGGER.error(
|
|
||||||
"No onewire sensor found. Check if dtoverlay=w1-gpio "
|
|
||||||
"is in your /boot/config.txt. "
|
|
||||||
"Check the mount_dir parameter if it's defined"
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
add_entities(devs, True)
|
|
||||||
|
|
||||||
|
|
||||||
class OneWire(Entity):
|
class OneWire(Entity):
|
||||||
@ -234,12 +265,6 @@ class OneWire(Entity):
|
|||||||
self._state = None
|
self._state = None
|
||||||
self._value_raw = None
|
self._value_raw = None
|
||||||
|
|
||||||
def _read_value_raw(self):
|
|
||||||
"""Read the value as it is returned by the sensor."""
|
|
||||||
with open(self._device_file) as ds_device_file:
|
|
||||||
lines = ds_device_file.readlines()
|
|
||||||
return lines
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
@ -300,24 +325,36 @@ class OneWireProxy(OneWire):
|
|||||||
class OneWireDirect(OneWire):
|
class OneWireDirect(OneWire):
|
||||||
"""Implementation of a 1-Wire sensor directly connected to RPI GPIO."""
|
"""Implementation of a 1-Wire sensor directly connected to RPI GPIO."""
|
||||||
|
|
||||||
|
def __init__(self, name, device_file, sensor_type, owsensor):
|
||||||
|
"""Initialize the sensor."""
|
||||||
|
super().__init__(name, device_file, sensor_type)
|
||||||
|
self._owsensor = owsensor
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data from the device."""
|
"""Get the latest data from the device."""
|
||||||
value = None
|
value = None
|
||||||
lines = self._read_value_raw()
|
try:
|
||||||
while lines[0].strip()[-3:] != "YES":
|
self._value_raw = self._owsensor.get_temperature()
|
||||||
time.sleep(0.2)
|
value = round(float(self._value_raw), 1)
|
||||||
lines = self._read_value_raw()
|
except (
|
||||||
equals_pos = lines[1].find("t=")
|
FileNotFoundError,
|
||||||
if equals_pos != -1:
|
InvalidCRCException,
|
||||||
value_string = lines[1][equals_pos + 2 :]
|
NotFoundSensorException,
|
||||||
value = round(float(value_string) / 1000.0, 1)
|
UnsupportResponseException,
|
||||||
self._value_raw = float(value_string)
|
) as ex:
|
||||||
|
_LOGGER.warning("Cannot read from sensor %s: %s", self._device_file, ex)
|
||||||
self._state = value
|
self._state = value
|
||||||
|
|
||||||
|
|
||||||
class OneWireOWFS(OneWire):
|
class OneWireOWFS(OneWire):
|
||||||
"""Implementation of a 1-Wire sensor through owfs."""
|
"""Implementation of a 1-Wire sensor through owfs."""
|
||||||
|
|
||||||
|
def _read_value_raw(self):
|
||||||
|
"""Read the value as it is returned by the sensor."""
|
||||||
|
with open(self._device_file) as ds_device_file:
|
||||||
|
lines = ds_device_file.readlines()
|
||||||
|
return lines
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data from the device."""
|
"""Get the latest data from the device."""
|
||||||
value = None
|
value = None
|
||||||
|
@ -1087,6 +1087,9 @@ pencompy==0.0.3
|
|||||||
# homeassistant.components.unifi_direct
|
# homeassistant.components.unifi_direct
|
||||||
pexpect==4.6.0
|
pexpect==4.6.0
|
||||||
|
|
||||||
|
# homeassistant.components.onewire
|
||||||
|
pi1wire==0.1.0
|
||||||
|
|
||||||
# homeassistant.components.pi4ioe5v9xxxx
|
# homeassistant.components.pi4ioe5v9xxxx
|
||||||
pi4ioe5v9xxxx==0.0.2
|
pi4ioe5v9xxxx==0.0.2
|
||||||
|
|
||||||
|
@ -516,6 +516,9 @@ pdunehd==1.3.2
|
|||||||
# homeassistant.components.unifi_direct
|
# homeassistant.components.unifi_direct
|
||||||
pexpect==4.6.0
|
pexpect==4.6.0
|
||||||
|
|
||||||
|
# homeassistant.components.onewire
|
||||||
|
pi1wire==0.1.0
|
||||||
|
|
||||||
# homeassistant.components.pilight
|
# homeassistant.components.pilight
|
||||||
pilight==0.1.1
|
pilight==0.1.1
|
||||||
|
|
||||||
@ -773,6 +776,9 @@ pyotp==2.3.0
|
|||||||
# homeassistant.components.openweathermap
|
# homeassistant.components.openweathermap
|
||||||
pyowm==2.10.0
|
pyowm==2.10.0
|
||||||
|
|
||||||
|
# homeassistant.components.onewire
|
||||||
|
pyownet==0.10.0.post1
|
||||||
|
|
||||||
# homeassistant.components.point
|
# homeassistant.components.point
|
||||||
pypoint==1.1.2
|
pypoint==1.1.2
|
||||||
|
|
||||||
|
129
tests/components/onewire/test_entity_sysbus.py
Normal file
129
tests/components/onewire/test_entity_sysbus.py
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
"""Tests for 1-Wire temperature sensor (device family 10, 22, 28, 3B, 42) connected on SysBus."""
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from unittest.mock import PropertyMock, patch
|
||||||
|
|
||||||
|
from pi1wire import (
|
||||||
|
InvalidCRCException,
|
||||||
|
NotFoundSensorException,
|
||||||
|
UnsupportResponseException,
|
||||||
|
)
|
||||||
|
|
||||||
|
from homeassistant.components.onewire.const import (
|
||||||
|
DEFAULT_OWSERVER_PORT,
|
||||||
|
DEFAULT_SYSBUS_MOUNT_DIR,
|
||||||
|
DOMAIN,
|
||||||
|
)
|
||||||
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||||
|
from homeassistant.const import TEMP_CELSIUS
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
from tests.common import async_fire_time_changed, mock_registry
|
||||||
|
|
||||||
|
MOCK_DEVICE_ID = "28-111111111111"
|
||||||
|
MOCK_DEVICE_NAME = "My DS18B20"
|
||||||
|
MOCK_ENTITY_ID = "sensor.my_ds18b20_temperature"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_onewiredirect_setup_valid_device(hass):
|
||||||
|
"""Test that sysbus config entry works correctly."""
|
||||||
|
entity_registry = mock_registry(hass)
|
||||||
|
config = {
|
||||||
|
"sensor": {
|
||||||
|
"platform": DOMAIN,
|
||||||
|
"mount_dir": DEFAULT_SYSBUS_MOUNT_DIR,
|
||||||
|
"port": DEFAULT_OWSERVER_PORT,
|
||||||
|
"names": {
|
||||||
|
MOCK_DEVICE_ID: MOCK_DEVICE_NAME,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.onewire.sensor.Pi1Wire"
|
||||||
|
) as mock_pi1wire, patch("pi1wire.OneWire") as mock_owsensor:
|
||||||
|
type(mock_owsensor).mac_address = PropertyMock(
|
||||||
|
return_value=MOCK_DEVICE_ID.replace("-", "")
|
||||||
|
)
|
||||||
|
mock_owsensor.get_temperature.side_effect = [
|
||||||
|
25.123,
|
||||||
|
FileNotFoundError,
|
||||||
|
25.223,
|
||||||
|
InvalidCRCException,
|
||||||
|
25.323,
|
||||||
|
NotFoundSensorException,
|
||||||
|
25.423,
|
||||||
|
UnsupportResponseException,
|
||||||
|
25.523,
|
||||||
|
]
|
||||||
|
mock_pi1wire.return_value.find_all_sensors.return_value = [mock_owsensor]
|
||||||
|
assert await async_setup_component(hass, SENSOR_DOMAIN, config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert len(entity_registry.entities) == 1
|
||||||
|
registry_entry = entity_registry.entities.get(MOCK_ENTITY_ID)
|
||||||
|
assert registry_entry is not None
|
||||||
|
assert (
|
||||||
|
registry_entry.unique_id == f"/sys/bus/w1/devices/{MOCK_DEVICE_ID}/w1_slave"
|
||||||
|
)
|
||||||
|
assert registry_entry.unit_of_measurement == TEMP_CELSIUS
|
||||||
|
|
||||||
|
# 25.123
|
||||||
|
current_time = datetime.now()
|
||||||
|
state = hass.states.get(MOCK_ENTITY_ID)
|
||||||
|
assert state.state == "25.1"
|
||||||
|
|
||||||
|
# FileNotFoundError
|
||||||
|
current_time = current_time + timedelta(minutes=2)
|
||||||
|
async_fire_time_changed(hass, current_time)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
state = hass.states.get(MOCK_ENTITY_ID)
|
||||||
|
assert state.state == "unknown"
|
||||||
|
|
||||||
|
# 25.223
|
||||||
|
current_time = current_time + timedelta(minutes=2)
|
||||||
|
async_fire_time_changed(hass, current_time)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
state = hass.states.get(MOCK_ENTITY_ID)
|
||||||
|
assert state.state == "25.2"
|
||||||
|
|
||||||
|
# InvalidCRCException
|
||||||
|
current_time = current_time + timedelta(minutes=2)
|
||||||
|
async_fire_time_changed(hass, current_time)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
state = hass.states.get(MOCK_ENTITY_ID)
|
||||||
|
assert state.state == "unknown"
|
||||||
|
|
||||||
|
# 25.323
|
||||||
|
current_time = current_time + timedelta(minutes=2)
|
||||||
|
async_fire_time_changed(hass, current_time)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
state = hass.states.get(MOCK_ENTITY_ID)
|
||||||
|
assert state.state == "25.3"
|
||||||
|
|
||||||
|
# NotFoundSensorException
|
||||||
|
current_time = current_time + timedelta(minutes=2)
|
||||||
|
async_fire_time_changed(hass, current_time)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
state = hass.states.get(MOCK_ENTITY_ID)
|
||||||
|
assert state.state == "unknown"
|
||||||
|
|
||||||
|
# 25.423
|
||||||
|
current_time = current_time + timedelta(minutes=2)
|
||||||
|
async_fire_time_changed(hass, current_time)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
state = hass.states.get(MOCK_ENTITY_ID)
|
||||||
|
assert state.state == "25.4"
|
||||||
|
|
||||||
|
# UnsupportResponseException
|
||||||
|
current_time = current_time + timedelta(minutes=2)
|
||||||
|
async_fire_time_changed(hass, current_time)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
state = hass.states.get(MOCK_ENTITY_ID)
|
||||||
|
assert state.state == "unknown"
|
||||||
|
|
||||||
|
# 25.523
|
||||||
|
current_time = current_time + timedelta(minutes=2)
|
||||||
|
async_fire_time_changed(hass, current_time)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
state = hass.states.get(MOCK_ENTITY_ID)
|
||||||
|
assert state.state == "25.5"
|
Loading…
x
Reference in New Issue
Block a user