core/tests/components/onewire/__init__.py

108 lines
3.3 KiB
Python
Raw Normal View History

"""Tests for 1-Wire integration."""
from __future__ import annotations
Add config flow for One wire (#39321) * Add support for config_flow Add support for switches Add support for binary sensors * Add config flow strings * Update .coveragerc * black-formatting * fixes for isort and flake * fixes for pylint * fixes for isort * fixes for isort * fixes for config_flow * Add devices to Device Registry * Updated comments * fixes for flake8 * Updated comments * Updated comments * Implement async_unload_entry * remove binary_sensor and switch implementation (will move to new PR) * Update .coveragerc Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update .coveragerc * Review config flow to store the configuration type * Add config_flow tests * Move CONF_NAMES to constants * Fix isort * Tweak to onewire logger * Tweak to onewire logger for sensor * Reset _LOGGER variable * Normalise header * Normalise header * Update to use references in config flow translations * Improve test coverage * fixes for isort * Update async_unload_entry * Update common strings * Update imports * Move connect attempt to executor * Update common strings * Remove OWFS from config_flow * Prevent duplicate config entries * Fix isort * Fix flake8 * Fix tests following removal of OWFS implementation * Fix flake8 * Adjust config from yaml to config-flow, and add ability to specify sysbus directory * Ajust unique_id for config entries * Fix test_config_flow * Fix invalid merge * Convert yaml to config_entry * Update sysbus tests * Tweaks to yaml import process, and add OWFS warning * Enable migration of OWFS platform config to config_entry * update the existing corresponding config entry on OWFS conversion * Remove CONFIG_SCHEMA * Move data_schema to constants * Remove log message * Remove duplicate warning * Update already_configured to use already_configured_device constant * Schedule get_entities on the executor * Update duplicate entry check for OWServer * Review TryCatch * Schedule os.path.isdir on the executor * rename owhost/owport * Update checks for empty * Fix incorrect patch * Move config_flow validation methods to new OneWireHub * Fix typo and pre-commit * Cleanup try/else * patch async_setup/async_setup_entry * cleanup implicit exit point * Fix invalid patch * cleanup implicit exit point * cleanup implicit exit point Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-10-24 01:57:16 +00:00
from typing import Any
from unittest.mock import MagicMock
2021-01-01 21:31:56 +00:00
from pyownet.protocol import ProtocolError
2023-04-29 09:18:16 +00:00
from homeassistant.const import Platform
Add config flow for One wire (#39321) * Add support for config_flow Add support for switches Add support for binary sensors * Add config flow strings * Update .coveragerc * black-formatting * fixes for isort and flake * fixes for pylint * fixes for isort * fixes for isort * fixes for config_flow * Add devices to Device Registry * Updated comments * fixes for flake8 * Updated comments * Updated comments * Implement async_unload_entry * remove binary_sensor and switch implementation (will move to new PR) * Update .coveragerc Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update .coveragerc * Review config flow to store the configuration type * Add config_flow tests * Move CONF_NAMES to constants * Fix isort * Tweak to onewire logger * Tweak to onewire logger for sensor * Reset _LOGGER variable * Normalise header * Normalise header * Update to use references in config flow translations * Improve test coverage * fixes for isort * Update async_unload_entry * Update common strings * Update imports * Move connect attempt to executor * Update common strings * Remove OWFS from config_flow * Prevent duplicate config entries * Fix isort * Fix flake8 * Fix tests following removal of OWFS implementation * Fix flake8 * Adjust config from yaml to config-flow, and add ability to specify sysbus directory * Ajust unique_id for config entries * Fix test_config_flow * Fix invalid merge * Convert yaml to config_entry * Update sysbus tests * Tweaks to yaml import process, and add OWFS warning * Enable migration of OWFS platform config to config_entry * update the existing corresponding config entry on OWFS conversion * Remove CONFIG_SCHEMA * Move data_schema to constants * Remove log message * Remove duplicate warning * Update already_configured to use already_configured_device constant * Schedule get_entities on the executor * Update duplicate entry check for OWServer * Review TryCatch * Schedule os.path.isdir on the executor * rename owhost/owport * Update checks for empty * Fix incorrect patch * Move config_flow validation methods to new OneWireHub * Fix typo and pre-commit * Cleanup try/else * patch async_setup/async_setup_entry * cleanup implicit exit point * Fix invalid patch * cleanup implicit exit point * cleanup implicit exit point Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-10-24 01:57:16 +00:00
2023-04-29 09:18:16 +00:00
from .const import ATTR_INJECT_READS, MOCK_OWPROXY_DEVICES
def setup_owproxy_mock_devices(
owproxy: MagicMock, platform: Platform, device_ids: list[str]
) -> None:
"""Set up mock for owproxy."""
main_dir_return_value = []
sub_dir_side_effect = []
main_read_side_effect = []
sub_read_side_effect = []
for device_id in device_ids:
_setup_owproxy_mock_device(
main_dir_return_value,
sub_dir_side_effect,
main_read_side_effect,
sub_read_side_effect,
device_id,
platform,
)
# Ensure enough read side effect
dir_side_effect = [main_dir_return_value] + sub_dir_side_effect
read_side_effect = (
main_read_side_effect
+ sub_read_side_effect
+ [ProtocolError("Missing injected value")] * 20
)
owproxy.return_value.dir.side_effect = dir_side_effect
owproxy.return_value.read.side_effect = read_side_effect
def _setup_owproxy_mock_device(
main_dir_return_value: list,
sub_dir_side_effect: list,
main_read_side_effect: list,
sub_read_side_effect: list,
device_id: str,
platform: Platform,
) -> None:
"""Set up mock for owproxy."""
mock_device = MOCK_OWPROXY_DEVICES[device_id]
# Setup directory listing
main_dir_return_value += [f"/{device_id}/"]
if "branches" in mock_device:
# Setup branch directory listing
for branch, branch_details in mock_device["branches"].items():
sub_dir_side_effect.append(
[ # dir on branch
f"/{device_id}/{branch}/{sub_device_id}/"
for sub_device_id in branch_details
]
)
_setup_owproxy_mock_device_reads(
main_read_side_effect,
sub_read_side_effect,
mock_device,
device_id,
platform,
)
if "branches" in mock_device:
for branch_details in mock_device["branches"].values():
for sub_device_id, sub_device in branch_details.items():
_setup_owproxy_mock_device_reads(
main_read_side_effect,
sub_read_side_effect,
sub_device,
sub_device_id,
platform,
)
def _setup_owproxy_mock_device_reads(
main_read_side_effect: list,
sub_read_side_effect: list,
mock_device: Any,
device_id: str,
platform: Platform,
) -> None:
"""Set up mock for owproxy."""
# Setup device reads
main_read_side_effect += [device_id[0:2].encode()]
if ATTR_INJECT_READS in mock_device:
main_read_side_effect += mock_device[ATTR_INJECT_READS]
# Setup sub-device reads
device_sensors = mock_device.get(platform, [])
if platform is Platform.SENSOR and device_id.startswith("12"):
# We need to check if there is TAI8570 plugged in
for expected_sensor in device_sensors:
sub_read_side_effect.append(expected_sensor[ATTR_INJECT_READS])
for expected_sensor in device_sensors:
sub_read_side_effect.append(expected_sensor[ATTR_INJECT_READS])