Use EntityDescription - onewire (#55003)

This commit is contained in:
epenet 2021-08-24 10:37:59 +02:00 committed by GitHub
parent 2796f65453
commit ccaf0d5c75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 836 additions and 721 deletions

View file

@ -1,9 +1,13 @@
"""Support for 1-Wire binary sensors."""
from __future__ import annotations
from dataclasses import dataclass
import os
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_IDENTIFIERS,
@ -16,77 +20,83 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import CONF_TYPE_OWSERVER, DOMAIN, SENSOR_TYPE_SENSED
from .model import DeviceComponentDescription
from .onewire_entities import OneWireBaseEntity, OneWireProxyEntity
from .const import CONF_TYPE_OWSERVER, DOMAIN, READ_MODE_BOOL
from .onewire_entities import OneWireEntityDescription, OneWireProxyEntity
from .onewirehub import OneWireHub
DEVICE_BINARY_SENSORS: dict[str, list[DeviceComponentDescription]] = {
# Family : { path, sensor_type }
"12": [
{
"path": "sensed.A",
"name": "Sensed A",
"type": SENSOR_TYPE_SENSED,
"default_disabled": True,
},
{
"path": "sensed.B",
"name": "Sensed B",
"type": SENSOR_TYPE_SENSED,
"default_disabled": True,
},
],
"29": [
{
"path": "sensed.0",
"name": "Sensed 0",
"type": SENSOR_TYPE_SENSED,
"default_disabled": True,
},
{
"path": "sensed.1",
"name": "Sensed 1",
"type": SENSOR_TYPE_SENSED,
"default_disabled": True,
},
{
"path": "sensed.2",
"name": "Sensed 2",
"type": SENSOR_TYPE_SENSED,
"default_disabled": True,
},
{
"path": "sensed.3",
"name": "Sensed 3",
"type": SENSOR_TYPE_SENSED,
"default_disabled": True,
},
{
"path": "sensed.4",
"name": "Sensed 4",
"type": SENSOR_TYPE_SENSED,
"default_disabled": True,
},
{
"path": "sensed.5",
"name": "Sensed 5",
"type": SENSOR_TYPE_SENSED,
"default_disabled": True,
},
{
"path": "sensed.6",
"name": "Sensed 6",
"type": SENSOR_TYPE_SENSED,
"default_disabled": True,
},
{
"path": "sensed.7",
"name": "Sensed 7",
"type": SENSOR_TYPE_SENSED,
"default_disabled": True,
},
],
@dataclass
class OneWireBinarySensorEntityDescription(
OneWireEntityDescription, BinarySensorEntityDescription
):
"""Class describing OneWire binary sensor entities."""
DEVICE_BINARY_SENSORS: dict[str, tuple[OneWireBinarySensorEntityDescription, ...]] = {
"12": (
OneWireBinarySensorEntityDescription(
key="sensed.A",
entity_registry_enabled_default=False,
name="Sensed A",
read_mode=READ_MODE_BOOL,
),
OneWireBinarySensorEntityDescription(
key="sensed.B",
entity_registry_enabled_default=False,
name="Sensed B",
read_mode=READ_MODE_BOOL,
),
),
"29": (
OneWireBinarySensorEntityDescription(
key="sensed.0",
entity_registry_enabled_default=False,
name="Sensed 0",
read_mode=READ_MODE_BOOL,
),
OneWireBinarySensorEntityDescription(
key="sensed.1",
entity_registry_enabled_default=False,
name="Sensed 1",
read_mode=READ_MODE_BOOL,
),
OneWireBinarySensorEntityDescription(
key="sensed.2",
entity_registry_enabled_default=False,
name="Sensed 2",
read_mode=READ_MODE_BOOL,
),
OneWireBinarySensorEntityDescription(
key="sensed.3",
entity_registry_enabled_default=False,
name="Sensed 3",
read_mode=READ_MODE_BOOL,
),
OneWireBinarySensorEntityDescription(
key="sensed.4",
entity_registry_enabled_default=False,
name="Sensed 4",
read_mode=READ_MODE_BOOL,
),
OneWireBinarySensorEntityDescription(
key="sensed.5",
entity_registry_enabled_default=False,
name="Sensed 5",
read_mode=READ_MODE_BOOL,
),
OneWireBinarySensorEntityDescription(
key="sensed.6",
entity_registry_enabled_default=False,
name="Sensed 6",
read_mode=READ_MODE_BOOL,
),
OneWireBinarySensorEntityDescription(
key="sensed.7",
entity_registry_enabled_default=False,
name="Sensed 7",
read_mode=READ_MODE_BOOL,
),
),
}
@ -104,12 +114,12 @@ async def async_setup_entry(
async_add_entities(entities, True)
def get_entities(onewirehub: OneWireHub) -> list[OneWireBaseEntity]:
def get_entities(onewirehub: OneWireHub) -> list[BinarySensorEntity]:
"""Get a list of entities."""
if not onewirehub.devices:
return []
entities: list[OneWireBaseEntity] = []
entities: list[BinarySensorEntity] = []
for device in onewirehub.devices:
family = device["family"]
@ -124,17 +134,18 @@ def get_entities(onewirehub: OneWireHub) -> list[OneWireBaseEntity]:
ATTR_MODEL: device_type,
ATTR_NAME: device_id,
}
for entity_specs in DEVICE_BINARY_SENSORS[family]:
entity_path = os.path.join(
os.path.split(device["path"])[0], entity_specs["path"]
for description in DEVICE_BINARY_SENSORS[family]:
device_file = os.path.join(
os.path.split(device["path"])[0], description.key
)
name = f"{device_id} {description.name}"
entities.append(
OneWireProxyBinarySensor(
description=description,
device_id=device_id,
device_name=device_id,
device_file=device_file,
device_info=device_info,
entity_path=entity_path,
entity_specs=entity_specs,
name=name,
owproxy=onewirehub.owproxy,
)
)
@ -145,6 +156,8 @@ def get_entities(onewirehub: OneWireHub) -> list[OneWireBaseEntity]:
class OneWireProxyBinarySensor(OneWireProxyEntity, BinarySensorEntity):
"""Implementation of a 1-Wire binary sensor."""
entity_description: OneWireBinarySensorEntityDescription
@property
def is_on(self) -> bool:
"""Return true if sensor is on."""