2020-10-14 08:19:12 +00:00
|
|
|
"""Tests for 1-Wire integration."""
|
2021-04-10 13:21:11 +00:00
|
|
|
from __future__ import annotations
|
2020-10-24 01:57:16 +00:00
|
|
|
|
2021-04-10 13:21:11 +00:00
|
|
|
from typing import Any
|
2021-10-18 17:16:53 +00:00
|
|
|
from unittest.mock import MagicMock
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2021-04-01 13:06:47 +00:00
|
|
|
from pyownet.protocol import ProtocolError
|
|
|
|
|
2023-04-29 09:18:16 +00:00
|
|
|
from homeassistant.const import Platform
|
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
|
2021-04-01 13:06:47 +00:00
|
|
|
|
|
|
|
|
2021-10-18 17:16:53 +00:00
|
|
|
def setup_owproxy_mock_devices(
|
2022-05-31 08:40:08 +00:00
|
|
|
owproxy: MagicMock, platform: Platform, device_ids: list[str]
|
2021-10-18 17:16:53 +00:00
|
|
|
) -> None:
|
2021-04-01 13:06:47 +00:00
|
|
|
"""Set up mock for owproxy."""
|
2021-10-22 09:45:40 +00:00
|
|
|
main_dir_return_value = []
|
|
|
|
sub_dir_side_effect = []
|
2021-04-01 13:06:47 +00:00
|
|
|
main_read_side_effect = []
|
|
|
|
sub_read_side_effect = []
|
|
|
|
|
|
|
|
for device_id in device_ids:
|
2021-10-22 09:45:40 +00:00
|
|
|
_setup_owproxy_mock_device(
|
|
|
|
main_dir_return_value,
|
|
|
|
sub_dir_side_effect,
|
|
|
|
main_read_side_effect,
|
|
|
|
sub_read_side_effect,
|
|
|
|
device_id,
|
|
|
|
platform,
|
|
|
|
)
|
2021-04-01 13:06:47 +00:00
|
|
|
|
|
|
|
# Ensure enough read side effect
|
2021-10-22 09:45:40 +00:00
|
|
|
dir_side_effect = [main_dir_return_value] + sub_dir_side_effect
|
2021-04-01 13:06:47 +00:00
|
|
|
read_side_effect = (
|
|
|
|
main_read_side_effect
|
|
|
|
+ sub_read_side_effect
|
|
|
|
+ [ProtocolError("Missing injected value")] * 20
|
|
|
|
)
|
2021-10-22 09:45:40 +00:00
|
|
|
owproxy.return_value.dir.side_effect = dir_side_effect
|
2021-04-01 13:06:47 +00:00
|
|
|
owproxy.return_value.read.side_effect = read_side_effect
|
2021-04-03 21:08:35 +00:00
|
|
|
|
|
|
|
|
2021-10-22 09:45:40 +00:00
|
|
|
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,
|
2022-05-31 08:40:08 +00:00
|
|
|
platform: Platform,
|
2021-10-22 09:45:40 +00:00
|
|
|
) -> 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,
|
2022-05-31 08:40:08 +00:00
|
|
|
platform: Platform,
|
2021-10-22 09:45:40 +00:00
|
|
|
) -> 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, [])
|
2023-02-03 14:11:54 +00:00
|
|
|
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])
|
2021-10-22 09:45:40 +00:00
|
|
|
for expected_sensor in device_sensors:
|
|
|
|
sub_read_side_effect.append(expected_sensor[ATTR_INJECT_READS])
|