async def test_owserver_setup_valid_device(owproxy, hass, device_id, platform): """Test for 1-Wire device. As they would be on a clean setup: all binary-sensors and switches disabled. """ await async_setup_component(hass, "persistent_notification", {}) entity_registry = mock_registry(hass) device_registry = mock_device_registry(hass) mock_device_sensor = MOCK_DEVICE_SENSORS[device_id] device_family = device_id[0:2] dir_return_value = [f"/{device_id}/"] read_side_effect = [device_family.encode()] if "inject_reads" in mock_device_sensor: read_side_effect += mock_device_sensor["inject_reads"] expected_sensors = mock_device_sensor.get(platform, []) for expected_sensor in expected_sensors: read_side_effect.append(expected_sensor["injected_value"]) # Ensure enough read side effect read_side_effect.extend([ProtocolError("Missing injected value")] * 20) owproxy.return_value.dir.return_value = dir_return_value owproxy.return_value.read.side_effect = read_side_effect with patch("homeassistant.components.onewire.SUPPORTED_PLATFORMS", [platform]): await setup_onewire_patched_owserver_integration(hass) await hass.async_block_till_done() assert len(entity_registry.entities) == len(expected_sensors) if len(expected_sensors) > 0: device_info = mock_device_sensor["device_info"] assert len(device_registry.devices) == 1 registry_entry = device_registry.async_get_device( {(DOMAIN, device_id)}, set()) assert registry_entry is not None assert registry_entry.identifiers == {(DOMAIN, device_id)} assert registry_entry.manufacturer == device_info["manufacturer"] assert registry_entry.name == device_info["name"] assert registry_entry.model == device_info["model"] for expected_sensor in expected_sensors: entity_id = expected_sensor["entity_id"] registry_entry = entity_registry.entities.get(entity_id) assert registry_entry is not None assert registry_entry.unique_id == expected_sensor["unique_id"] assert registry_entry.unit_of_measurement == expected_sensor["unit"] assert registry_entry.device_class == expected_sensor["class"] assert registry_entry.disabled == expected_sensor.get( "disabled", False) state = hass.states.get(entity_id) if registry_entry.disabled: assert state is None else: assert state.state == expected_sensor["result"] assert state.attributes["device_file"] == expected_sensor.get( "device_file", registry_entry.unique_id)
def setup_owproxy_mock_devices( owproxy: MagicMock, platform: str, 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_devices(owproxy, domain, device_ids) -> None: """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()] if "inject_reads" in mock_device: main_read_side_effect += mock_device["inject_reads"] # Setup sub-device reads device_sensors = mock_device.get(domain, []) for expected_sensor in device_sensors: sub_read_side_effect.append(expected_sensor["injected_value"]) # 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
async def test_sensors_on_owserver_coupler(owproxy, hass, device_id): """Test for 1-Wire sensors connected to DS2409 coupler.""" await async_setup_component(hass, "persistent_notification", {}) entity_registry = mock_registry(hass) mock_coupler = MOCK_COUPLERS[device_id] dir_side_effect = [] # List of lists of string read_side_effect = [] # List of byte arrays dir_side_effect.append([f"/{device_id}/"]) # dir on root read_side_effect.append(device_id[0:2].encode()) # read family on root if "inject_reads" in mock_coupler: read_side_effect += mock_coupler["inject_reads"] expected_sensors = [] for branch, branch_details in mock_coupler["branches"].items(): dir_side_effect.append([ # dir on branch f"/{device_id}/{branch}/{sub_device_id}/" for sub_device_id in branch_details ]) for sub_device_id, sub_device in branch_details.items(): read_side_effect.append(sub_device_id[0:2].encode()) if "inject_reads" in sub_device: read_side_effect.extend(sub_device["inject_reads"]) expected_sensors += sub_device[SENSOR_DOMAIN] for expected_sensor in sub_device[SENSOR_DOMAIN]: read_side_effect.append(expected_sensor["injected_value"]) # Ensure enough read side effect read_side_effect.extend([ProtocolError("Missing injected value")] * 10) owproxy.return_value.dir.side_effect = dir_side_effect owproxy.return_value.read.side_effect = read_side_effect with patch("homeassistant.components.onewire.SUPPORTED_PLATFORMS", [SENSOR_DOMAIN]): await setup_onewire_patched_owserver_integration(hass) await hass.async_block_till_done() assert len(entity_registry.entities) == len(expected_sensors) for expected_sensor in expected_sensors: entity_id = expected_sensor["entity_id"] registry_entry = entity_registry.entities.get(entity_id) assert registry_entry is not None assert registry_entry.unique_id == expected_sensor["unique_id"] assert registry_entry.unit_of_measurement == expected_sensor["unit"] assert registry_entry.device_class == expected_sensor["class"] assert registry_entry.disabled == expected_sensor.get( "disabled", False) state = hass.states.get(entity_id) if registry_entry.disabled: assert state is None else: assert state.state == expected_sensor["result"] assert state.attributes["device_file"] == expected_sensor[ "device_file"]
async def test_owserver_binary_sensor(owproxy, hass, device_id): """Test for 1-Wire binary sensor. This test forces all entities to be enabled. """ await async_setup_component(hass, "persistent_notification", {}) entity_registry = mock_registry(hass) mock_device_sensor = MOCK_DEVICE_SENSORS[device_id] device_family = device_id[0:2] dir_return_value = [f"/{device_id}/"] read_side_effect = [device_family.encode()] if "inject_reads" in mock_device_sensor: read_side_effect += mock_device_sensor["inject_reads"] expected_sensors = mock_device_sensor[BINARY_SENSOR_DOMAIN] for expected_sensor in expected_sensors: read_side_effect.append(expected_sensor["injected_value"]) # Ensure enough read side effect read_side_effect.extend([ProtocolError("Missing injected value")] * 10) owproxy.return_value.dir.return_value = dir_return_value owproxy.return_value.read.side_effect = read_side_effect # Force enable binary sensors patch_device_binary_sensors = copy.deepcopy(DEVICE_BINARY_SENSORS) for item in patch_device_binary_sensors[device_family]: item["default_disabled"] = False with patch( "homeassistant.components.onewire.SUPPORTED_PLATFORMS", [BINARY_SENSOR_DOMAIN] ), patch.dict( "homeassistant.components.onewire.binary_sensor.DEVICE_BINARY_SENSORS", patch_device_binary_sensors, ): await setup_onewire_patched_owserver_integration(hass) await hass.async_block_till_done() assert len(entity_registry.entities) == len(expected_sensors) for expected_sensor in expected_sensors: entity_id = expected_sensor["entity_id"] registry_entry = entity_registry.entities.get(entity_id) assert registry_entry is not None state = hass.states.get(entity_id) assert state.state == expected_sensor["result"] assert state.attributes["device_file"] == expected_sensor.get( "device_file", registry_entry.unique_id )
async def test_owserver_switch(owproxy, hass, device_id): """Test for 1-Wire switch. This test forces all entities to be enabled. """ await async_setup_component(hass, "persistent_notification", {}) entity_registry = mock_registry(hass) mock_device_sensor = MOCK_DEVICE_SENSORS[device_id] device_family = device_id[0:2] dir_return_value = [f"/{device_id}/"] read_side_effect = [device_family.encode()] if "inject_reads" in mock_device_sensor: read_side_effect += mock_device_sensor["inject_reads"] expected_sensors = mock_device_sensor[SWITCH_DOMAIN] for expected_sensor in expected_sensors: read_side_effect.append(expected_sensor["injected_value"]) # Ensure enough read side effect read_side_effect.extend([ProtocolError("Missing injected value")] * 10) owproxy.return_value.dir.return_value = dir_return_value owproxy.return_value.read.side_effect = read_side_effect # Force enable switches patch_device_switches = copy.deepcopy(DEVICE_SWITCHES) for item in patch_device_switches[device_family]: item["default_disabled"] = False with patch("homeassistant.components.onewire.SUPPORTED_PLATFORMS", [SWITCH_DOMAIN]), patch.dict( "homeassistant.components.onewire.switch.DEVICE_SWITCHES", patch_device_switches): await setup_onewire_patched_owserver_integration(hass) await hass.async_block_till_done() assert len(entity_registry.entities) == len(expected_sensors) for expected_sensor in expected_sensors: entity_id = expected_sensor["entity_id"] registry_entry = entity_registry.entities.get(entity_id) assert registry_entry is not None state = hass.states.get(entity_id) assert state.state == expected_sensor["result"] if state.state == STATE_ON: owproxy.return_value.read.side_effect = [b" 0"] expected_sensor["result"] = STATE_OFF elif state.state == STATE_OFF: owproxy.return_value.read.side_effect = [b" 1"] expected_sensor["result"] = STATE_ON await hass.services.async_call( SWITCH_DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id}, blocking=True, ) await hass.async_block_till_done() state = hass.states.get(entity_id) assert state.state == expected_sensor["result"]