Add Onewire diagnostic and config switches and binary_sensors (#59309)

* Onewire: Add diagnostic and config switches and binary_sensors
This commit adds diagnostic and config switches and binary_sensors to
the HobbyBoards devices.  With these, the user will be able to configure
those devices, without having to run owwrite/owread commands outside
of HA.

* Address review from @epenet

* Add HB_HUB to DEVICE_SUPPORT_OWSERVER

* Device class and entity category enums

* Fixup merge breakage

* Remove duplicate lines
This commit is contained in:
Tim Rightnour 2021-12-07 05:59:43 -07:00 committed by GitHub
parent 45c463b61c
commit 489d85d862
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 241 additions and 7 deletions

View file

@ -6,6 +6,7 @@ import os
from typing import TYPE_CHECKING
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
@ -13,10 +14,12 @@ from homeassistant.components.onewire.model import OWServerDeviceDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_TYPE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
CONF_TYPE_OWSERVER,
DEVICE_KEYS_0_3,
DEVICE_KEYS_0_7,
DEVICE_KEYS_A_B,
DOMAIN,
@ -61,8 +64,33 @@ DEVICE_BINARY_SENSORS: dict[str, tuple[OneWireBinarySensorEntityDescription, ...
)
for id in DEVICE_KEYS_A_B
),
"EF": (), # "HobbyBoard": special
}
# EF sensors are usually hobbyboards specialized sensors.
HOBBYBOARD_EF: dict[str, tuple[OneWireBinarySensorEntityDescription, ...]] = {
"HB_HUB": tuple(
OneWireBinarySensorEntityDescription(
key=f"hub/short.{id}",
entity_registry_enabled_default=False,
name=f"Hub Short on Branch {id}",
read_mode=READ_MODE_BOOL,
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
)
for id in DEVICE_KEYS_0_3
),
}
def get_sensor_types(
device_sub_type: str,
) -> dict[str, tuple[OneWireBinarySensorEntityDescription, ...]]:
"""Return the proper info array for the device type."""
if "HobbyBoard" in device_sub_type:
return HOBBYBOARD_EF
return DEVICE_BINARY_SENSORS
async def async_setup_entry(
hass: HomeAssistant,
@ -89,11 +117,16 @@ def get_entities(onewirehub: OneWireHub) -> list[BinarySensorEntity]:
assert isinstance(device, OWServerDeviceDescription)
family = device.family
device_id = device.id
device_type = device.type
device_info = device.device_info
device_sub_type = "std"
if "EF" in family:
device_sub_type = "HobbyBoard"
family = device_type
if family not in DEVICE_BINARY_SENSORS:
if family not in get_sensor_types(device_sub_type):
continue
for description in DEVICE_BINARY_SENSORS[family]:
for description in get_sensor_types(device_sub_type)[family]:
device_file = os.path.join(os.path.split(device.path)[0], description.key)
name = f"{device_id} {description.name}"
entities.append(