Update inheritance for onewire entities (#43019)
This commit is contained in:
parent
6f4219d709
commit
7716740c18
4 changed files with 50 additions and 36 deletions
|
@ -5,7 +5,7 @@ from homeassistant.components.binary_sensor import BinarySensorEntity
|
|||
from homeassistant.const import CONF_TYPE
|
||||
|
||||
from .const import CONF_TYPE_OWSERVER, DOMAIN, SENSOR_TYPE_SENSED
|
||||
from .onewire_entities import OneWireProxy
|
||||
from .onewire_entities import OneWireProxyEntity
|
||||
from .onewirehub import OneWireHub
|
||||
|
||||
DEVICE_BINARY_SENSORS = {
|
||||
|
@ -109,7 +109,7 @@ def get_entities(onewirehub: OneWireHub):
|
|||
os.path.split(device["path"])[0], device_sensor["path"]
|
||||
)
|
||||
entities.append(
|
||||
OneWireBinarySensor(
|
||||
OneWireProxyBinarySensor(
|
||||
sensor_id,
|
||||
device_file,
|
||||
device_sensor["type"],
|
||||
|
@ -123,7 +123,7 @@ def get_entities(onewirehub: OneWireHub):
|
|||
return entities
|
||||
|
||||
|
||||
class OneWireBinarySensor(BinarySensorEntity, OneWireProxy):
|
||||
class OneWireProxyBinarySensor(OneWireProxyEntity, BinarySensorEntity):
|
||||
"""Implementation of a 1-Wire binary sensor."""
|
||||
|
||||
@property
|
||||
|
|
|
@ -5,7 +5,6 @@ from typing import Any, Dict, Optional
|
|||
from pyownet import protocol
|
||||
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from .const import (
|
||||
SENSOR_TYPE_COUNT,
|
||||
|
@ -18,24 +17,24 @@ from .const import (
|
|||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class OneWire(Entity):
|
||||
"""Implementation of a 1-Wire sensor."""
|
||||
class OneWireBaseEntity(Entity):
|
||||
"""Implementation of a 1-Wire entity."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name,
|
||||
device_file,
|
||||
sensor_type: str,
|
||||
sensor_name: str = None,
|
||||
entity_type: str,
|
||||
entity_name: str = None,
|
||||
device_info=None,
|
||||
default_disabled: bool = False,
|
||||
):
|
||||
"""Initialize the sensor."""
|
||||
self._name = f"{name} {sensor_name or sensor_type.capitalize()}"
|
||||
"""Initialize the entity."""
|
||||
self._name = f"{name} {entity_name or entity_type.capitalize()}"
|
||||
self._device_file = device_file
|
||||
self._sensor_type = sensor_type
|
||||
self._device_class = SENSOR_TYPES[sensor_type][1]
|
||||
self._unit_of_measurement = SENSOR_TYPES[sensor_type][0]
|
||||
self._entity_type = entity_type
|
||||
self._device_class = SENSOR_TYPES[entity_type][1]
|
||||
self._unit_of_measurement = SENSOR_TYPES[entity_type][0]
|
||||
self._device_info = device_info
|
||||
self._state = None
|
||||
self._value_raw = None
|
||||
|
@ -43,14 +42,9 @@ class OneWire(Entity):
|
|||
|
||||
@property
|
||||
def name(self) -> Optional[str]:
|
||||
"""Return the name of the sensor."""
|
||||
"""Return the name of the entity."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def state(self) -> StateType:
|
||||
"""Return the state of the sensor."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def device_class(self) -> Optional[str]:
|
||||
"""Return the class of this device."""
|
||||
|
@ -63,7 +57,7 @@ class OneWire(Entity):
|
|||
|
||||
@property
|
||||
def device_state_attributes(self) -> Optional[Dict[str, Any]]:
|
||||
"""Return the state attributes of the sensor."""
|
||||
"""Return the state attributes of the entity."""
|
||||
return {"device_file": self._device_file, "raw_value": self._value_raw}
|
||||
|
||||
@property
|
||||
|
@ -82,22 +76,22 @@ class OneWire(Entity):
|
|||
return not self._default_disabled
|
||||
|
||||
|
||||
class OneWireProxy(OneWire):
|
||||
"""Implementation of a 1-Wire sensor through owserver."""
|
||||
class OneWireProxyEntity(OneWireBaseEntity):
|
||||
"""Implementation of a 1-Wire entity connected through owserver."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
device_file: str,
|
||||
sensor_type: str,
|
||||
sensor_name: str,
|
||||
entity_type: str,
|
||||
entity_name: str,
|
||||
device_info: Dict[str, Any],
|
||||
default_disabled: bool,
|
||||
owproxy: protocol._Proxy,
|
||||
):
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(
|
||||
name, device_file, sensor_type, sensor_name, device_info, default_disabled
|
||||
name, device_file, entity_type, entity_name, device_info, default_disabled
|
||||
)
|
||||
self._owproxy = owproxy
|
||||
|
||||
|
@ -117,9 +111,9 @@ class OneWireProxy(OneWire):
|
|||
except protocol.Error as exc:
|
||||
_LOGGER.error("Owserver failure in read(), got: %s", exc)
|
||||
else:
|
||||
if self._sensor_type == SENSOR_TYPE_COUNT:
|
||||
if self._entity_type == SENSOR_TYPE_COUNT:
|
||||
value = int(self._value_raw)
|
||||
elif self._sensor_type in [
|
||||
elif self._entity_type in [
|
||||
SENSOR_TYPE_SENSED,
|
||||
SWITCH_TYPE_LATCH,
|
||||
SWITCH_TYPE_PIO,
|
||||
|
|
|
@ -10,6 +10,7 @@ from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|||
from homeassistant.config_entries import SOURCE_IMPORT
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TYPE
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from .const import (
|
||||
CONF_MOUNT_DIR,
|
||||
|
@ -30,7 +31,7 @@ from .const import (
|
|||
SENSOR_TYPE_VOLTAGE,
|
||||
SENSOR_TYPE_WETNESS,
|
||||
)
|
||||
from .onewire_entities import OneWire, OneWireProxy
|
||||
from .onewire_entities import OneWireBaseEntity, OneWireProxyEntity
|
||||
from .onewirehub import OneWireHub
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -277,7 +278,7 @@ def get_entities(onewirehub: OneWireHub, config):
|
|||
os.path.split(device["path"])[0], device_sensor["path"]
|
||||
)
|
||||
entities.append(
|
||||
OneWireProxy(
|
||||
OneWireProxySensor(
|
||||
device_names.get(sensor_id, sensor_id),
|
||||
device_file,
|
||||
device_sensor["type"],
|
||||
|
@ -311,7 +312,7 @@ def get_entities(onewirehub: OneWireHub, config):
|
|||
}
|
||||
device_file = f"/sys/bus/w1/devices/{sensor_id}/w1_slave"
|
||||
entities.append(
|
||||
OneWireDirect(
|
||||
OneWireDirectSensor(
|
||||
device_names.get(sensor_id, sensor_id),
|
||||
device_file,
|
||||
device_info,
|
||||
|
@ -350,7 +351,7 @@ def get_entities(onewirehub: OneWireHub, config):
|
|||
os.path.split(family_file_path)[0], sensor_value
|
||||
)
|
||||
entities.append(
|
||||
OneWireOWFS(
|
||||
OneWireOWFSSensor(
|
||||
device_names.get(sensor_id, sensor_id),
|
||||
device_file,
|
||||
sensor_key,
|
||||
|
@ -360,7 +361,16 @@ def get_entities(onewirehub: OneWireHub, config):
|
|||
return entities
|
||||
|
||||
|
||||
class OneWireDirect(OneWire):
|
||||
class OneWireProxySensor(OneWireProxyEntity):
|
||||
"""Implementation of a 1-Wire sensor connected through owserver."""
|
||||
|
||||
@property
|
||||
def state(self) -> StateType:
|
||||
"""Return the state of the entity."""
|
||||
return self._state
|
||||
|
||||
|
||||
class OneWireDirectSensor(OneWireBaseEntity):
|
||||
"""Implementation of a 1-Wire sensor directly connected to RPI GPIO."""
|
||||
|
||||
def __init__(self, name, device_file, device_info, owsensor):
|
||||
|
@ -368,6 +378,11 @@ class OneWireDirect(OneWire):
|
|||
super().__init__(name, device_file, "temperature", "Temperature", device_info)
|
||||
self._owsensor = owsensor
|
||||
|
||||
@property
|
||||
def state(self) -> StateType:
|
||||
"""Return the state of the entity."""
|
||||
return self._state
|
||||
|
||||
def update(self):
|
||||
"""Get the latest data from the device."""
|
||||
value = None
|
||||
|
@ -383,13 +398,18 @@ class OneWireDirect(OneWire):
|
|||
self._state = value
|
||||
|
||||
|
||||
class OneWireOWFS(OneWire): # pragma: no cover
|
||||
class OneWireOWFSSensor(OneWireBaseEntity): # pragma: no cover
|
||||
"""Implementation of a 1-Wire sensor through owfs.
|
||||
|
||||
This part of the implementation does not conform to policy regarding 3rd-party libraries, and will not longer be updated.
|
||||
https://developers.home-assistant.io/docs/creating_platform_code_review/#5-communication-with-devicesservices
|
||||
"""
|
||||
|
||||
@property
|
||||
def state(self) -> StateType:
|
||||
"""Return the state of the entity."""
|
||||
return self._state
|
||||
|
||||
def _read_value_raw(self):
|
||||
"""Read the value as it is returned by the sensor."""
|
||||
with open(self._device_file) as ds_device_file:
|
||||
|
|
|
@ -6,7 +6,7 @@ from homeassistant.components.switch import SwitchEntity
|
|||
from homeassistant.const import CONF_TYPE
|
||||
|
||||
from .const import CONF_TYPE_OWSERVER, DOMAIN, SWITCH_TYPE_LATCH, SWITCH_TYPE_PIO
|
||||
from .onewire_entities import OneWireProxy
|
||||
from .onewire_entities import OneWireProxyEntity
|
||||
from .onewirehub import OneWireHub
|
||||
|
||||
DEVICE_SWITCHES = {
|
||||
|
@ -173,7 +173,7 @@ def get_entities(onewirehub: OneWireHub):
|
|||
os.path.split(device["path"])[0], device_switch["path"]
|
||||
)
|
||||
entities.append(
|
||||
OneWireSwitch(
|
||||
OneWireProxySwitch(
|
||||
sensor_id,
|
||||
device_file,
|
||||
device_switch["type"],
|
||||
|
@ -187,7 +187,7 @@ def get_entities(onewirehub: OneWireHub):
|
|||
return entities
|
||||
|
||||
|
||||
class OneWireSwitch(SwitchEntity, OneWireProxy):
|
||||
class OneWireProxySwitch(OneWireProxyEntity, SwitchEntity):
|
||||
"""Implementation of a 1-Wire switch."""
|
||||
|
||||
@property
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue