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-10-20 09:43:59 +00:00
|
|
|
from types import MappingProxyType
|
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
|
|
|
|
|
2021-10-18 17:16:53 +00:00
|
|
|
from homeassistant.components.onewire.const import DEFAULT_SYSBUS_MOUNT_DIR
|
2021-10-20 09:43:59 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, ATTR_STATE
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_registry import EntityRegistry
|
2020-10-24 01:57:16 +00:00
|
|
|
|
2021-10-20 09:43:59 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_DEFAULT_DISABLED,
|
|
|
|
ATTR_DEVICE_FILE,
|
|
|
|
ATTR_INJECT_READS,
|
|
|
|
ATTR_UNIQUE_ID,
|
|
|
|
FIXED_ATTRIBUTES,
|
|
|
|
MOCK_OWPROXY_DEVICES,
|
|
|
|
MOCK_SYSBUS_DEVICES,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def check_and_enable_disabled_entities(
|
|
|
|
entity_registry: EntityRegistry, expected_entities: MappingProxyType
|
|
|
|
) -> None:
|
|
|
|
"""Ensure that the expected_entities are correctly disabled."""
|
|
|
|
for expected_entity in expected_entities:
|
|
|
|
if expected_entity.get(ATTR_DEFAULT_DISABLED):
|
|
|
|
entity_id = expected_entity[ATTR_ENTITY_ID]
|
|
|
|
registry_entry = entity_registry.entities.get(entity_id)
|
|
|
|
assert registry_entry.disabled
|
|
|
|
assert registry_entry.disabled_by == "integration"
|
|
|
|
entity_registry.async_update_entity(entity_id, **{"disabled_by": None})
|
|
|
|
|
|
|
|
|
|
|
|
def check_entities(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entity_registry: EntityRegistry,
|
|
|
|
expected_entities: MappingProxyType,
|
|
|
|
) -> None:
|
|
|
|
"""Ensure that the expected_entities are correct."""
|
|
|
|
for expected_entity in expected_entities:
|
|
|
|
entity_id = expected_entity[ATTR_ENTITY_ID]
|
|
|
|
registry_entry = entity_registry.entities.get(entity_id)
|
|
|
|
assert registry_entry is not None
|
|
|
|
assert registry_entry.unique_id == expected_entity[ATTR_UNIQUE_ID]
|
|
|
|
state = hass.states.get(entity_id)
|
|
|
|
assert state.state == expected_entity[ATTR_STATE]
|
|
|
|
assert state.attributes[ATTR_DEVICE_FILE] == expected_entity.get(
|
|
|
|
ATTR_DEVICE_FILE, registry_entry.unique_id
|
|
|
|
)
|
|
|
|
for attr in FIXED_ATTRIBUTES:
|
|
|
|
assert state.attributes.get(attr) == expected_entity.get(attr)
|
2021-04-01 13:06:47 +00:00
|
|
|
|
|
|
|
|
2021-10-18 17:16:53 +00:00
|
|
|
def setup_owproxy_mock_devices(
|
|
|
|
owproxy: MagicMock, platform: str, device_ids: list(str)
|
|
|
|
) -> None:
|
2021-04-01 13:06:47 +00:00
|
|
|
"""Set up mock for owproxy."""
|
|
|
|
dir_return_value = []
|
|
|
|
main_read_side_effect = []
|
|
|
|
sub_read_side_effect = []
|
|
|
|
|
|
|
|
for device_id in device_ids:
|
|
|
|
mock_device = MOCK_OWPROXY_DEVICES[device_id]
|
|
|
|
|
|
|
|
# Setup directory listing
|
|
|
|
dir_return_value += [f"/{device_id}/"]
|
|
|
|
|
|
|
|
# Setup device reads
|
|
|
|
main_read_side_effect += [device_id[0:2].encode()]
|
2021-10-19 19:41:01 +00:00
|
|
|
if ATTR_INJECT_READS in mock_device:
|
|
|
|
main_read_side_effect += mock_device[ATTR_INJECT_READS]
|
2021-04-01 13:06:47 +00:00
|
|
|
|
|
|
|
# Setup sub-device reads
|
2021-10-18 17:16:53 +00:00
|
|
|
device_sensors = mock_device.get(platform, [])
|
2021-04-01 13:06:47 +00:00
|
|
|
for expected_sensor in device_sensors:
|
2021-10-19 19:41:01 +00:00
|
|
|
sub_read_side_effect.append(expected_sensor[ATTR_INJECT_READS])
|
2021-04-01 13:06:47 +00:00
|
|
|
|
|
|
|
# Ensure enough read side effect
|
|
|
|
read_side_effect = (
|
|
|
|
main_read_side_effect
|
|
|
|
+ sub_read_side_effect
|
|
|
|
+ [ProtocolError("Missing injected value")] * 20
|
|
|
|
)
|
|
|
|
owproxy.return_value.dir.return_value = dir_return_value
|
|
|
|
owproxy.return_value.read.side_effect = read_side_effect
|
2021-04-03 21:08:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup_sysbus_mock_devices(
|
2021-10-18 17:16:53 +00:00
|
|
|
platform: str, device_ids: list[str]
|
2021-04-10 13:21:11 +00:00
|
|
|
) -> tuple[list[str], list[Any]]:
|
2021-04-03 21:08:35 +00:00
|
|
|
"""Set up mock for sysbus."""
|
|
|
|
glob_result = []
|
|
|
|
read_side_effect = []
|
|
|
|
|
|
|
|
for device_id in device_ids:
|
|
|
|
mock_device = MOCK_SYSBUS_DEVICES[device_id]
|
|
|
|
|
|
|
|
# Setup directory listing
|
|
|
|
glob_result += [f"/{DEFAULT_SYSBUS_MOUNT_DIR}/{device_id}"]
|
|
|
|
|
|
|
|
# Setup sub-device reads
|
2021-10-18 17:16:53 +00:00
|
|
|
device_sensors = mock_device.get(platform, [])
|
2021-04-03 21:08:35 +00:00
|
|
|
for expected_sensor in device_sensors:
|
2021-10-19 19:41:01 +00:00
|
|
|
if isinstance(expected_sensor[ATTR_INJECT_READS], list):
|
|
|
|
read_side_effect += expected_sensor[ATTR_INJECT_READS]
|
2021-04-03 21:08:35 +00:00
|
|
|
else:
|
2021-10-19 19:41:01 +00:00
|
|
|
read_side_effect.append(expected_sensor[ATTR_INJECT_READS])
|
2021-04-03 21:08:35 +00:00
|
|
|
|
|
|
|
# Ensure enough read side effect
|
|
|
|
read_side_effect.extend([FileNotFoundError("Missing injected value")] * 20)
|
|
|
|
|
|
|
|
return (glob_result, read_side_effect)
|