Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions homeassistant/components/onewire/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,25 @@

from dataclasses import dataclass
import os
from typing import TYPE_CHECKING

from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
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,
READ_MODE_BOOL,
)
from .model import OWServerDeviceDescription
from .onewire_entities import OneWireEntityDescription, OneWireProxyEntity
from .onewire_entities import OneWireEntity, OneWireEntityDescription
from .onewirehub import OneWireHub


Expand Down Expand Up @@ -98,23 +94,19 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up 1-Wire platform."""
# Only OWServer implementation works with binary sensors
if config_entry.data[CONF_TYPE] == CONF_TYPE_OWSERVER:
onewirehub = hass.data[DOMAIN][config_entry.entry_id]
onewirehub = hass.data[DOMAIN][config_entry.entry_id]

entities = await hass.async_add_executor_job(get_entities, onewirehub)
async_add_entities(entities, True)
entities = await hass.async_add_executor_job(get_entities, onewirehub)
async_add_entities(entities, True)


def get_entities(onewirehub: OneWireHub) -> list[BinarySensorEntity]:
def get_entities(onewirehub: OneWireHub) -> list[OneWireBinarySensor]:
"""Get a list of entities."""
if not onewirehub.devices:
return []

entities: list[BinarySensorEntity] = []
entities: list[OneWireBinarySensor] = []
for device in onewirehub.devices:
if TYPE_CHECKING:
assert isinstance(device, OWServerDeviceDescription)
family = device.family
device_id = device.id
device_type = device.type
Expand All @@ -130,7 +122,7 @@ def get_entities(onewirehub: OneWireHub) -> list[BinarySensorEntity]:
device_file = os.path.join(os.path.split(device.path)[0], description.key)
name = f"{device_id} {description.name}"
entities.append(
OneWireProxyBinarySensor(
OneWireBinarySensor(
description=description,
device_id=device_id,
device_file=device_file,
Expand All @@ -143,7 +135,7 @@ def get_entities(onewirehub: OneWireHub) -> list[BinarySensorEntity]:
return entities


class OneWireProxyBinarySensor(OneWireProxyEntity, BinarySensorEntity):
class OneWireBinarySensor(OneWireEntity, BinarySensorEntity):
"""Implementation of a 1-Wire binary sensor."""

entity_description: OneWireBinarySensorEntityDescription
Expand Down
117 changes: 16 additions & 101 deletions homeassistant/components/onewire/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@
import voluptuous as vol

from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TYPE
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import config_validation as cv, device_registry as dr
from homeassistant.helpers.device_registry import DeviceRegistry

from .const import (
CONF_MOUNT_DIR,
CONF_TYPE_OWSERVER,
CONF_TYPE_SYSBUS,
DEFAULT_OWSERVER_HOST,
DEFAULT_OWSERVER_PORT,
DEFAULT_SYSBUS_MOUNT_DIR,
DEFAULT_HOST,
DEFAULT_PORT,
DEVICE_SUPPORT_OPTIONS,
DOMAIN,
INPUT_ENTRY_CLEAR_OPTIONS,
Expand All @@ -27,31 +23,21 @@
OPTION_ENTRY_SENSOR_PRECISION,
PRECISION_MAPPING_FAMILY_28,
)
from .model import OWServerDeviceDescription
from .onewirehub import CannotConnect, InvalidPath, OneWireHub
from .model import OWDeviceDescription
from .onewirehub import CannotConnect, OneWireHub

DATA_SCHEMA_USER = vol.Schema(
{vol.Required(CONF_TYPE): vol.In([CONF_TYPE_OWSERVER, CONF_TYPE_SYSBUS])}
)
DATA_SCHEMA_OWSERVER = vol.Schema(
{
vol.Required(CONF_HOST, default=DEFAULT_OWSERVER_HOST): str,
vol.Required(CONF_PORT, default=DEFAULT_OWSERVER_PORT): int,
}
)
DATA_SCHEMA_MOUNTDIR = vol.Schema(
DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_MOUNT_DIR, default=DEFAULT_SYSBUS_MOUNT_DIR): str,
vol.Required(CONF_HOST, default=DEFAULT_HOST): str,
vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
}
)


async def validate_input_owserver(
hass: HomeAssistant, data: dict[str, Any]
) -> dict[str, str]:
async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, str]:
"""Validate the user input allows us to connect.

Data has the keys from DATA_SCHEMA_OWSERVER with values provided by the user.
Data has the keys from DATA_SCHEMA with values provided by the user.
"""

hub = OneWireHub(hass)
Expand All @@ -65,24 +51,6 @@ async def validate_input_owserver(
return {"title": host}


async def validate_input_mount_dir(
hass: HomeAssistant, data: dict[str, Any]
) -> dict[str, str]:
"""Validate the user input allows us to connect.

Data has the keys from DATA_SCHEMA_MOUNTDIR with values provided by the user.
"""
hub = OneWireHub(hass)

mount_dir = data[CONF_MOUNT_DIR]

# Raises InvalidDir exception on failure
await hub.check_mount_dir(mount_dir)

# Return info that you want to store in the config entry.
return {"title": mount_dir}


class OneWireFlowHandler(ConfigFlow, domain=DOMAIN):
"""Handle 1-Wire config flow."""

Expand All @@ -100,29 +68,10 @@ async def async_step_user(
Let user manually input configuration.
"""
errors: dict[str, str] = {}
if user_input is not None:
self.onewire_config.update(user_input)
if CONF_TYPE_OWSERVER == user_input[CONF_TYPE]:
return await self.async_step_owserver()
if CONF_TYPE_SYSBUS == user_input[CONF_TYPE]:
return await self.async_step_mount_dir()

return self.async_show_form(
step_id="user",
data_schema=DATA_SCHEMA_USER,
errors=errors,
)

async def async_step_owserver(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle OWServer configuration."""
errors = {}
if user_input:
# Prevent duplicate entries
self._async_abort_entries_match(
{
CONF_TYPE: CONF_TYPE_OWSERVER,
CONF_HOST: user_input[CONF_HOST],
CONF_PORT: user_input[CONF_PORT],
}
Expand All @@ -131,7 +80,7 @@ async def async_step_owserver(
self.onewire_config.update(user_input)

try:
info = await validate_input_owserver(self.hass, user_input)
info = await validate_input(self.hass, user_input)
except CannotConnect:
errors["base"] = "cannot_connect"
else:
Expand All @@ -140,37 +89,8 @@ async def async_step_owserver(
)

return self.async_show_form(
step_id="owserver",
data_schema=DATA_SCHEMA_OWSERVER,
errors=errors,
)

async def async_step_mount_dir(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle SysBus configuration."""
errors = {}
if user_input:
# Prevent duplicate entries
await self.async_set_unique_id(
f"{CONF_TYPE_SYSBUS}:{user_input[CONF_MOUNT_DIR]}"
)
self._abort_if_unique_id_configured()

self.onewire_config.update(user_input)

try:
info = await validate_input_mount_dir(self.hass, user_input)
except InvalidPath:
errors["base"] = "invalid_path"
else:
return self.async_create_entry(
title=info["title"], data=self.onewire_config
)

return self.async_show_form(
step_id="mount_dir",
data_schema=DATA_SCHEMA_MOUNTDIR,
step_id="user",
data_schema=DATA_SCHEMA,
errors=errors,
)

Expand All @@ -188,21 +108,16 @@ def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize OneWire Network options flow."""
self.entry_id = config_entry.entry_id
self.options = dict(config_entry.options)
self.configurable_devices: dict[str, OWServerDeviceDescription] = {}
self.devices_to_configure: dict[str, OWServerDeviceDescription] = {}
self.configurable_devices: dict[str, OWDeviceDescription] = {}
self.devices_to_configure: dict[str, OWDeviceDescription] = {}
self.current_device: str = ""

async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Manage the options."""
controller: OneWireHub = self.hass.data[DOMAIN][self.entry_id]
if controller.type == CONF_TYPE_SYSBUS:
return self.async_abort(
reason="SysBus setup does not have any config options."
)

all_devices: list[OWServerDeviceDescription] = controller.devices # type: ignore[assignment]
all_devices: list[OWDeviceDescription] = controller.devices # type: ignore[assignment]
if not all_devices:
return self.async_abort(reason="No configurable devices found.")

Expand Down
13 changes: 3 additions & 10 deletions homeassistant/components/onewire/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@

from homeassistant.const import Platform

CONF_MOUNT_DIR = "mount_dir"

CONF_TYPE_OWSERVER = "OWServer"
CONF_TYPE_SYSBUS = "SysBus"

DEFAULT_OWSERVER_HOST = "localhost"
DEFAULT_OWSERVER_PORT = 4304
DEFAULT_SYSBUS_MOUNT_DIR = "/sys/bus/w1/devices/"
DEFAULT_HOST = "localhost"
DEFAULT_PORT = 4304

DOMAIN = "onewire"

DEVICE_KEYS_0_3 = range(4)
DEVICE_KEYS_0_7 = range(8)
DEVICE_KEYS_A_B = ("A", "B")

DEVICE_SUPPORT_OWSERVER = {
DEVICE_SUPPORT = {
"05": (),
"10": (),
"12": (),
Expand All @@ -35,7 +29,6 @@
"7E": ("EDS0066", "EDS0068"),
"EF": ("HB_HUB", "HB_MOISTURE_METER", "HobbyBoards_EF"),
}
DEVICE_SUPPORT_SYSBUS = ["10", "22", "28", "3B", "42"]

DEVICE_SUPPORT_OPTIONS = ["28"]

Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/onewire/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"name": "1-Wire",
"documentation": "https://www.home-assistant.io/integrations/onewire",
"config_flow": true,
"requirements": ["pyownet==0.10.0.post1", "pi1wire==0.1.0"],
"requirements": ["pyownet==0.10.0.post1"],
"codeowners": ["@garbled1", "@epenet"],
"iot_class": "local_polling",
"loggers": ["pi1wire", "pyownet"]
"loggers": ["pyownet"]
}
16 changes: 1 addition & 15 deletions homeassistant/components/onewire/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,15 @@

from dataclasses import dataclass

from pi1wire import OneWireInterface

from homeassistant.helpers.entity import DeviceInfo


@dataclass
class OWDeviceDescription:
"""OWDeviceDescription device description class."""
"""1-Wire device description class."""

device_info: DeviceInfo


@dataclass
class OWDirectDeviceDescription(OWDeviceDescription):
"""SysBus device description class."""

interface: OneWireInterface


@dataclass
class OWServerDeviceDescription(OWDeviceDescription):
"""OWServer device description class."""

family: str
id: str
path: str
Expand Down
Loading